import java.awt.*; import java.applet.*; public class solarSystem extends Applet { private int height, width; private solarSystemCanvas ssc; private motion move; public void init() { //Get width and height of applet width = Integer.parseInt(getParameter("width")); height = Integer.parseInt(getParameter("height")); setBackground(Color.black); ssc = new solarSystemCanvas(width, height); setLayout(new BorderLayout()); add("Center", ssc); // a thread for animation } public void start() { if (move == null) { motion move = new motion(ssc); move.start(); } } public void stop() { if (move != null) { move.stop(); move = null; } } } class solarSystemCanvas extends Canvas { //Constants for numbers of items static int NUM_STARS = 200; static int NUM_PLANETS = 9; static int NUM_ROIDS = 100; //Constant names for planets in array static int MERCURY = 0; static int VENUS = 1; static int EARTH = 2; static int MARS = 3; static int JUPITER = 4; static int SATURN = 5; static int URANUS = 6; static int NEPTUNE = 7; static int PLUTO = 8; private int centerX, centerY; private Star[] star; private Graphics offgfx, bggfx; private Image offimage, background; private Planet[] planet, asteroid; public solarSystemCanvas(int width, int height) { //Find the Coordinates of the Center of Applet this.centerX = width / 2; this.centerY = height / 2; //Initialize Stars this.star = new Star[NUM_STARS]; for (int i = 0; i < NUM_STARS; i++) { this.star[i] = new Star(width, height); } //Initialize Planets this.planet = new Planet[NUM_PLANETS]; this.planet[MERCURY] = new Planet(4, 40, Color.cyan); this.planet[VENUS] = new Planet(10, 60, Color.yellow); this.planet[EARTH] = new Planet(10, 80, Color.blue); this.planet[MARS] = new Planet(6, 100, Color.red); this.planet[JUPITER] = new Planet(20, 140, Color.red); this.planet[SATURN] = new Planet(18, 160, Color.magenta); this.planet[URANUS] = new Planet(15, 180, Color.orange); this.planet[NEPTUNE] = new Planet(15,200, Color.blue); this.planet[PLUTO] = new Planet(4, 218, Color.green); //Initialize Asteroids this.asteroid = new Planet[NUM_ROIDS]; for (int i = 0; i < NUM_ROIDS; i++) { this.asteroid[i] = new Planet((int)(Math.random() * 3) + 2, 110 + (int)(Math.random() * 20), Color.white); } //Position Objects repaint(); } public void update(Graphics g) { //Create Background if (background == null) { background = createImage(centerX * 2, centerY * 2); bggfx = background.getGraphics(); //Draw Stars bggfx.setColor(Color.white); for (int i = 0; i < NUM_STARS; i++) { star[i].drawStar(bggfx); } //Draw Sun circle(bggfx, Color.yellow, centerX, centerY, 50); //Draw Planet Orbits for (int i = 0; i < NUM_PLANETS; i++) { planet[i].drawOrbit(bggfx, centerX, centerY); } } //create double buffer if (offgfx == null) { offimage = createImage(centerX * 2, centerY * 2); offgfx = offimage.getGraphics(); } offgfx.drawImage(background, 0, 0, null); //Draw Planets for (int i = 0; i < NUM_PLANETS; i++) { planet[i].movePlanet(centerX, centerY); planet[i].drawPlanet(offgfx); } //Draw Asteroids for (int i = 0; i < NUM_ROIDS; i++) { asteroid[i].movePlanet(centerX, centerY); asteroid[i].drawPlanet(offgfx); } g.drawImage(offimage, 0, 0, null); } public void circle(Graphics g, Color c, int x, int y, int radius) { g.setColor(c); g.fillOval(x - radius / 2, y - radius / 2, radius, radius); } public boolean mouseDown(Event e, int x, int y) { //See if User Clicks on a Planet for(int i = 0; i < NUM_PLANETS; i++) { this.planet[i].testPlanet(x, y); } //See if User Clicks on the Sun if ((x > centerX - 25) && (y > centerY - 25) && (x < centerX + 25) && (y < centerY + 25)) { for(int i = 0; i < NUM_PLANETS; i++) { this.planet[i].unBouncePlanet(); } } return super.mouseDown(e, x, y); } } class Star { private int x, y, size; Star(int width, int height) { this.x = (int)(Math.random() * (width - 2) + 1); this.y = (int)(Math.random() * (height - 2) + 1); this.size = (int)(Math.random() * 3 + 1); } public void drawStar(Graphics g) { g.fillRect(this.x, this.y, this.size, this.size); } } class Planet { private static double pi = 3.141569; private int size, distance; private double radians, speed, x, y, xSpeed, ySpeed; private boolean bounce; private Color color; Planet(int size, int distance, Color color) { this.size = size; this.distance = distance; this.bounce = false; this.color = color; this.radians = Math.random() * 2 * pi; this.speed = 2 * pi / distance ; } public void movePlanet(int centerX, int centerY) { if (!(this.bounce)) { //Orbit Planet around Sun this.radians += speed; this.x = Math.cos(this.radians) * distance + centerX - size / 2; this.y = Math.sin(this.radians) * distance + centerY - size / 2; } else { //Bounce Planet around Screen this.x += xSpeed; this.y += ySpeed; if ((this.x <= 2) || (this.x + size >= (centerX * 2) - 2)) { xSpeed = -xSpeed; } if ((this.y <= 2) || (this.y + size >= (centerY * 2) - 2)) { ySpeed = -ySpeed; } } } public void testPlanet(int x, int y) { if ((x > this.x) && (y > this.y) && (x < this.x + size) && (y < this.y + size)) { this.bounce = true; xSpeed = Math.random() * 8 + 2; if (Math.random() > .5) { xSpeed = -xSpeed; } ySpeed = Math.random() * 8 + 2; if (Math.random() > .5) { ySpeed = -ySpeed; } } } public void unBouncePlanet() { this.bounce = false; } public void drawOrbit(Graphics g, int centerX, int centerY) { g.setColor(Color.white); g.drawOval(centerX - distance, centerY - distance, distance * 2, distance * 2); } public void drawPlanet(Graphics g) { g.setColor(this.color); g.fillOval((int)(this.x), (int)(this.y), this.size, this.size); } } class motion extends Thread { private solarSystemCanvas ssc; public motion(solarSystemCanvas ssc) { this.ssc = ssc; } public void run() { while(true) { ssc.repaint(); try { sleep(20); } catch(InterruptedException ie) {} } } }