//Purpose: Create an applet that will listen to Mouse Events // so that whenever the user clicks down on the mouse and // drags it and clicks up...a straignt line will be drawn // from the points of where they clicked down and clicked up. // Also, a current straigth line is drawn as the user is // dragging the mouse from the start (clicked down) to the // currently dragged position. You may assume that a maximum // of 20 lines will be draw on the Applet's graphics. import java.awt.*; import java.applet.*; import java.awt.event.*; public class test9 extends Applet implements MouseListener,MouseMotionListener{ //Variables Point Start[] = new Point[20]; //X,Y info of start of lines Point End[] = new Point[20]; //X,Y info of end of lines Point current_start; Point current_point; int numlines= 0; Point boundary; //to represent the boundary of the applet //Set background to yellow, register listeners // get boundary of applet public void init() { setBackground(Color.yellow); addMouseListener(this); addMouseMotionListener(this); current_point = null; boundary = new Point(this.size().width,this.size().height); } //Mouse Clicked Down...save in current_start public void mousePressed(MouseEvent e) { if(numlines < 20) current_start = new Point(e.getX(), e.getY()); else System.out.println("Can Not Take Any More Input Lines"); } //Mouse Drag....reset current_point to current position public void mouseDragged(MouseEvent e) { if(numlines <20) { current_point = new Point(e.getX(), e.getY()); repaint(); //will draw this current line } } //Mouse Up ...save created line if can and call repaint // to draw it. Make sure the end point is inside the applet public void mouseReleased(MouseEvent e) { if( (numlines < 20) && (e.getX() < boundary.x) && (e.getY() < boundary.y) ) { Start[numlines] = current_start; End[numlines] = new Point(e.getX(), e.getY()); numlines++; //Reset current points to empty current_point = null; current_start = null; //repaint will draw all lines including this one // just created. repaint(); } } //Draw all of the lines stored in Start[], End[]. //Draw the current line from current_start to current_point public void paint(Graphics g) { //set drawing color g.setColor(Color.red); //Draw stored lines for (int i=0; i