//*****************************************************************
//
// title:       Lines
// author:      Chris Lattner
//
//  Silly java applet that draws a bunch of colorful line thingers.
//
//*****************************************************************

import java.applet.*;
import java.awt.*;
import vista.*;

public class Lines extends Applet {

  private VTimer fTimer;  // add the timer so speed can be changed
  private int numPoints = 3;
  private final int maxPoints = 50;
  private int Size;

  private Button PlusButton;
  private Button MinusButton;

  private Dimension WinSize;
  private double angle = 0;
  private Point2D Pts[] = new Point2D[maxPoints];
  private int CenterX;
  private int CenterY;

  private void CalcPoints(int Width, int Height) {
    if (Width > Height)
      Size = Height / 3;
    else
      Size = Width / 3;

    for (int i = 0; i < numPoints; i++) {
        Pts[i] = new Point2D(
          (int) (Size*Math.cos(2*Math.PI / numPoints * i) +
                 Size*Math.sin(2*Math.PI / numPoints * i)),
          (int) (Size*Math.sin(2*Math.PI / numPoints * i) -
                 Size*Math.cos(2*Math.PI / numPoints * i)),
                 Width / 2, Height / 2);
    }
  }

  public void init ()
  {
    PlusButton  = new Button("+");
    MinusButton = new Button("-");
    add(PlusButton);
    add(MinusButton);
    WinSize = this.size();
    CalcPoints(WinSize.width, WinSize.height);

    RotatePts();

    fTimer   = new VTimer(15, this);    // create a new timer
    fTimer.start();                     // start the timer going
  }

  private double radius = 1;

  //
  // Trap each timer event and change movement information.
  public boolean action (Event event, Object arg)
  {
    if (event.target == fTimer) {
        angle += .04;
        radius = Math.cos(angle);
        CenterX = WinSize.width /2 + (int) (Math.sin(angle)*Size/2);
        CenterY = WinSize.height/2 + (int) (Math.cos(angle*3/2)*Size/2);
        RotatePts();
        repaint();
        return true;
    }
    else if (event.target == PlusButton) {
        if (numPoints < maxPoints) {
            numPoints++;
            CalcPoints(WinSize.width, WinSize.height);
        }
        return true;
    }
    else if (event.target == MinusButton) {
        if (numPoints > 1) {
            numPoints--;
            CalcPoints(WinSize.width, WinSize.height);
        }
        return true;
    }
    else
      return false;
  }

  private void RotatePts () {
    for (int i = 0; i < numPoints; ++i)
        Pts[i].RotatePoint(angle, radius);
  }

  // Draw the Border around the window.  Clears the other pixels though.

  public void paint (Graphics g)
  {
    setBackground(Color.black);
    g.draw3DRect(0, 0, WinSize.width-1, WinSize.height-1, false);
    for (int i = 0; i < numPoints; ++i) {
        g.setColor(new Color((int)(Math.random()*255),
                             (int)(Math.random()*255),
                             (int)(Math.random()*255)));

        g.drawLine(Pts[i].sx, Pts[i].sy,
            Pts[((i+1) % numPoints)].sx,
            Pts[((i+1) % numPoints)].sy);

        g.drawLine(Pts[i].sx, Pts[i].sy, CenterX, CenterY);
    }
  }
}
