In this Java AWT Drawing Tutorial, we will do Free Hand Drawing using Mouse Drag. Here, you will learn the need for calling the Paint() method directly in-place of repaint() call.
1 2 3 4 5 6 7 8 |
case "FreeH": //Sample 11: Free Hand Drawing g.drawLine( FirstPoint.x, FirstPoint.y, SecondPoint.x, SecondPoint.y); break; |
1 2 |
//Sample 12: Register Mouse Motion Listener addMouseMotionListener(this); |
1 2 3 4 5 6 7 |
//Sample 06: Set Second Point //Sample 13: Introduce the Condition if (DrawMode.compareTo("FreeH") != 0) { SecondPoint.setLocation(e.getX(), e.getY()); repaint(); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public void mouseDragged(MouseEvent e) { //Sample 14: Perform Free Hand Drawing if (DrawMode.compareTo("FreeH") == 0) { if (SecondPoint.x != 0 && SecondPoint.y != 0) { FirstPoint.x = SecondPoint.x; FirstPoint.y = SecondPoint.y; } SecondPoint.setLocation(e.getX(), e.getY()); paint(getGraphics()); } } |
- AWT Drawing – Part 3 – Perform Rectangle Drawing with Mouse #25
- AWT Drawing Part 5 – setXORMode & Rubber Banding of Line, Rect #27
Categories: AWT-Tube