/************************************************************************
 * FILENAME : drawing.java
 * 
 ************************************************************************/


import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;


import java.util.*;
import java.awt.image.*;
import java.net.*;
import java.applet.*;
import java.text.*;

public class d4 extends Applet
    implements Runnable, MouseListener, MouseMotionListener {

   // class variables 
   Font F = new Font("TimesRoman", Font.PLAIN, 14);
   Color default_fgcol = Color.blue;
   Color default_fgcol1 = Color.red;
   int Drawing = 0;
   int StartX = 0;
   int StopX = 0;
   int StopY = 0;
   int StartY = 0;
   Thread runner;

   boolean MouseEnter = false;
   boolean MouseRelease = false;
   boolean MouseClick = false;
   boolean MousePressed = false;
   boolean NeedUpdate = false;

   boolean LineMode = false;
   boolean SquareMode = false;
   boolean CircleMode = false;

// internal class functions
   public void mouseReleased(MouseEvent e) {
      int x = e.getX();
      int y = e.getY();

      if ((StartX != 0) && (StartY != 0)) {

      if (LineMode) {
         StopX = x;
         StopY = y;
         NeedUpdate = true;
      }
      if (SquareMode) {
         StopX = x;
         StopY = y;
         NeedUpdate = true;
      }
      if (CircleMode) {
         StopX = x;
         StopY = y;
         NeedUpdate = true;
      }

      MouseRelease = true;
   }}


   public void init() {
      addMouseListener(this);
   }


   public void destroy() {
      removeMouseListener(this);
   }


   public void mousePressed(MouseEvent e) {
      MousePressed = true;
      Dimension d = getSize();
      int x = e.getX();
      int y = e.getY();

      if (((d.height-1)/4) > x) {
         CircleMode = false;
         LineMode = false;
         SquareMode = false;
         StartX = 0;
         StartY = 0;

         if (y < ((d.height-1)/4)) {
            // press top button
            SquareMode = true;
         } else {
            if (y < ((d.height-1)/2)) {
               // press second button
               LineMode = true;
            } else {
               if (y < (((d.height-1)*3)/4)) {
                  // press third button
                  CircleMode = true;
               } else {
                  // press last button
                  // othermode = true;
               }
            }
         }
      } else {
         if (LineMode) {
            StartX = x;
            StartY = y;
         }
         if (CircleMode) {
            StartX = x;
            StartY = y;
         }
         if (SquareMode) {
            StartX = x;
            StartY = y;
         }
      }
   }

   public void mouseClicked(MouseEvent e) {
      int x = e.getX();
      int y = e.getY();

      if (Drawing == 1) {
         StopX = x;
         StopY = y;
         Drawing = 0;
         NeedUpdate = true;
      } else {
         Drawing = 1;
         StartX = x;
         StartY = y;
         StopX = x;
         StopY = y;
      }
      MouseClick = true;
   }


   public void mouseDragged(MouseEvent mouseevent) {

// StopX = mouseevent.getX();
// StopY = mouseevent.getY();

// draw new position
// Grap.setColor(default_fgcol);
// Grap.drawLine(StartX, StartY, StopX, StopY);

      mouseevent.consume();
   }

   public void mouseEntered(MouseEvent e) {
      MouseEnter = true;
      NeedUpdate = true;
   }

   public void mouseExited(MouseEvent e) {
      Drawing = 0;
   }

   public void mouseMoved(MouseEvent mouseevent) {
   }

   public String getAppletInfo() {
      return "Drawing Area";
   }


// aux function used by circle
   public void plotpoints(int x0, int y0, int x, int y, Graphics g) {
      g.drawLine(x0+x,y0+y,x0+x,y0+y);
      g.drawLine(x0+y,y0+x,x0+y,y0+x);
      g.drawLine(x0+y,y0-x,x0+y,y0-x);
      g.drawLine(x0+x,y0-y,x0+x,y0-y);
      g.drawLine(x0-x,y0-y,x0-x,y0-y);
      g.drawLine(x0-y,y0-x,x0-y,y0-x);
      g.drawLine(x0-y,y0+x,x0-y,y0+x);
      g.drawLine(x0-x,y0+y,x0-x,y0+y);
   }

   // Circle is just Bresenham's algorithm for a scan converted circle
   public void circle(int x0, int y0, int r, Graphics g) {
      int x,y;
      float d;

      x = 0;
      y = r;
      d = (5/4)-r;
      plotpoints(x0, y0, x, y, g);

      while (y > x){
         if (d < 0) {
            d = d + (2 * x) + 3;
            x++;
         } else {
            d = d + (2 * (x - y)) + 5;
            x++;
            y--;
         }
         plotpoints(x0, y0, x, y, g);
      }
   }



   public void rectangle(int LeftEdge, int TopEdge, int Width, int Height, Graphics g) {
      g.drawLine(LeftEdge, TopEdge, LeftEdge, TopEdge+Height);
      g.drawLine(LeftEdge, TopEdge, LeftEdge+Width, TopEdge);
      g.drawLine(LeftEdge+Width, TopEdge, LeftEdge+Width, TopEdge+Height);
      g.drawLine(LeftEdge, TopEdge+Height, LeftEdge+Width, TopEdge+Height);
   }


   public void square(int LeftEdge, int TopEdge, int SideLength, Graphics g) {
      rectangle(LeftEdge, TopEdge, SideLength, SideLength, g);
   }


   // Paint is the main part of the program
   public void paint(Graphics g) {
      if (NeedUpdate == true) {
         g.setFont(F);
         g.setColor(default_fgcol);
         // get the size of the drawing area, so we can position the pieces.
         Dimension d = getSize();

         // draw border around the drawing area
         rectangle(0, 0, d.width-1, d.height-1, g);
         // draw activity buttons
         square(0, 0, (d.height-1)/4, g);
         square(0, 1+(d.height-1)/4, (d.height-1)/4, g);
         square(0, 1+(d.height-1)/2, (d.height-1)/4, g);
         square(0, 1+3*(d.height-1)/4, (d.height-1)/4, g);

         // switch color to red
         g.setColor(default_fgcol1);

         // draw the inards of the button
         square((int)(((d.height-1)/4)*0.2), (int)(((d.height-1)/4)*0.2), (int)(((d.height-1)/4)*0.6), g);
         g.drawLine((int)(((d.height-1)/4)*0.2),
                    (int)(((d.height-1)/4)*0.2) + 1 + (int)((d.height-1)/4),
                    (int)(((d.height-1)/4)*0.8), 
                    (int)(((d.height-1)/4)*0.8) + 1 + (int)((d.height-1)/4));
         circle((int)(((d.height-1)/2)*0.5),
                (int)(((d.height-1)/2)*1.0) + 1 + (int)((d.height-1)/4),
                (int)(((d.height-1)/8)*0.3),
                g);

         if (SquareMode) {
            square(StartX, StartY, Math.max(StopX-StartX, StopY-StartY), g);
         }
         if (LineMode) {
            g.drawLine(StartX, StartY, StopX, StopY);
         }
         if (CircleMode) {
            circle(StartX, 
                   StartY, 
                   (int)Math.sqrt(((StopX-StartX) * (StopX-StartX)) + ((StopY-StartY) * (StopY-StartY))), g);
         }
         g.drawString("Painting 6", 200, 270);
         if (MouseEnter == true) {
            g.drawString("MouseEnter", 200, 220);
         }
         if (MousePressed == true) {
            g.drawString("MousePressed", 200, 180);
         }
         if (MouseClick == true) {
            g.drawString("MouseClick", 200, 140);
         }
         if (MouseRelease == true) {
            g.drawString("MouseRelease", 200, 100);
         }
         NeedUpdate = false;
         MouseEnter = false;
         MousePressed = false;
         MouseClick = false;
         MouseRelease = false;
      }
   }

   public void run() {
      for (Thread thread = Thread.currentThread(); runner == thread;) {
         if (NeedUpdate==true) {repaint();}
         try {
            Thread.sleep(25L);
         } catch(InterruptedException _ex) { }
      }
   }

   public void start() {
      if (runner == null) {
         runner = new Thread(this);
         runner.start();
      }
   }

   public void stop() {
      if (runner != null)
         runner = null;
      }
   }
