In this Java AWT Drawing Tutorial, we will see how to draw rectangle using Mouse Press and Mouse Release. We will also see the challenges one will face when drawing the rectangle using mouse and drawRect Graphics API.
1 2 3 4 5 6 7 8 |
//Sample 09: Relocate Rectangle Start Point private Point GetTopLeft() { Point TopLeft = new Point(); TopLeft.x = Math.min(FirstPoint.x,SecondPoint.x ); TopLeft.y = Math.min(FirstPoint.y,SecondPoint.y ); return TopLeft; } |
1 2 3 4 5 6 7 |
case "RectD": //Sample 10: Draw Rectangle Point TopLeft = GetTopLeft(); int width = Math.abs(SecondPoint.x - FirstPoint.x); int height = Math.abs(SecondPoint.y - FirstPoint.y); g.drawRect(TopLeft.x, TopLeft.y, width, height); break; |
- AWT Drawing – Part 2 – Perform Line Drawing with Mouse #24
- AWT Drawing – Part 4 – Perform Free Hand Drawing #26
Categories: AWT-Tube