import java.applet.*; import java.awt.*; public class InOrbit extends Applet implements Runnable { // Double-buffering stuff. private Dimension appletSize; private Image offscreenBuffer; public Graphics gOff, g; // Reference of what size it should be public static int width = 600; public static int height = 600; // Some variables for the earth private int earthDistance = 270; private double earthAngVelocity = Math.PI/720; private double earthPosition = 0.0; // Set up space for the person icon private Image sun, earth; // Set up space for the thread that will run to move the car private Thread timeLoop; public void init() { // Set up the double buffering. g = getGraphics(); appletSize = getSize(); offscreenBuffer = createImage(appletSize.width, appletSize.height); gOff = offscreenBuffer.getGraphics(); // Get the pictures sun = getImage(getDocumentBase(), "img_sun.gif"); earth = getImage(getDocumentBase(), "img_earth.gif"); // Start up the thread timeLoop = new Thread(this); timeLoop.start(); } public void paint(Graphics g) { g.drawImage(offscreenBuffer, 0, 0, null); } public void run() { // Keep thread looping while (true) { // Clear our display area gOff.setColor(Color.white); gOff.fillRect(0, 0, InOrbit.width, InOrbit.height+100); // Draw the pretty sun gOff.drawImage(sun, InOrbit.width/2-120, InOrbit.height/2-120, null); // Calculate the earth's coordinate position double earthX = earthDistance * Math.sin(earthPosition); double earthY = earthDistance * Math.cos(earthPosition); // Draw earth (-25 for image offset) gOff.drawImage(earth, (int)(InOrbit.width/2+earthX-25), (int)(InOrbit.height/2+earthY-25), null); // Move the earth earthPosition+=earthAngVelocity; if (earthPosition > 2*Math.PI) earthPosition-=2*Math.PI; // Paint the world paint(g); // Sleep try {timeLoop.sleep(10);} catch (InterruptedException e) {} } } // We don't need the graphics resources anymore. public void stop() { g.dispose(); gOff.dispose(); } }