title: Drawing a line from 2 mouse clicks
body: <p>I am trying to draw a line from a series of mouse clicks.</p>
  
  <p>For example, when i click from one point on the screen to the next, it connects up the lines, and then i can keep clicking to next points where it continues to draw a continuous drawing line.</p>
  
  <p>An example is here: <a href="http://oneslime.net/java/Tutorial_2" rel="nofollow">http://oneslime.net/java/Tutorial_2</a> under <strong>Exercise 2</strong></p>
  
  <p>I believe there is some error in my logic (does not draw lines, just a point), but I just can't seem to find it!</p>
  
  <p>Can anybody help me out?</p>
  
  <p>Here is my code:</p>
  
  <pre><code>import javax.swing.*;
  import java.awt.*;
  import java.awt.event.MouseAdapter;
  import java.awt.event.MouseEvent;
  
  public class RoadCreator extends JPanel {
  
      private MouseHandler mouseHandler = new MouseHandler();
  
      private Point previousPoint = new Point();
      private Point nextPoint = new Point();
      private boolean drawing;
  
      public RoadCreator() {
          this.setPreferredSize(new Dimension(640, 480));
          this.addMouseListener(mouseHandler);
         this.addMouseMotionListener(mouseHandler);
      }
  
      @Override
      protected void paintComponent(Graphics g) {
          super.paintComponent(g);
          Graphics2D g2d = (Graphics2D) g;
          g2d.setColor(Color.blue);
          g2d.setRenderingHint(
              RenderingHints.KEY_ANTIALIASING,
              RenderingHints.VALUE_ANTIALIAS_ON);
          g2d.setStroke(new BasicStroke(8,
              BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL));
          g.drawLine(previousPoint.x, previousPoint.y, nextPoint.x, nextPoint.y);
      }
  
      private class MouseHandler extends MouseAdapter {
  
          @Override
          public void mousePressed(MouseEvent e) {
              drawing = true;
              nextPoint = e.getPoint();
              repaint();
              previousPoint = e.getPoint();
  
          }
      }
  
      public void display() {
          JFrame f = new JFrame("Road Creator");
          f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          f.add(this);
          f.pack();
          f.setVisible(true);
      }
  
      }
  </code></pre>
  
  <p>Any help would be GREATLY appreciated, thank you!</p>