import objectdraw.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;

// A Satellite that revolves around its center point.
public class Satellite extends ActiveObject implements ChangeListener {

    // size and speed of the satellite
    private static final int SIZE = 10;
    private static final double SPEED = 3.14/50.0;
    private static final int PAUSE = 20;
    
    private double radius;   // radius of the path to travel
    private double theta;    // angle to the satellite
    private Location center; // center of the path
    
    private FilledOval ball;      // the satellite
    private JSlider radiusSlider; // slider to adjust radius of orbit
    
    // draw the satellite, then begin orbiting
    public Satellite(Location theCenter,
                     JSlider theSlider, 
                     DrawingCanvas canvas) {
        center = theCenter;
        ball = new FilledOval(center.getX() + radius, center.getY(), SIZE, SIZE, canvas);
        ball.setColor(Color.blue);
        theta = 0;
        radius = theSlider.getValue();
        radiusSlider = theSlider;
        radiusSlider.addChangeListener(this);
        start();
    }
    
    // move the satellite in a circle around the center point
    public void run() {
        while (true) {
            double vx = center.getX() + Math.cos(theta) * radius;
            double vy = center.getY() + Math.sin(theta) * radius;
            ball.moveTo(vx - SIZE / 2, vy - SIZE / 2);
            theta = theta + SPEED;
            pause(PAUSE);
        }
    }
    
    // called when the slider is moved
    public void stateChanged(ChangeEvent event) {
        setRadius(radiusSlider.getValue());
    }
    
    // adjust the radius
    private void setRadius(int r) {
        radius = r;
    }
    
}
