
import java.awt.*;

/**
 * Represents a "sun" that can change size from radius 25 to
 * radius 75 and back.  The size is determined by a "phase"
 * property.  There are also some rays coming out of the sun.
 * The position of the rays along the edge of the sun change
 * with the phase.  By advancing the phase and redrawing the
 * sun, you can animate the drawing.
 */
public class Pulsar {

   private Color color = Color.YELLOW;
   private int centerX, centerY;
   private int phase;
   
   /**
    *  Set the color of the main part of the sun.  The default
    *  color is yellow.  The rays are always orange.
    *  @param color The new color of the sun.  If this parameter
    *  is null, the color changes back to the default, yellow.
    */
   public void setColor(Color color) {
      if (color == null)
         this.color = Color.YELLOW;
      else
         this.color = color;
   }
   
   public Color getColor() {
      return color;
   }
   
   /**
    * Set the location of the center of the sun.  Initially,
    * the center is at (0,0).
    */
   public void setCenter(int x, int y) {
      centerX = x;
      centerY = y;
   }
   
   /**
    * Return the x-coordinate of the center of the sun.
    */
   public int getCenterX() {
      return centerX;
   }
   
   /**
    * Return the y-coordinate of the center of the sun.
    */
   public int getCenterY() {
      return centerY;
   }
   
   /**
    * Set the phase, which determines the size of the sun.  The
    * size is determined by (phase % 100).  If this value is between
    * 0 and 49, then the size is 25+(phase%100).  If it is between
    * 50 and 99, the size is 100-(phase%100).  The phase must be
    * non-negative.  If the parameter is negative, its absolute
    * value is used.
    */
   public void setPhase(int phase) {
      this.phase = Math.abs(phase);
   }
   
   /**
    * Add one to the current value of phase.
    */
   public void advancePhase() {
      phase++;
   }
      
   /**
    * Get the current value of phase.
    */
   public double getPhase() {
      return phase;
   }
      
   /**
    * Draw the sun in the given graphics context, using the current
    * values of the color, phase, and center properties.
    */
   public void draw(Graphics g) {
      int r = (phase % 100);
      if (r >=50)
         r = 100 - r;
      r = r + 25;
      int angle = 2*(phase % 10);
      g.setColor(new Color(255,130,0));
      for (int i = 0; i < 18; i++) {
         double a = (i*20+angle)*(2*Math.PI/180);
         int x = (int)(100*Math.cos(a));
         int y = (int)(100*Math.sin(a));
         g.drawLine(centerX, centerY, centerX + x, centerY + y);
      }
      for (int i = 0; i < 18; i++) {
         double a = (i*20-angle)*(2*Math.PI/180);
         int x = (int)(90*Math.cos(a));
         int y = (int)(90*Math.sin(a));
         g.drawLine(centerX, centerY, centerX + x, centerY + y);
      }
      g.setColor(color);
      g.fillOval(centerX - r, centerY - r, 2*r, 2*r);
      g.setColor(Color.BLACK);
      g.drawOval(centerX - r, centerY - r, 2*r, 2*r);
   }

}

