/* DrawOuter ======= Author Chris Pollett Course: CSCI 102 Clark University Date: Feb.8 Purpose: Implements a draw progam which has three ======== modes free-hand mode, line draw mode, and clear mode. The latter clears the screen. How to Use: =========== javac DrawOuter.java appletviewer DrawOuter.class Methods: ======== init() -- overrides Applet init() to initialise menus, mouse listeners, the frame app attached to DrawOuter and the instance m of DrawInner attached to app. actionPerformed(ActionEvent e) -- handles events generated by menus. Inner Classes: ============== DrawInner -- the inner applet on which all the drawing is done MotionHandler -- handles mouseDragged events MouseHandler -- handles mousePressed and mouseReleased events */ import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class DrawOuter extends Applet implements ActionListener { //would be better to have an inner class implement //actionPerformed but am too lazy to change it MenuItem[] mi = {new MenuItem("free-hand"), new MenuItem("line"), new MenuItem("clear")}; final Frame app = new Frame("Draw Program"); DrawInner m = new DrawInner(); MenuBar mb = new MenuBar(); Menu drawtypes = new Menu("Draw Types"); public void init() { //post: all components of applet set up app.setSize(400,300); //set frame size app.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { app.dispose(); } }); //check whether frame has been closed drawtypes.add(mi[0]); //add menu items to menu drawtypes.add(mi[1]); drawtypes.add(mi[2]); mi[0].addActionListener(this); // listen to menu items mi[1].addActionListener(this); mi[2].addActionListener(this); mb.add(drawtypes); // add menu to menu bar app.setMenuBar(mb); // add menu bar to frame m.init(); m.start(); //start DrawInner applet m app.add(m, BorderLayout.CENTER); app.setVisible(true); add(app); // show frame } public void actionPerformed(ActionEvent e) { //post: draw mode set depending on menu item selected if(e.getSource() == mi[0]){ m.mode = "free-hand"; } if(e.getSource() == mi[1]){ m.mode = "line"; } if(e.getSource() == mi[2]){ m.oldmode= m.mode; m.mode = "clear"; m.repaint(); } } class DrawInner extends Applet { //see DrawOuter above for a description private int startX = -10, xValue = -10, oldXValue= -10; private int startY = -10, yValue = -10, oldYValue= -10; String mode ="free-hand", oldmode="free-hand"; boolean started = false; public void init() { //post: listeners set up and background color set addMouseMotionListener( new MotionHandler( this )); addMouseListener (new MouseHandler(this)); setBackground(Color.white); } public void paint (Graphics g) { //post: applet drawn to appropriately depending on mode. g.setColor(Color.black); if(mode.equals("free-hand")) g.fillOval(xValue,yValue,2,2); if(mode.equals("clear")) mode=oldmode; //after a clear go // back to old mode if(mode.equals("line")) { if(started){ g.setXORMode(Color.white); g.drawLine(startX, startY,oldXValue,oldYValue); g.drawLine(startX, startY,xValue,yValue); } } } public void update( Graphics g){ //post: update normally clears the screen // this overrriden version only does this if we are // in clear mode if(mode.equals("clear")) {super.update(g); } else{ paint (g );} } public void setCoordinates(int x, int y) { //post: set values of current and old // mouse position. called by a mouse // dragged event oldXValue = xValue; oldYValue = yValue; xValue =x; yValue =y; repaint(); } public void setStartCoordinates(int x, int y) { //post: set start, current and old x,y // coordinates of mouse. Called by // MouseHandler when in line mode startX =x; startY =y; xValue =x; yValue=y; oldXValue = x; oldYValue= y; } } // // MotionHandler inner class // class MotionHandler extends MouseMotionAdapter { //see DrawOuter above for a description private DrawInner drawer; public MotionHandler(DrawInner d){drawer =d;} public void mouseDragged(MouseEvent e) { //post: give current x,y coordinate to the // DrawInner applet when everything drawn. drawer.setCoordinates( e.getX(), e.getY());} } // // MouseHandler inner class // class MouseHandler extends MouseAdapter { //see DrawOuter above for a description private DrawInner drawer; public MouseHandler(DrawInner d){drawer =d;} public void mousePressed(MouseEvent e) { //post: set up DrawInner class to draw // lines if(drawer.mode.equals("line")){ drawer.setStartCoordinates( e.getX(), e.getY()); drawer.started=true; } } public void mouseReleased(MouseEvent e) { //post: set up stop lines from being drawn. // drawer.started=false; drawer.setStartCoordinates( e.getX(), e.getY()); } } }