//Tim Zaharev
// CS396L - Spring 1999
// To run this applet, click on solar.html.
import java.applet.*;
import java.awt.*;
import java.lang.Math;
import java.awt.event.*;
public class Solar extends Applet
{
Canvas canvas;
public planet earth;
public planet mercury;
public planet venus;
public planet mars;
public planet jupiter;
public planet saturn;
public planet uranus;
public planet neptune;
public planet pluto;
Color color;
Button button;
public static boolean reverse = false;
public void init()
{
color = Color.blue;
canvas = new
Canvas();
canvas.setSize(500,300);
canvas.setBackground(Color.black);
add(canvas);
Button button
= new Button("End the world");
add(button);
button.addActionListener(new
EndWorldListener ());
mercury =
new planet(3, 40, 5, canvas, Color.yellow);
venus = new
planet(9, 50, 15, canvas, Color.cyan);
earth = new
planet(10, 70, 20, canvas, Color.blue);
mars = new
planet(4, 80, 40, canvas, Color.red);
jupiter =
new planet(15, 90, 200, canvas, Color.gray);
saturn = new
planet(13, 100, 500, canvas, Color.orange);
uranus = new
planet(11, 115, 1000, canvas, Color.pink);
neptune =
new planet(11, 125, 1500, canvas, Color.gray);
pluto = new
planet(3, 135, 2000, canvas, Color.darkGray);
} // method init
class EndWorldListener implements ActionListener
{
public void
actionPerformed(ActionEvent event)
{
reverse = true;
}
} // class EndWorldListner
}//class Solar
class planet implements Runnable
{
int diameter, distance, period, x,
y;
Graphics g;
Color c;
Thread t;
double angle = 0;
planet(int dm, int d, int p, Canvas
canvas, Color color)
{
t = new Thread(this);
diameter =
dm;
distance =
d;
period = p;
x=0;
y=0;
g = canvas.getGraphics();
c = color;
t.start();
} // constructor
public void run()
{
g.setColor(Color.yellow);
g.fillOval(235,
135, 30, 30);
while(true)
{
x =250 + (int)(Math.cos(angle)* distance *1.7 );
y = 150 - (int)(Math.sin(angle)* distance );
g.setColor(c);
g.fillOval(x, y, diameter, diameter);
try {t.sleep (period ); } catch (InterruptedException e) {}
g.setColor(Color.black);
g.fillOval(x, y, diameter, diameter);
angle = modifyAngle(angle);
}// while(true)
} // method run
public double modifyAngle( double a)
{
if(solar.reverse)
{
g.setColor(Color.black);
g.fillOval(235, 135, 30, 30);
return a = (a - Math.PI/180);
}
return a =
(a + Math.PI/180);
}
} // class planet