The below picture shows the example which we will create here. In the AWT Frame, we will display only one button. The button Do Task will open a AWT Window which shows a status message for a small while. This window is a sign to the user that some processing is going on. But our example mimics the long-running task with the ‘Thread.sleep’ method. After a few seconds, the window closes automatically. You can use this same example to create a flash window. Here, we displayed some text in the window. In a flash window, you can display an image to show your brand.
rubber band cut from its circular shape as a line. Hold one end of a rubber band between left thumb and left index finger and hold other end between thumb and index finger of right hand. Now, keeping your left-hand stand still, move the other end of the rubber band on your right hand. You can see a line stretching. While drawing this aid the user to see how their drawing primitive (Say Line, Rectangle, circle) looks while they still drag the mouse. In this example, we will use this rubber banding technique to draw line and rectangle. The user should first place a check mark on the rubber band checkbox and then pick either line or rectangle as drawing mode. Now, we will proceed with the example.
But for Free-Hand drawing, we should not wait till mouse release. We should draw while the mouse is dragged. This means we need to arrange the points P1 and P2 on each reported drag event. In the first part of the below picture, the Red Cross line shows the MouseEvent which AWT reports to the listener. We can still use the drawLine API and connect the currently reported point with the previous one. The net effect is a smooth curve even-though what we draw is a straight line in small chunks. Note, AWT reports the drag events so quickly and hence user will not see the straight inter-connected lines.
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:
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: