//<APPLET CODE="CubeMove.class" WIDTH=400 height=300></APPLET>

import java.applet.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;

public class CubeMove extends Applet {
	ManagePanel mp;
	Cube backC, dragC;
	Image banner, osi;
	Graphics h;
	ExBanner printB;

public void init() {
	setBackground(new Color(130,130,206));
	banner = getImage(getDocumentBase(),"banner.gif");
	printB = new ExBanner(82,16,334-82,102);
	mp = new ManagePanel();
	backC = new Cube(112,164,26*4,26*3,25);
	dragC = new Cube(10,50);
	osi = createImage(this.size().width,this.size().height);
	h = osi.getGraphics();
} // End init


public void paint(Graphics g) {
	h.setColor(new Color(130,130,206));
	h.fillRect(0,0,400,400);

	h.drawImage(banner,80,15,this);
	dragC.show(h);
	backC.show(h);
	printB.show(h);

	mp.show(h);

	h.drawLine(229-13,170-6,126-13,170-6);
	h.drawLine(125-13,170-6,125-13,248-6);
	h.drawLine(229-13,170-6,229-13,248-6);

	g.drawImage(osi,0,0,this);
} // End paint

public void update(Graphics g){
	paint(g);
}

	public boolean mouseUp(Event evt, int x, int y) {
		mp.up(x,y);
		repaint();
		return true;
	}

	public boolean mouseDown(Event evt, int x, int y) {
		System.out.println("X: "+x+" Y: "+y);
		if (x> 10 && x<49)
			if (y> 38&& y <76) {
				mp.addObj(new Cube(10,50));
				printB.next();
			}

		mp.down(x,y);
		repaint();
		return true;
	}

	public boolean mouseDrag(Event evt, int x, int y) {
		mp.drag(x,y);
		repaint();
		return true;
	}
} // End class

//////////////////////////
class Cube extends Box {
	Color side = new Color(209,248,209);

public Cube(int x, int y) {
   super(x,y);
   //this.x = x;
   //this.y = y;
   w=26;
   l=26;
   zl=l/2;
}

public Cube(int x, int y, int w, int l) {
   super(x,y);
   this.w = w;
   this.l = l;
   zl=l/2;
}

public Cube(int x, int y, int w, int l, int zl) {
   super(x,y);
   this.w = w;
   this.l = l;
   this.zl=13;
   side = new Color(180,180,250);
   System.out.println(""+zl);
}

public void show(Graphics g) {
   upCube2(g);
   //g.setColor(new Color(180,180,250));
  // g.fillRect(x,y,w,l);
   g.setColor(side);
   g.fillRect(x,y,w,l);
   g.setColor(Color.black);
   g.drawRect(x,y,w,l);
  
}
public void downCube(Graphics g){
   g.drawRect(x+zl,y+zl,w,l);
   g.drawLine(x,y,x+zl,y+zl);  
   g.drawLine(x+w,y,x+w+zl,y+zl);  
   g.drawLine(x,y+l,x+zl,y+l+zl);  
   g.drawLine(x+w,y+l,x+w+zl,y+l+zl);  
}

public void upCube(Graphics g){
   g.setColor(Color.black);
   g.drawLine(x+zl,y-zl,x+w+zl,y-zl);
   g.drawLine(x+w+zl,y+l-zl,x+w+zl,y-zl);
   g.drawLine(x,y,x+zl,y-zl);  
   g.drawLine(x+w,y,x+w+zl,y-zl);  
   g.drawLine(x+w,y+l,x+w+zl,y+l-zl);  
}

public void upCube2(Graphics g){
   g.setColor(side);
   Polygon pete = new Polygon();
   pete.addPoint(x,y);
   pete.addPoint(x+zl,y-zl);
   pete.addPoint(x+w+zl,y-zl);
   pete.addPoint(x+w,y);
   g.fillPolygon(pete);

   Polygon pam = new Polygon();
   pam.addPoint(x+w,y);
   pam.addPoint(x+w,y+l);
   pam.addPoint(x+w+zl,y+l-zl);
   pam.addPoint(x+w+zl,y-zl);
   g.fillPolygon(pam);

   g.setColor(Color.black);
   g.drawLine(x+zl,y-zl,x+w+zl,y-zl);
   g.drawLine(x+w+zl,y+l-zl,x+w+zl,y-zl);
   g.drawLine(x,y,x+zl,y-zl);  
   g.drawLine(x+w,y,x+w+zl,y-zl);  
   g.drawLine(x+w,y+l,x+w+zl,y+l-zl);  

}

}//End Class


////////////////////////
class Box {
	int x, y, pointOnImageX,pointOnImageY;
	boolean select;
   	int w, l,zl;

	public Box(int x, int y,int pointOnImageX, int pointOnImageY) {
		this.x = x;
		this.y = y;
		this.pointOnImageX = x-pointOnImageX;
		this.pointOnImageY = y-pointOnImageY;
		select = true;
	}

	public Box(int x, int y) {
		this.x = x;
		this.y = y;
		this.pointOnImageX = x;
		this.pointOnImageY = y;
		select = true;
	}

	public int getX() {
		return x;
	}

	public int getY() {
		return y;
	}
	
	public void setX(int x) {
		this.x = x;
	}

	public void setY(int y) {
		this.y = y;
	}

	public void show (Graphics g) {

	}
	
	public boolean contact(int x1, int y1) {
		if (x1 >= x && x1 <= x+w+zl) {
			if (y1 >= y-zl && y1 <=y+l) {
				return true;
			}
		}
		select = false;
		return false;
	}

	public void deselect() {
		setX( ( (getX()) /26) *26+8);
		setY( ( (getY()) /26) *26+8);
		select = false;
	}

	public void select(int pointOnImageX1,int pointOnImageY1) {
		pointOnImageX = x-pointOnImageX1;
		pointOnImageY = y-pointOnImageY1;
		select = true;
	}

   	public void objectDrag(int x, int y) {
        if (select) {
		this.x=(x+pointOnImageX);
		this.y=(y+pointOnImageY);
	  }
   	}
} // End class
/////////////////////////////////
class ManagePanel {
	Vector objVect;

	public ManagePanel() {
		objVect = new Vector();
	}

	public void addObj(Object x) {
		objVect.addElement(x);
	}

	public void show(Graphics g) {
		sort();

		for (int i=0;i<objVect.size();i++) {
			Cube c = (Cube) objVect.elementAt(i);
			c.show(g);
		}
	}

	public void sort() {
		Cube a [];
		a = new Cube[objVect.size()];
		
		for (int i=0;i<objVect.size();i++) {
			Cube c = (Cube) objVect.elementAt(i);
			a[i]=c;
		}
		for (int i=0;i<objVect.size();i++) {
			for (int j=i;j<objVect.size();j++) {
				Cube temp;
				if (a[i].getX() > a[j].getX()) {
					temp = a[i];
					a[i]=a[j];
					a[j]=temp;
				}
				if (a[i].getY() < a[j].getY()) {
					temp = a[i];
					a[i]=a[j];
					a[j]=temp;
				}	
			}
		}
		int max = objVect.size();
		objVect = new Vector();
		for (int i=0;i<max;i++){
			objVect.addElement(a[i]);
		}

	} // End sort

	public void up(int x, int y) {
		for (int i=0;i<objVect.size();i++) {
	   		Cube c = (Cube) objVect.elementAt(i);
			c.deselect();
		}
	}


	public void down(int x, int y) {
		for (int i=0;i<objVect.size();i++) {
			Cube c = (Cube) objVect.elementAt(i);
			 if (c.contact(x,y))
				c.select(x,y);
		}
	}

	public void drag(int x, int y) {
		for (int i=0;i<objVect.size();i++) {
			Cube c = (Cube)objVect.elementAt(i);		
	   		c.objectDrag(x,y);
		}
	}
} // End ManagePanel
//////////////////////////////////
class ExBanner {
	String s[];
	int max = 4,
	    current,
	    x,y,w,l;
	Font f;

public ExBanner(int x, int y, int w, int l) {
	f = new Font("Helvetica",Font.BOLD,24);
	s = new String[max];

	s[0] = "Click and drag the cube on the left.";
	s[1] = "Now drag the cube into the rectangle below.";
	s[2] = "Continue filling the retangle with other cubes.";
	s[3] = "How few cubes will it take to fill the rectangle?";
	current = 0;
	this.x = x;
	this.y = y;
	this.w = w;
	this.l = l;
}

public void next(){
	if (current < max-1)
		current++;
}

public void show (Graphics g) {
	g.setFont(f);
	g.setColor(Color.black);

	FontMetrics fm = Toolkit.getDefaultToolkit().getFontMetrics(f);
	int heig = fm.getHeight();
	int count = 1;
	if (current < max) {
		if (fm.stringWidth(s[current])+10>x+w) {
			// Cut string
			String temp = "", temp2="";
			StringTokenizer token = new StringTokenizer(s[current]," ");
			int count2 = 0;
			int maxi = token.countTokens();

			for (int i=0;i < maxi;i++) {
				temp += temp2;
				if ((fm.stringWidth(temp2) + fm.stringWidth(temp)+10) > w-20) {
					g.drawString(temp,x+10,y+heig*(count2+1));
					temp = "";
					count2++;
				}
				temp2 = token.nextToken()+" ";
			}
			temp += temp2;
			g.drawString(temp,x+10,y+heig*(count2+1));

		} else {
			g.drawString(s[current],x+10,y+10+heig);
		}
	}
}

} // End class