title: Java Challenge on Permitting the User to Draw A Line
body: <p>My question has been alluded to in <a href="https://stackoverflow.com/questions/9711938/java-draw-line-as-the-mouse-is-moved">java draw line as the mouse is moved</a>, however, I have not advanced far enough into this book to have covered JPanels, JFrames and Points as stated by the prior programmer who asked this question.  </p>
  
  <p>Answering this question definitely would help most beginner programmers better understand the graphics class and drawing, an often intricate process, especially for beginners.  </p>
  
  <p>According to the text I am using (as I am learning Java on my own), this was the example of how to draw a line using Java: </p>
  
  <pre><code>/*
   * LineTest
   * Demonstrates drawing lines
   */
  import java.awt.*;
  public class LineTest extends Canvas {
  public LineTest() {
  super();
  setSize(300, 200);
  setBackground(Color.white);
  }
  public static void main(String args[]) {
  LineTest lt = new LineTest();
  GUIFrame frame = new GUIFrame("Line Test");
  frame.add(lt);
  frame.pack();
  frame.setVisible(true);
  }
  public void paint(Graphics g) {
  g.drawLine(10, 10, 50, 100);
  g.setColor(Color.blue);
  g.drawLine(60, 110, 275, 50);
  g.setColor(Color.red);
  g.drawLine(50, 50, 300, 200);
  }
  }
  </code></pre>
  
  <p>The specification is:   </p>
  
  <blockquote>
    <p>Create an application that allows you to draw lines by clicking the initial
    point and dragging the mouse to the second point. The application should
    be repainted so that you can see the line changing size and position as you
    are dragging the mouse. When the mouse button is released, the line is
    drawn.</p>
  </blockquote>
  
  <p>As you will recognize, running this program does not create any drawing by the user.  I believe this error is encountered due to line 21: <code>g.drawLine(x, y, x2, y2);</code> being incorrect since this is the statement defining the drawing of the line.  </p>
  
  <p>Any help is greatly appreciated.  Thank you in advance for all your time and cooperation regarding this matter.  </p>
  
  <p>My code to answer the question is: </p>
  
  <pre><code>import java.awt.*;
  import java.awt.event.*;
  
  public class LineDrawer extends Canvas
                          implements MouseListener, MouseMotionListener {
  int x, y, x2, y2;
  
  public LineDrawer() {
      super();
      setSize(300, 200);
      setBackground(Color.white);
  }
  public void mouseClicked(MouseEvent me) {
                  int x = me.getX();
                  int y = me.getY();
                  int x2 = me.getX();
                  int y2 = me.getY();
  
      }
  public void paint(Graphics g) {
              g.drawLine(x, y, x2, y2);
              g.setColor(Color.blue);
      }
  
  
      public void mousePressed(MouseEvent me) {
          repaint();
      }
  
      public void mouseDragged(MouseEvent me) {
      }
      public void mouseExited(MouseEvent me) {
      }
      public void mouseEntered(MouseEvent me) {
      }
  
  
      public void mouseReleased(MouseEvent me) {
      }
      public void mouseMoved(MouseEvent me) {
      }
  
      public static void main(String args[]) {
          LineDrawer ld = new LineDrawer();
          GUIFrame frame = new GUIFrame("Line Drawer");
          frame.add(ld);
          frame.pack();
          frame.setVisible(true);
      }
  
  }
  </code></pre>
  
  <p>P.S.:  I have been hesitant to ask for help since I am concerned that other programmers would answer with methods that I have not yet learned.  </p>