title: Java Swing Repaint While Dragging Mouse
body: <p>I'm having difficulty getting a custom panel to repaint itself while I'm dragging the mouse over it.</p>
  
  <p>Basically, I'm drawing free-form lines on a custom panel with the following handlers attached:</p>
  
  <pre><code>MouseInputAdapter mia = new MouseInputAdapter() {
              @Override
              public void mousePressed(MouseEvent e) {
                  if(_app_split_right_buttons_radioInkBtn.isSelected()) {
                      _app_split_right_journal.StartLine();
                      _app_split_right_journal.AddLineSegment(e.getX(), e.getY());
                  }
                  _app_split_right_journal.repaint();
              }
              @Override
              public void mouseReleased(MouseEvent e) {
                  if(_app_split_right_buttons_radioInkBtn.isSelected()) {
                      _app_split_right_journal.AddLineSegment(e.getX(), e.getY());
                      _app_split_right_journal.EndLine();
                  }
                  _app_split_right_journal.repaint();
              }
              @Override
              public void mouseDragged(MouseEvent e) {
                  if(_app_split_right_buttons_radioInkBtn.isSelected()) {
                      _app_split_right_journal.AddLineSegment(e.getX(), e.getY());
                  }
                  _app_split_right_journal.repaint();
              }
          };
          _app_split_right_journal.addMouseListener(mia);
          _app_split_right_journal.addMouseMotionListener(mia);
  </code></pre>
  
  <p>However, I can't seem to get it to repaint while I'm dragging the mouse, only after I release it (the line draws correctly). Oddly enough, if in the middle of my drag, I right-click, the line will suddenly start drawing as I'm dragging.</p>
  
  <p>Any help would be appreciated.</p>
  
  <p>Edit: For clarity, the StartLine()/AddLineSegment()/EndLine() stuff just adds points to an ArrayList of Point objects, and the repaint() function has been overridden to cycle through those points and draw line segments between each. Code below:</p>
  
  <pre><code>public void DrawShapes(Graphics g) {
          g.setColor(Color.black);
          for(Geometry.Shape shape : _shapeList) {
              if(shape instanceof Geometry.Line) {
                  ArrayList&lt;Point&gt; points = ((Line) shape).GetPointList();
                  Point p1 = points.get(0);
                  for(int i=1; i&lt;points.size(); i++) {
                      Point p = points.get(i);
                      g.drawLine(p1.x, p1.y, p.x, p.y);
                      p1 = p;
                  }
              }
  }
  </code></pre>
  
  <p>Edit: Figured it out. It's because I was cycling through the list of lines, but I forgot that I don't add the line I'm currently drawing to that list until I release the mouse and call EndLine(). I had to put separate code in the repaint() method to paint the shape I'm currently in the middle of drawing. Code added to repaint(): </p>
  
  <pre><code>if(_currentShape instanceof Geometry.Line) {
              ArrayList&lt;Point&gt; points = ((Line)_currentShape).GetPointList();
              Point p1 = points.get(0);
              for(int i=1; i&lt;points.size(); i++) {
                  Point p = points.get(i);
                  g.drawLine(p1.x, p1.y, p.x, p.y);
                  p1 = p;
              }
          }
  </code></pre>