/*By: Arnold Correa
 * Version 1: MiniPaint 
 *
 * Description:
 * **************************************************************
 * This paint program makes certain shapes such as:		*
 * Circles, lines, & squares/rectangules			*
 * **************************************************************/

import java.awt.*;
import java.applet.Applet;
import javax.swing.*;
import java.awt.event.*;

public class myShapes extends JApplet implements ActionListener{
	public myLine line;
	public myRect rect;
	public myOval oval;
	int x = 150;
	int y = 150;
	int x1, x2, y1, y2, xxSize, yySize;
	char lastShapeInfo;
	boolean draw = false;
	boolean ranSuperPaintOnce = false;
	char shapeType = 'i';
	JButton LineButton = new JButton("Line");
	JButton CircleButton = new JButton("Circle");
	JButton SquareRectButton = new JButton("Square/Rectangle");
	JButton EmptyButton = new JButton("Clear Board");
	Container c = getContentPane();

	private static void delay(long msec){ //delayAmount b4 graphics is drawn
		long startPoint = System.currentTimeMillis();
		while (System.currentTimeMillis() - startPoint < msec){}
	}
	public void init(){
		c.setLayout(new FlowLayout());
		c.add(LineButton);
		c.add(CircleButton);
		c.add(SquareRectButton);
		c.add(EmptyButton);
		LineButton.addActionListener(this);
		CircleButton.addActionListener(this);
		SquareRectButton.addActionListener(this);
		EmptyButton.addActionListener(this);
	}

	public void paint(Graphics g){
		if(!ranSuperPaintOnce){
			super.paint(g);
			ranSuperPaintOnce = true;
		}
		g.setColor(Color.red);
		
		switch(shapeType){
		case 'C':
			delay(10); //makes it realtime
			oval.draw(g);
			break;
		case 'L':
			delay(10); //makes it realtime
			line.draw(g);
			break;
		case 'S':
			delay(10); //makes it realtime
			rect.draw(g);
			break;
		case 'E'://clear button
			super.paint(g);
			break;
		}
	}

	
	abstract class myShape{
		protected int xx1, yy1, xx2, yy2, xSize, ySize;
		protected Color color;
		
		//myShape Default Constructor
		myShape(){
			xx1 = 0;
			yy1 = 0;
			xx2 = 0;
			yy2 = 0;
			color = Color.white;
		}		
		//myShape Constructor
		myShape(int x1coord,int y1coord, int x2coord,int y2coord, Color c){
			xx1 = x1coord;
			yy1 = y1coord;
			xx2 = x2coord;
			yy2 = y2coord;
			color = c;
		}	
		//sets X & Y coordinates for Superclass (inherited by subclasses)
		void setX1(int x1){
			xx1 = x1;
		}
		void setX2(int x2){
			xx2 = x2;
		}
		void setY1(int y1){
			yy1 = y1;
		}
		void setY2(int y2){
			yy2 = y2;
		}
		void setColor(Color c){
			color = c;
		}
		void setXSize(int xSize){
			this.xSize = xSize;
		}
		void setYSize(int ySize){
			this.ySize = ySize;
		}

		//returns x & y coordinates for Superclass (inherited by subclasses)
		int getX1(){
			return xx1;
		}
		int getX2(){
			return xx2;
		}
		int getY1(){
			return yy1;
		}
		int getY2(){
			return yy2;
		}
		int getXSize(){
			return xSize;
		}
		int getYSize(){
			return ySize;
		}
		Color getColor(){
			return color;
		}
		public abstract void draw (Graphics g);
	}
	
	class myLine extends myShape{
		
		//myLine Constructor
		public myLine(int xx1,int yy1,int xx2,int yy2, Color c){
			super (xx1,yy1,xx2,yy2,c); //instance var inherited from superclass

		}
		//default Constructor
		public myLine(){}
		public void draw (Graphics g){
			g.setColor(Color.red);//override the color for now
			g.drawLine(xx1,yy1,xx2,yy2);
			System.out.println("X1,Y1: " + xx1 + ", " + yy1);
			System.out.println("X2,Y2: " + xx2 + ", " + yy2);
			System.out.println();
		}
		
	}
	class myRect extends myShape{
		//default Constructor
		public myRect(){}
		//Constructor
		public myRect(int xx1, int yy1, int xSize, int ySize, Color c){
			super (xx1,yy1,xSize,ySize,c);
	}
		public void draw (Graphics g){
			System.out.println("The Rectangle was drawn");
			System.out.println("X1,Y1: " + xx1 + ", " + yy1);
			System.out.println("X2,Y2: " + xx2 + ", " + yy2);
			System.out.println("XSize:" + getSize());
			System.out.println();
			g.setColor(Color.red);
			g.drawRect(xx1,yy1,xSize,ySize);
	}
	}
	class myOval extends myShape{
		//Constructor
		myOval(int xx1, int yy1, int xSize, int ySize, Color c){
			super(xx1,yy1,xSize,ySize,c);
		}
		//default constructor
		myOval(){}
		public void draw (Graphics g){
			System.out.println("The oval was drawn");
			System.out.println("X1,Y1: " + xx1 + ", " + yy1);
			System.out.println("X2,Y2: " + xx2 + ", " + yy2);
			System.out.println("Xsize, Ysize: " + xSize + " " + ySize);
			System.out.println();
			g.setColor(Color.red);
			g.fillOval(xx1,yy1,xSize,ySize);
		}
	}

	public void actionPerformed(ActionEvent thisEvent){
		//Graphics z;
		rect = new myRect();
		oval = new myOval();
		line = new myLine();
		if (thisEvent.getSource() == EmptyButton){
			shapeType = 'E';//chooses to draw ____ shape
			setCursor(new Cursor(Cursor.HAND_CURSOR));
			repaint();
		}
		if (thisEvent.getSource() == SquareRectButton){
			shapeType = 'S';//chooses to draw square/rect shape
			setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
		}
		if (thisEvent.getSource() == LineButton){
			shapeType = 'L'; //chooses to create LINE shape
			setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
		}
		if (thisEvent.getSource() == CircleButton){
			shapeType = 'C'; //chooses to draw CIRCLE shape
			setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
		}
		addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent ev) {
				switch(shapeType){ //sets the X1 & Y1 values			
					case 'E':
						break;
					case 'S':
						rect.setX1((int)ev.getX()); //x1 = ev.getX();
						rect.setY1((int)ev.getY()); //y1 = ev.getY();
						x1 = rect.getX1();
						y1 = rect.getY1();
						break;
					case 'C':
						oval.setX1((int)ev.getX()); //x1 = ev.getX();
						oval.setY1((int)ev.getY()); //y1 = ev.getY();
						x1 = oval.getX1();
						y1 = oval.getY1();						
						break;
					case 'L':
						line.setX1((int)ev.getX()); //x1 = ev.getX();
						line.setY1((int)ev.getY()); //y1 = ev.getY();
						x1 = line.getX1();
						y1 = line.getY1();
						break;
					default:
						break;
				}
				System.out.println("MOUSELISTENER: MOUSEPRESSED at position" + x1 + "&" + " , " + y1);
			}
			public void mouseReleased(MouseEvent ev){
				switch (shapeType){//sets X,Y, xSize & ySize (x & y widths)
					case 'L':
						line.setX2(ev.getX()); //x2 = ev.getX();
						line.setY2(ev.getY()); //y2 = ev.getY();
						x2 = line.getX2();
						y2 = line.getY2();
						xxSize = (int)(line.getX2()-line.getX1());
						yySize = (int)(line.getY2()-line.getY1());
						break;
					case 'C':
						oval.setX2(ev.getX()); //x2 = ev.getX();
						oval.setY2(ev.getY()); //y2 = ev.getY();
						x2 = oval.getX2();
						y2 = oval.getY2();						
						xxSize = (int)(oval.getX2()-oval.getX1());
						yySize = (int)(oval.getY2()-oval.getY1());
						break;
					case 'S':						
						rect.setX2(ev.getX()); //x2 = ev.getX();
						rect.setY2(ev.getY()); //y2 = ev.getY();
						x2 = rect.getX2();
						y2 = rect.getY2();						
						xxSize = (int)(rect.getX2()-rect.getX1());
						yySize = (int)(rect.getY2()-rect.getY1());
						break;
				}
				System.out.println("MOUSELISTENER: MOUSERELEASED at position" 
					+ x2 + " , " + y2);
				
				//CALCULATES AND SETS THE HEIGHT AND WIDTH (SIZE) FOR X & Y
				//int tmpPos;
				System.out.println(xxSize + " THIS IS THE STUFF");
				if (xxSize < 0 && shapeType !='L'){ //my Absolute value fix
					xxSize = xxSize*(-1);
					
				//	tmpPos = x2;
				//	x2 = x1;
				//	x1 = tmpPos;
					x1 = x2;
					oval.setX1(x2);
					rect.setX1(x2);
					System.out.println("SHAPE IS NOT A LINE");
					System.out.println("XX is negative");
				}
				if (yySize < 0 && shapeType !='L'){ //my absolute value fix
					yySize = yySize*-1;
				//	tmpPos = x2;
				//	x2 = x1;
				//	x1 = tmpPos;
					y1 = y2;

					//swap coordinates: it should draw from the 2nd point and expand to bottom
					oval.setY1(y2);
					rect.setY1(y2);
					System.out.println("XXSIZE: " + xxSize + "  YYSIZE: " + yySize);
				}
				oval.setXSize(xxSize);//xxSize = x2 - x1;
				oval.setYSize(yySize);//yySize = y2 - y1;
				line.setXSize(xxSize);//xxSize = x2 - x1;
				line.setYSize(yySize);//yySize = y2 - y1;
				rect.setXSize(xxSize);//xxSize = x2 - x1;
				rect.setYSize(yySize);//yySize = y2 - y1;
				System.out.println("XXSIZE: " + oval.getXSize() + "  YYSIZE: " + oval.getYSize());
				repaint();
			//	System.out.println("MOUSELISTENER: MouseReleased at position" + x2 + " , " + y2);
			}
			});
			addMouseMotionListener(new MouseMotionAdapter() {
				//public void MouseMoved(MouseEvent ev){
				//	System.out.println("Mouse moved to new Position: ");
				//	System.out.println(ev.getX());					
				//}
			//	public void mouseDragged(MouseEvent ev) { //GOOD FOR SHOWING WHAT IT WOULD LOOK LIKE
				//System.out.println("Mouse was dragged");
				//}
			});

	}
}

