Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

Line Drawing : AWT Drawing – Part 2

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:

Java AWT Line Drawing Sequence
Java AWT Line Drawing Sequence

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.

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.

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.

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.

Line Drawing Java AWT Example
Line Drawing Java AWT Example

6. AWT Drawing Part 2: Lines – Youtube Video

Categories: AWT

Tags: , ,

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.