import java.awt.*; import java.applet.*; /** Runnable applet, shows a solar system v. 1.0 uses circular orbits Lin Jensen 7 June 2005 Uses a class Planet to hold planetary properties and draw the planet run() sleeps for a time interval and then repaints, paint() calls on all the Planets to update their position draw themselves. We will have to decide how often to repaint, and how much motion will occur each time. Earth will orbit in 4 seconds. repaint 30 times a second avaids jerkiness, like the movies */ public class Solar extends Applet implements Runnable { public static int half = 150; // assume applet is 300 x 300 private static int stepTime = 33; // 1/30 second public static double earthRotation = 3.0; // how long for one earth rotiation public static double step = 2.0*Math.PI*stepTime/1000.0/earthRotation; //angular increment for earth in each step // earth revolves in 20 steps or 1 second // actually it takes longer, because paint takes significant time private Planet sun = new Planet(0.0, Color.yellow, 18); private Planet mercury = new Planet(0.25, Color.darkGray, 4); private Planet venus = new Planet(0.5, Color.white, 10); private Planet earth = new Planet(1.0, Color.blue.brighter(), 10); private Planet mars = new Planet(1.6, Color.red, 8); public void start() /** This is important, we start a thread, it will call run() */ { new Thread(this).start(); } public void run() { while (true) // infinite loop { try { Thread.sleep(stepTime); // repaint 20 times per second } catch (InterruptedException e){} repaint(); } } public void paint(Graphics g) { setBackground(Color.black); sun.draw(g); venus.draw(g); mercury.draw(g); earth.draw(g); mars.draw(g); } } // end Solar