Drawing AWT Rectangle with Mouse

Rectangle via drawRect: AWT Drawing – Part 3

In this part of the AWT Drawing, we will see how to draw a rectangle using a mouse. The AWT API requires a corner point plus width & height of the rectangle. Graphics API method drawRect needs four parameters to draw a rectangle. Now, have a look at the below picture: First two params are for locating the rectangle in the Graphic sheet. In our case, the sheet is a AWT Frame Window. The width and height define the rectangle’s dimension from the specified location. So, x and y denote the top-left corner of the rectangle and Width and Height denote the size of the rectangle. The below picture shows how a rectangle is drawn in the Frame window with the passed in four parameters:

Java AWT Line Drawing Sequence

Line Drawing : AWT Drawing – Part 2

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: