import java.awt.*; import java.awt.event.*; import java.awt.image.*; import java.net.*; import java.applet.*; /** Applet to allow user to draw circle,square,rectangle,oval,arc,freehand or Change Color * * @author Larry Healy CIS 525 Assignment 3 * @return returns to the html * @version Java 1.1.4 * */ public class LetsDrawShapes extends Applet { // Initialize global variables // // myDimensions - hold dimensions for the shapes // w - used for sizing // shapeType, colorStatus - track the current shape and color // // int[] myDimensions = new int[6]; int w = 0; String shapeType = "rect"; int colorStatus = 1; /** Intialize applet, Includes MouseListener(change colorStatus), MouseMotionListener(DrawYourOwn) * Intialize shape dimensions and draw buttons via addMyButton * * @param None * @return None * */ public void init() { // Setup mouse listener's // MouseListener theListener = new MouseAdapter() { public void mousePressed(MouseEvent event) { colorStatus++; if (colorStatus == 14) colorStatus = 1; if (shapeType != "own") repaint(); } }; MouseMotionListener theListener_1 = new MouseMotionAdapter() { public void mouseDragged(MouseEvent event) { if (shapeType == "own") { getGraphics().fillRect(event.getX()-25, event.getY()-25, 30,20); } } }; addMouseListener(theListener); addMouseMotionListener(theListener_1); // set intial dimensions for default // myDimensions[0] = 20; myDimensions[1] = 60; myDimensions[2] = 40; myDimensions[3] = 80; myDimensions[4] = 25; myDimensions[5] = 100; // add the buttons to the display and associate with proper action class // addMyButton(); } /** Select the color depending upon the color status dictated by the mouse click * * @param g Graphics - the current graphic object in display * @return color is set for curent object * */ public void selectMyColor(Graphics g) { switch(colorStatus) { case 1: g.setColor(Color.black); break; case 2: g.setColor(Color.blue); break; case 3: g.setColor(Color.cyan); break; case 4: g.setColor(Color.darkGray); break; case 5: g.setColor(Color.gray); break; case 6: g.setColor(Color.green); break; case 7: g.setColor(Color.lightGray); break; case 8: g.setColor(Color.magenta); break; case 9: g.setColor(Color.orange); break; case 10: g.setColor(Color.pink); break; case 11: g.setColor(Color.red); break; case 12: g.setColor(Color.yellow); break; } } /** Paint the object and fill it as defined by button actions and repaint method * * @param g Graphics - the current graphic object in display * @return selected graphic is drawn with color is set for curent object by button action * set to the default size * */ public void paint (Graphics g) { setBackground(Color.white); selectMyColor(g); if (shapeType == "rect") { g.drawRect(myDimensions[0],myDimensions[1],myDimensions[2] + w,myDimensions[3]+ w); // colorstatus = 13 is No fill for object if (colorStatus != 13) g.fillRect (myDimensions[0],myDimensions[1],myDimensions[2] + w,myDimensions[3]+ w); } if (shapeType == "square") { g.drawRect(myDimensions[0],myDimensions[1],myDimensions[2] + w,myDimensions[3]+ w); // colorstatus = 13 is No fill for object if (colorStatus != 13) g.fillRect (myDimensions[0],myDimensions[1],myDimensions[2] + w,myDimensions[3]+ w); } if (shapeType == "circle") { g.drawOval(myDimensions[0],myDimensions[1],myDimensions[2] + w,myDimensions[3]+ w); // colorstatus = 13 is No fill for object if (colorStatus != 13) g.fillOval (myDimensions[0],myDimensions[1],myDimensions[2] + w,myDimensions[3]+ w); } if (shapeType == "arc") { g.drawArc(myDimensions[0],myDimensions[1],myDimensions[2] + w,myDimensions[3]+ w,myDimensions[4],myDimensions[5]); // colorstatus = 13 is No fill for object if (colorStatus != 13) g.fillArc (myDimensions[0],myDimensions[1],myDimensions[2] + w,myDimensions[3]+ w,myDimensions[4],myDimensions[5]); } if (shapeType == "oval") { g.drawOval(myDimensions[0],myDimensions[1],myDimensions[2] + w,myDimensions[3]+ w); // colorstatus = 13 is No fill for object if (colorStatus != 13) g.fillOval (myDimensions[0],myDimensions[1],myDimensions[2] + w,myDimensions[3]+ w); } } /** Add the action buttons - 8 Buttons * * @param None * @return buttons are displayed that are functional and waiting to be pressed and processed by actionListener * */ // Buttons // Draw Rectangle // Draw Circle // Draw Oval // Draw Square // Draw Arc // Draw Increase the size of a shape // Draw Decrease the size of a shape // Draw Your own shape and play // public void addMyButton () { // Ignore the default layout manager setLayout(null); // Create button instances Button b1 = new Button("Increase Size"); Button b2 = new Button("Decrease Size"); Button b3 = new Button("Circle"); Button b4 = new Button("Square"); Button b5 = new Button("Rectangle"); Button b6 = new Button("Arc"); Button b7 = new Button("Oval"); Button b8 = new Button("Draw Your Own"); // Set Size the buttons b1.setSize(100,40); b2.setSize(100,40); b3.setSize(100,40); b4.setSize(100,40); b5.setSize(100,40); b6.setSize(100,40); b7.setSize(100,40); b8.setSize(100,40); // Set Locate the buttons on screen b1.setLocation(10,5); b2.setLocation(110,5); b3.setLocation(210,5); b4.setLocation(310,5); b5.setLocation(410,5); b6.setLocation(510,5); b7.setLocation(610,5); b8.setLocation(710,5); // Intialize objects IncreaseSize increaseShapeSize = new IncreaseSize(); DecreaseSize decreaseShapeSize = new DecreaseSize(); DrawMyCircle circleShape = new DrawMyCircle(); DrawMySquare squareShape = new DrawMySquare(); DrawMyRect rectShape = new DrawMyRect(); DrawMyArc arcShape = new DrawMyArc(); DrawMyOval ovalShape = new DrawMyOval(); DrawMyOwn ownShape = new DrawMyOwn(); // Add action listeneres for each button b1.addActionListener(increaseShapeSize); b2.addActionListener(decreaseShapeSize); b3.addActionListener(circleShape); b4.addActionListener(squareShape); b5.addActionListener(rectShape); b6.addActionListener(arcShape); b7.addActionListener(ovalShape); b8.addActionListener(ownShape); // Add buttons to container add(b1); add(b2); add(b3); add(b4); add(b5); add(b6); add(b7); add(b8); } /** Increase size of current object * * @author Larry Healy CIS 525 Assignment 3 * @param event Action depress button * @return Action performed * @version Java 1.1.4 * */ // Please note inner classes are not supported by javadoc 1.1 so the public classes listed below // will not be in the javadoc printout, this is per Sun Javadoc documentation public class IncreaseSize implements ActionListener { public void actionPerformed(ActionEvent event) { w = w + 20; if (shapeType != "own") repaint(); } } /** Decrease size of current object * * @author Larry Healy CIS 525 Assignment 3 * @param Action depress button * @return Action performed * @version Java 1.1.4 * */ public class DecreaseSize implements ActionListener { public void actionPerformed(ActionEvent event) { w = w - 20; if (shapeType != "own") repaint(); } } /** Clear the screen and Draw a Circle * * @author Larry Healy CIS 525 Assignment 3 * @param Action depress button * @return Action performed * @version Java 1.1.4 * */ public class DrawMyCircle implements ActionListener { public void actionPerformed(ActionEvent event) { myDimensions[0] = 20; myDimensions[1] = 60; myDimensions[2] = 20; myDimensions[3] = 20; w = 0; shapeType = "circle"; repaint(); } } /** Clear the screen and Draw a Square * * @author Larry Healy CIS 525 Assignment 3 * @param Action depress button * @return Action performed * @version Java 1.1.4 * */ public class DrawMySquare implements ActionListener { public void actionPerformed(ActionEvent event) { myDimensions[0] = 20; myDimensions[1] = 64; myDimensions[2] = 20; myDimensions[3] = 20; w = 0; shapeType = "square"; repaint(); } } /** Clear the screen and Draw a Rectangle * * @author Larry Healy CIS 525 Assignment 3 * @param Action depress button * @return Action performed * @version Java 1.1.4 * */ public class DrawMyRect implements ActionListener { public void actionPerformed(ActionEvent event) { myDimensions[0] = 20; myDimensions[1] = 60; myDimensions[2] = 40; myDimensions[3] = 80; w = 0; shapeType = "rect"; repaint(); } } /** Clear the screen and Draw a Arc * * @author Larry Healy CIS 525 Assignment 3 * @param Action depress button * @return Action performed * @version Java 1.1.4 * */ public class DrawMyArc implements ActionListener { public void actionPerformed(ActionEvent event) { myDimensions[0] = 20; myDimensions[1] = 60; myDimensions[2] = 50; myDimensions[3] = 75; myDimensions[4] = 25; myDimensions[5] = 165; w = 0; shapeType = "arc"; repaint(); } } /** Clear the screen and Draw a Oval * * @author Larry Healy CIS 525 Assignment 3 * @param Action depress button * @return Action performed * @version Java 1.1.4 * */ public class DrawMyOval implements ActionListener { public void actionPerformed(ActionEvent event) { myDimensions[0] = 20; myDimensions[1] = 60; myDimensions[2] = 20; myDimensions[3] = 70; w = 0; shapeType = "oval"; repaint(); } } /** Clear the screen and Draw by using the mouse drag listener * * @author Larry Healy CIS 525 Assignment 3 * @param Action depress button * @return Action performed * @version Java 1.1.4 * */ public class DrawMyOwn implements ActionListener { public void actionPerformed(ActionEvent event) { w = 0; shapeType = "own"; repaint(); } } }