/** MyApplet9.java: Interactive Line Drawing and Mouse Events ** Assignment: Modify this file centering the click ** instruction at the top of the applet, and changing ** it so that instead of drawing lines, it has some other ** unusual (weird?) behavior to entertain (befuddle?) ** the user. ** Also, take a look at the MyApplet9.html webpage ** file for displaying this applet. **/ /** USE EXISTING JAVA CLASSES **/ import java.awt.*; import java.applet.Applet; /** EXTEND THE JAVA APPLET CLASS **/ public class MyApplet9 extends Applet { Font font = new Font("TimesRoman", Font.BOLD, 18); final int maxnumlines = 20; // Define max number of lines, final int maxsize = maxnumlines + 5; // max array size, Point startpoint[] = new Point[maxsize]; // the array of start points, Point endpoint[] = new Point[maxsize]; // the array of end points, Point anchorpoint, connectpoint; // the current anchor point and int entrynum = 0; // connection point, and the // current array entry number, // starting with 0 (instead of 1). public void init() { setBackground(Color.white); // Change the gray background to white. } /** HANDLE THE MOUSEDOWN EVENT **/ public boolean mouseDown(Event e, int x, int y) { if (entrynum < maxsize) { anchorpoint = new Point(x,y); // Initialize both points to connectpoint = anchorpoint; // the current mouse position. return true; // Note that this will give us a } // way to prevent painting a single else // point (see the mouseUp method). { return false; } } /** HANDLE THE MOUSEDRAG EVENT **/ public boolean mouseDrag(Event e, int x, int y) { if (entrynum < maxsize) // A mouseDrag event occurs only { // when the mouse is moved. So, connectpoint = new Point(x,y); // we must have connectpoint != repaint(); // anchorpoint, which ensures that return true; // a temporary (purple) line will } // be painted (see the paint method). else { return false; } } /** HANDLE THE MOUSEUP EVENT **/ public boolean mouseUp(Event e, int x, int y) { if (entrynum < maxsize && connectpoint != anchorpoint) { // Don't add a single point. addline(); // Add new points to the arrays. repaint(); // Repaint everything, including return true; // the new line (all blue). } else { return false; } } /** THE ADDLINE METHOD **/ void addline() { startpoint[entrynum] = anchorpoint;// Record the startpoint and endpoint[entrynum] = connectpoint;// endpoint of the new line. entrynum++; // Add 1 to the current entry number. anchorpoint = null; // Nullifying these points prevents connectpoint = null; // the line from being painted twice } // (see the paint method). /** CREATE THE CURRENT FRAME **/ public void paint(Graphics g) { g.setColor(Color.blue); g.setFont(font); g.drawString("Click and Drag in Window", 100, 20); for (int i = 0; i < entrynum; i++) // Note that nothing happens here, { // until a mouseDown event occurs. if (i < maxnumlines) // Repaint all lines from 0 to { // maxnumlines-1. g.drawLine(startpoint[i].x, startpoint[i].y, endpoint[i].x, endpoint[i].y); } else { g.setColor(Color.red); // Also, if entrynum > maxnumlines, g.drawString("Sorry, line quota exceeded !!",// then print this endpoint[i].x, endpoint[i].y);// instead of any } // extra line. } if (connectpoint != anchorpoint) // Paint a temporary purple line { // in response to a mouseDrag event. g.setColor(Color.magenta); g.drawLine(anchorpoint.x, anchorpoint.y, connectpoint.x, connectpoint.y); } } }