/* sky.java Rich Gibson's attack on the skyline problem. */ import java.awt.*; import java.awt.Graphics; import java.applet.*; import java.util.*; //StringTokenizer lives in here public class sky extends Applet implements Runnable{ //This is the flag that tells start and stop if we ever, well, started or stopped Thread runner = null; int waitState = 100; //how long to wait during the endless event loop skyline ourCity = new skyline(); Choice foo = new Choice(); Label label1 = new Label(); Label label2 = new Label(); Label move1 = new Label(); Label move2 = new Label(); Label xy = new Label(); Button deleteLast = new Button();; Button makeSkyline = new Button();; int screenHeight=400; int screenWidth=500; int bottomOffset=100; //where controls start int groundLevel=110; //where the ground is, really screenHeight-groundLevel int showSkyline = 0; //skyline flag //variables for adding a building boolean addingBuilding = false; Color addingBuildingColor = Color.red; int newX,newY,newWidth,newHeight = 0; int startX,startY = 0; public void start(){ //This is the start of the thread...it gets called once each time a //thread is started. int L, H, R = 0; if (runner == null) { //we haven't been called before, start thread and load parameters runner = new Thread(this); runner.start(); String test = getParameter("bldgIn"); System.out.println("Skyline start"); StringTokenizer bldgSt = new StringTokenizer(getParameter("bldgIn"),","); while (bldgSt.hasMoreTokens()){ L=Integer.parseInt((String) bldgSt.nextElement()); H=Integer.parseInt((String) bldgSt.nextElement()); R=Integer.parseInt((String) bldgSt.nextElement()); // System.out.println("xyz = "+x+" "+y+" "+" "+z); ourCity.addBuilding(L,H,R); } //reading in parameters //bldgInput=new Choice(); //ourCity.updateInput(bldgInput,foo); //ourCity.updateResults(bldgResults); System.out.println("All params read"); repaint(); } //if runner==null } //start public void stop(){ if (runner !=null){ //we _were_ started, so stop that nonesense runner.stop(); runner=null; } //runner !=null } //stop public void run(){ } public boolean mouseUp(Event evt, int x, int y){ addingBuilding=false; //System.out.println("Mouse up ...Event.target="+evt.target); //convert from screen coordinates to funky coords. newX=startX; newY=startY; newWidth=x-newX; newHeight=y-newY; //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 L = (newX)/10; int H = (y-newY)/10; int R = (newX+newWidth)/10; ourCity.addBuilding(L,H,R); move1.setText(""); move2.setText(""); move1.reshape(0,0,0,0); move2.reshape(0,0,0,0); repaint(); return true; } public boolean mouseDown(Event evt, int x, int y){ addingBuilding=true; //System.out.println("Mouse Down ...Event.target="+evt.target); newX=x; newY=y; startX = x; startY = y; move1.setText("Adding Building, Start: "+newX+","+newY); move1.reshape(175,screenHeight-bottomOffset+25, 200,15); return true; } 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 mouseClick(Event evt, int x, int y){ //System.out.println("Mouse click ...Event.target="+evt.target); return true; } public boolean mouseDrag(Event evt, int x, int y){ //System.out.println("Mouse drag ...Event.target="+evt.target); newX=startX; newY=startY; newWidth=x-newX; newHeight=y-newY; //allow the initial point to be any corner of the rubber banded rectangle if (newWidth < 0){ newX += newWidth; newWidth=Math.abs(newWidth); } if (newHeight < startY){ newY += newHeight; newHeight=Math.abs(newHeight); } //the translation functions...really should be in building... int L = (newX)/10; int H = (y-newY)/10; int R = (newX+newWidth)/10; xy.setText("Mouse at: "+x+", "+y); xy.reshape(5,5,150,15); move2.setText("Pixel width "+newWidth+" Pixel height "+newHeight+" L "+L+" H "+H+" R "+R); move2.reshape(175,screenHeight-bottomOffset+35, 275,25); repaint(); //this is counting on paint() rather than a graphics context under our control return true; } public void init() { super.init(); setLayout(null); resize(screenWidth,screenHeight); add(ourCity.bldgInput); add(ourCity.bldgResults); add(label1); add(label2); add(deleteLast); add(makeSkyline); add(move1); add(move2); add(xy); label1.setText("Input Data"); label2.setText("Skyline Points"); deleteLast.setLabel("Boom! (Urban Renewal)"); makeSkyline.setLabel("Show Skyline"); label1.setBackground(Color.white); label2.setBackground(Color.white); move1.setBackground(Color.white); move1.setBackground(Color.white); deleteLast.setBackground(Color.red); makeSkyline.setBackground(Color.white); xy.setBackground(Color.white); ourCity.bldgInput.setBackground(Color.white); ourCity.bldgResults.setBackground(Color.white); ourCity.bldgInput.reshape(100,screenHeight-bottomOffset,140,25); ourCity.bldgResults.reshape(340,screenHeight-bottomOffset,140,25); label1.reshape(10, screenHeight-bottomOffset,80,25); //initial x,y,width, height label2.reshape(250, screenHeight-bottomOffset,80,25); deleteLast.reshape(10,screenHeight-bottomOffset+25,150,25); makeSkyline.reshape(10,screenHeight-bottomOffset+55,150,25); label1.setAlignment(Label.RIGHT); label2.setAlignment(Label.RIGHT); move1.setAlignment(Label.LEFT); move2.setAlignment(Label.LEFT); setBackground(Color.white); } //init public void paint(Graphics g){ //draw the buildings - for now this is ugly (and doesn't do what the //assignment asks for, that will change // //this draws a red border around the applet g.setColor(Color.red); g.drawRect(0,0,screenWidth-5,screenHeight-5); g.setColor(Color.blue); if (showSkyline == 1){ g.drawPolygon(ourCity.makeSkyline(screenWidth, screenHeight, groundLevel)); } else { g.drawPolygon(ourCity.makeSkylineBox(screenWidth, screenHeight, groundLevel)); } //g.fillPolygon(ourCity.makeSkyline(screenWidth, screenHeight, groundLevel)); //are we in the midst of creating a new rectangle? if (addingBuilding) { g.setColor(addingBuildingColor); g.drawRect(newX,newY,newWidth,newHeight); } } public boolean handleEvent(Event event) { boolean myFlag = false; if (event.id == Event.ACTION_EVENT && event.target == deleteLast){ //time for urban renewal! // System.out.println("Handle Event Before Urban renewal..."); ourCity.urbanRenewal(); myFlag = true; // System.out.println("After Urban renewal..."); repaint(); } if (event.id == Event.ACTION_EVENT && event.target == makeSkyline){ showSkyline=1-showSkyline; if (showSkyline==0){ makeSkyline.setLabel("Show Skyline"); } else { makeSkyline.setLabel("Show Boxes"); } repaint(); } if (myFlag == false) { return super.handleEvent(event); } else { return myFlag; } } //{{DECLARE_CONTROLS //}} }