I find it difficult to get a custom panel to redraw it while I click on it with the mouse.
Basically, I draw free-form strings on the user panel with the following handlers attached:
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);
However, I cannot force it to redraw while I drag the mouse, only after I release it (the line is drawn correctly). Oddly enough, if in the middle of my drag and drop, I right-clicked, the line will start to draw when I drag.
Any help would be appreciated.
Edit: for clarity, the material StartLine () / AddLineSegment () / EndLine () simply adds points to the ArrayList Point objects, and the repaint () function is redefined to cycle through these points and draw line segments between each. Code below:
public void DrawShapes(Graphics g) { g.setColor(Color.black); for(Geometry.Shape shape : _shapeList) { if(shape instanceof Geometry.Line) { ArrayList<Point> points = ((Line) shape).GetPointList(); Point p1 = points.get(0); for(int i=1; i<points.size(); i++) { Point p = points.get(i); g.drawLine(p1.x, p1.y, px, py); p1 = p; } } }
Edit: Figured this out. This is because I cycled through the list of lines, but I forgot that I did not add the line that I am currently drawing on this list until I release the mouse and call EndLine (). I had to put separate code in the repaint () method to draw the form, which I am now in the middle of drawing. Code added in repaint ():
if(_currentShape instanceof Geometry.Line) { ArrayList<Point> points = ((Line)_currentShape).GetPointList(); Point p1 = points.get(0); for(int i=1; i<points.size(); i++) { Point p = points.get(i); g.drawLine(p1.x, p1.y, px, py); p1 = p; } }