/* LineDraw * * Example of a Java Applet that draws two connected lines near the middle of the screen. * Subsequent clicks reset the line starting point to the previous click location, so the line extends from the previous * mouse click location to the current mouse click location. Display the current line as well as the previous line. * In addition the line endpoints are displayed as text at the bottom left of the screen. * Ensure line segments don't intersect. * * Note that this is an "old-fashioned" way of handling mouse clicks. Compiling this will likely lead to a * warning of using the "deprecated" method for mouseDown(). you can ignore this warning for now. * A more robust (and more complex) approach is to us Swing and JApplet. */ // import predefined code to help with various aspects of the program import java.awt.Graphics; // allows drawing import java.awt.Event; // allows handling events import java.awt.Font; // allows placing text import java.awt.geom.*; // used in checking for line intersection // declare the class, that itself is a kind of Applet public class LineDraw extends java.applet.Applet { // Declare the variables used to store the various lines // Line 0 int line0x1; int line0y1; int line0x2; int line0y2; // Line 1 int line1x1; int line1y1; int line1x2; int line1y2; // Line 2 int line2x1; int line2y1; int line2x2; int line2y2; // Line 3 int line3x1; int line3y1; int line3x2; int line3y2; // Line 4 int line4x1; int line4y1; int line4x2; int line4y2; // store which line should be changed on this turn int whichLine = 0; final int NUMBER_OF_LINES = 5; // this is a constant, used below in the program //------------------------------------------------------------------------------------------------------------------------------- // init() // Code inside init() is used to initialize values used in the Applet public void init() { // set the line 0 to start from the middle and go up line0x1 = (getSize().width / 2); line0y1 = (getSize().width / 2); line0x2 = line0x1; line0y2 =line0y1 - 40; // set line 1 to then go right. Add a small offset so that intersection could still work line1x1 = line0x2 + 1; line1y1 = line0y2 - 1; line1x2 = line1x1 + 40; line1y2 = line1y1; // set line 2 to go down. Add a small offset so that intersection could still work line2x1 = line1x2 + 1; line2y1 = line1y2 + 1; line2x2 = line2x1; line2y2 = line2y1 + 40; // set line 3 to again go right. Add a small offset so that intersection could still work line3x1 = line2x2 + 1; line3y1 = line2y2 + 1; line3x2 = line3x1 + 40; line3y2 = line3y1; // set line 4 to again go up. Add a small offset so that intersection could still work line4x1 = line3x2 + 1; line4y1 = line3y2 - 1; line4x2 = line4x1; line4y2 = line4y1 - 40; setBackground(java.awt.Color.white); // set the background color to be white setFont(new Font("Helvetica",Font.BOLD,14)); // set the font for displaying text requestFocus(); // make the applet window come to the front }//end init() //------------------------------------------------------------------------------------------------------------------------------- // mouseDown() // Handle the mouse clicks, resetting the line endpoints to be the mouse (x,y) location // Before changing line locations, verify that the lines don't intersect. If they do intersect, // don't update the line values and end the program public boolean mouseDown(Event evt, int x, int y) { // When endpoints overlap, then line intersection doesn't work correctly. For this reason as each new line is drawn, // offset its endpoint by 1. int xOffset = 1; int yOffset = 1; // Change the appropriate line, depending on which turn it is. if( whichLine == 0) { // change the values for line 0, where the new starting point for line 0 is the ending point for line 4, // and the ending point for line 0 is the mouse click location. // First adjust the endpoint offsets, if necessary, so the endpoints aren't the same, which causes intersection problems if( x