//--Kimberly Bansal //--Assignment 3 //--CIS 525 //--November 13, 2000 import java.applet.*; import java.awt.*; /** ShapeApplet Class is a subclass of default Applet Class. * * @author Kimberly Bansal * */ public class ShapeApplet extends Applet { private Choice colorChoice; private Choice shapeChoice; private Choice fillChoice; Graphics gfx; Rectangle rectwind=new Rectangle (290,245,40,30); Button btn; protected Color shapeclr; //mouse coordinates private int x1=0; private int y1=0; private int x2=0; private int y2=0; //------------------------------------------------------------- public void init() { resize(400,285); gfx=getGraphics(); setBackground(Color.white); gfx.drawRect(rectwind.x,rectwind.y,rectwind.width,rectwind.height); setLayout(null); //set up the color choices in the pane colorChoice = new Choice(); colorChoice.addItem("Red"); colorChoice.addItem("Green"); colorChoice.addItem("Black"); colorChoice.addItem("Yellow"); colorChoice.addItem("Blue"); colorChoice.addItem("Cyan"); colorChoice.addItem("Magenta"); colorChoice.addItem("Orange"); colorChoice.reshape(2, 2, 150,150); //Adds all the color choices to the pane add(colorChoice); //Set up the shape choices in the pane shapeChoice = new Choice(); shapeChoice.addItem("Rectangle"); shapeChoice.addItem("Round Rectangle"); shapeChoice.addItem("Oval"); shapeChoice.addItem("Line"); shapeChoice.reshape(2, 32, 150,150); //Adds all the shape choices to the pane add(shapeChoice); //set up the fill choices in the pane fillChoice = new Choice(); fillChoice.addItem("Filled"); fillChoice.addItem("Hollow"); fillChoice.reshape(2, 62, 150,150); //adds all the fill choices to the pane add(fillChoice); //set up the "clear" button in pane: btn = new Button("Clear"); btn.reshape(2, 92, 150, 30); add (btn); } //---------------------------------------------------------- /** Clear Button Event Creation */ public boolean action(Event e_button, Object arg) { if (e_button.target == btn) { repaint(); return true; } else return false; } //------------------------------------------------------------- /** Mouse Down Event */ public boolean mouseDown(Event e, int x, int y) { x1 = x; y1 = y; return true; } /** Mouse Up Function picks up the user selected options and draws the requested shape */ public boolean mouseUp(Event e, int x, int y) { x2 = x; y2 = y; String fillString = fillChoice.getSelectedItem(); String colorString = colorChoice.getSelectedItem(); if (colorString.equals("Red")) shapeclr = Color.red; else if (colorString.equals("Green")) shapeclr = Color.green; else if (colorString.equals("Black")) shapeclr = Color.black; else if (colorString.equals("Yellow")) shapeclr = Color.yellow; else if (colorString.equals("Cyan")) shapeclr = Color.cyan; else if (colorString.equals("Magenta")) shapeclr = Color.magenta; else if (colorString.equals("Orange")) shapeclr = Color.orange; else shapeclr = Color.blue; gfx.setColor(shapeclr); int width = Math.abs(x2-x1); int height = Math.abs(y2-y1); int topx; int topy; if (x2 > x1) topx = x1; else topx = x2; if (y2 > y1) topy = y1; else topy = y2; String shapeString = shapeChoice.getSelectedItem(); if (shapeString.equals("Rectangle")) { gfx.drawRect(topx, topy, width, height); if (fillString.equals("Filled")) gfx.fillRect(topx, topy, width, height); } else if (shapeString.equals("Round Rectangle")) { gfx.drawRoundRect(topx, topy, width, height, 20, 20); if (fillString.equals("Filled")) gfx.fillRoundRect(topx, topy, width, height, 20, 20); } else if (shapeString.equals("Oval")) { gfx.drawOval(topx, topy, width, height); if (fillString.equals("Filled")) gfx.fillOval(topx, topy, width, height); } else if (shapeString.equals("Line")) { gfx.drawLine(x1, y1, x2, y2); } return true; } //------------------------------------------------------------------- public void paint (Graphics gfx) { showStatus("Shape Applet: running"); gfx.setColor(Color.red); } }