//
import java.awt.*;

public class Example3 extends BufferedApplet
{
   int width = 0, height = 0;
   int click = 0;
   int colorState = 0;

   public void render(Graphics g) {
      if (width == 0) {
         width  = bounds().width;
         height = bounds().height;
      }

      // DRAW A CLEAR WHITE BACKGROUND

      g.setColor(Color.white);
      g.fillRect(0, 0, width, height);

      int x = (int)(width / 2 + width / 4 * Math.cos(0.1 * click));
      int y = (int)(height/ 2 - width / 4 * Math.sin(0.1 * click));

      // SET THE COLOR BASED ON COLOR STATE

      g.setColor(colorState == 0 ? Color.red : Color.green);

      // DRAW A DIAMOND SHAPE AT x,y

      drawDiamond(g, x, y);

      // ADVANCE TIME

      click++;
      animating = true;
   }

   public boolean mouseUp(Event e, int x, int y) {
      colorState = 1 - colorState;
      return true;
   }

   // SUPPORT CODE FOR DRAWING A DIAMOND SHAPE

   void drawDiamond(Graphics g, int x, int y) {
      for (int i = 0 ; i < 4 ; i++) {
         X[i] = x + diamondX[i];
         Y[i] = y + diamondY[i];
      }
      g.fillPolygon(X, Y, 4);
   }

   int diamondX[] = {0,20,0,-20};
   int diamondY[] = {20,0,-20,0};
   int X[] = new int[20];
   int Y[] = new int[20];
}