/* unitCircleDemo - demo the unitCircle class Display the screen, just like Rubber.class, and allow user to click and drag to define the area of a new unit circle...then instanciate that bad boy, and so we get a screen full of unit circles. */ import java.awt.*; import java.applet.*; import java.util.*; public class unitCircleThread extends Applet { unitCircle uc; Vector ucList = new Vector(); Label xy = new Label(); TextField ta = new TextField(); Label prompt = new Label(); boolean drawing = false; int newX, newY, newWidth, newHeight = 0; int startX,startY = 0; public boolean mouseMove(Event evt, int x, int y){ xy.setText("Mouse at: "+x+", "+y); xy.reshape(5,5,150,15); return true; } public boolean mouseDown(Event evt, int x, int y){ System.out.println("mouse down id="+evt.id); drawing=true; newX=x; newY=y; startX = newX; startY = newY; return true; } public boolean mouseDrag(Event evt, int x, int y){ newX=startX; newY=startY; newWidth=x-newX; newHeight=y-newY; xy.setText("Mouse at: "+x+", "+y); //allow the initial point to be any corner of the rubber banded rectangle if (newWidth < 0){ newX += newWidth; newWidth=Math.abs(newWidth); } if (newHeight < 0){ newY += newHeight; newHeight=Math.abs(newHeight); } repaint(); return true; } public boolean mouseUp(Event evt, int x, int y){ newX=startX; newY=startY; newWidth=x-newX; newHeight=y-newY; xy.setText("Mouse at: "+x+", "+y); //allow the initial point to be any corner of the rubber banded rectangle if (newWidth < 0){ newX += newWidth; newWidth=Math.abs(newWidth); } if (newHeight < 0){ newY += newHeight; newHeight=Math.abs(newHeight); } int k = Integer.parseInt(ta.getText()); addUnitCircle(newX,newY,newWidth,newHeight,k); drawing=false; repaint(); return true; } public void addUnitCircle(int x, int y, int width, int height, int delay){ unitCircle uc = new unitCircle(x,y,width,height, delay); add(uc); uc.start(); // uc.reshape(x,y,width,height); ucList.addElement(uc); } public void paint(Graphics g){ g.setColor(Color.black); if (drawing){ g.setColor(Color.blue); g.drawRect(newX,newY,newWidth,newHeight); } //draw ucList /*This is no longer needed-because the components are now properly added, and are drawing themselves in relationship to their own personal sense of zero. g.setColor(Color.black); Enumeration e = ucList.elements(); while (e.hasMoreElements()){ unitCircle uc = (unitCircle) e.nextElement(); uc.paint(g); } */ } //paint public void init() { super.init(); add(xy); //adds Label xy to this container. setLayout(null); //don't use an existing layout manager resize(500,300); //resize the applet window xy.reshape(5,5,150,15); //resize the xy label setBackground(Color.lightGray); add(ta); ta.reshape(175,20,100,20); ta.setText("500"); add(prompt); prompt.setText("Circle delay (milliseconds): "); prompt.reshape(5,20,150,20); //add some unit Circles addUnitCircle(5,50,100,100,15); addUnitCircle(160,50,100,100,125); addUnitCircle(5,160,100,100,250); addUnitCircle(160,160,100,100,500); } //init }