1. Drawing Line via AWT Graphics
A line requires two points. The JAVA AWT Graphics API also require two points to perform Line Drawing. In our Example, we will use left mouse press to record the first point and left mouse release to record the second point. Then, the Graphics API uses these two points to draw the line. This means, after pressing the mouse button, the user will drag the mouse to the location where they want the second point. Once the second point location is reached, the user will release the mouse button. The below picture explains the sequence of operation on the mouse to draw a line:
2. Set First Point for AWT Line
The mouse press will register the Point
P1. So, we will use the mousePressed
handler function to register this first point P1. In the below code, we reset the points P1 and P2 before storing the actual mouse location in Point P1. This reset is required so that user can draw the line a fresh.
The method getX
and getY
from the MouseEvent
tells the current mouse cursor area. Using this location values, we set the FirstPoint instance of type Point by calling setLocation
method.
1 2 3 4 5 6 7 |
public void mousePressed(MouseEvent e) { //Sample 05: Set First Point FirstPoint.setLocation(0, 0); SecondPoint.setLocation(0, 0); FirstPoint.setLocation(e.getX(), e.getY()); } |
3. Second Point for the AWT Line
We have AWT Point object to represent start of the line is ready. Now, we need to get the second point for the AWT Line. The mouseReleased
handler will record this point. In the below code, getX
and getY
method of MouseEvent
feeds the SecondPoint
instance with the x and y coordinate values. Once second point for the line is ready, we make a call to repaint
method. The repaint
will paint the AWT Frame with background color. This action erases whatever present in the Frame Window. Next, it will make a call to the paint method. We will call all the drawing functions in the paint method override. Note, we haven’t yet overridden this paint method.
1 2 3 4 5 6 |
@Override public void mouseReleased(MouseEvent e) { //Sample 06: Set Second Point SecondPoint.setLocation(e.getX(), e.getY()); repaint(); } |
4. Register With MouseListener
In the constructor, we must register the AWT Frame with the MouseListener
. This will push the MouseEvent
to the listener so that we can register the First and Second point for drawing the Line.
1 2 |
//Sample 07: Register with Mouse Listener addMouseListener(this); |
5. The drawLine API
As we are aware, the repaint
will call the paint
method override. So first we must override the paint
method in the AWT Frame. The below code shows the skeleton for the override. Based on the DrawMode
, we form the switch…case and inside the body, we can perform the actual drawing. In this example, we are interested in drawing the line and hence we will take care of the LineD case.
Java AWT Graphics
provides a method
drawLine which we perform the Line Drawing on the component attached to it. In our case, Frame is the component which is attached to the Graphics
object. Note, for the drawLine method we pass (x1,y1) and (x2,y2) points via the Point instances FirstPoint
and SecondPoint
.
Below picture shows how a line is drawn in the AWT Frame Window. Note, when you attempt to draw the next line, this first line will be deleted. However, this will make you understand how to draw a line using Graphics object and Mouse. To preserve all the lines, the co-ordinates values needs to be persisted in an internal data-structure.

6. AWT Drawing Part 2: Lines – Youtube Video
Categories: AWT
Tags: AWT Graphics, drawLine API, MouseListener