1. AWT Graphics and Components
In Java, Graphics objects take care of drawing works. The Graphics object draws the components of AWT components. In other words, all the AWT Components are linked with an AWT Graphics object which takes care of how to draw them. Note, java containers like Frame and Dialogs are also a component and hence they also contain the Graphics object.
The Graphics object holds the drawing factors like Color, Font, Line Thickness. It also exposes methods to do drawings like Line, rectangle, Circle. When the program coder invokes the drawing methods on the Graphics objects, it applies the properties like Color, Fonts etc. to the drawing.
2. The Role of Paint and Repaint
Most of the program writers come across the Graphics methods paint and repaint. Let us learn their roles here. Have a look at the below picture:
Here, we have a Frame Window which has some drawing in it. You can see the Circle and Star objects are in different line thickness. Also notice that the shapes are in color like red, green. The Graphics objects associated with the Frame Window takes care of drawing these shapes. The Graphics object hold other member like Font and Color to define its drawing characteristics. When we derive a component class, we have the freedom of overriding the paint method. Java AWT Framework uses this paint method to perform the user defined drawings.
The repaint method performs following two tasks:
- Draw the background of the component with the back color assigned for the Graphics object. This will erase any existing drawing on the surface.
- Make a call to the paint method. Here, if use defined a drawing, that will be drawn.
As the name suggests, repaint is to paint the drawing from the scratch after erasing the existing content. The paint is an override which Java AWT calls whenever it needs to draw on the component. Note, the screenshot shows a Java AWT Frame which is a container. Since Java AWT derived the containers from the component, we can say the Frame Window as component also. So it will have valid Graphics object to serve the need of drawings.
3. The use of getGraphics Method
All the components in Java AWT are tied to a Graphics object and one can retrieve it by calling the function getGraphics. Say, for example, you can call getGraphics on a AWT Button and draw a focus rectangle in it via the returned AWT Graphics object.
4. About the AWT Graphics Example
In the next coming articles, we will develop an example which helps in learning the Java AWT Graphics and its functions. Have a look at the screenshot of the example below:
The example is a Java AWT Frame, and it contains drawing commands on the right-hand side. There are three Drawing Modes to draw lines, rectangles and Free Hand lines and curves. The Mode tells the user what drawing mode they are in and they can change it by clicking the command buttons.
We can perform drawing using the mouse on the left side white drawing screen. For example, to draw one or more lines, one should first click the Draw Line button. This will set drawing mode as “Line Drawing”. Then, they can press down the left mouse button, drag it and then release. The mouse press defines the first point and release defines the second point. This sample draws the line between these points. Likewise, the user can draw multiple lines while drawing mode is in Line drawing. The same goes true for rectangle drawing as well.
To perform Free-Hand drawing, the user will set the mode to Free-Hand by clicking the button for it. Then they can click and drag the mouse. A smooth inter-connected line will be drawn between the previous cursor location to current location. Using this user can draw curves and lines. Even they can attempt their signature.
5. Watch AWT Drawing – As Youtube Video