Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
🔎
Sign In | Register
HOW-TO JAVA
By Todd Sundsted, JavaWorld
NOV 1, 1996 12:00 AM PT
HOW-TO
Using the Graphics class
A close look at the Graphics class and the drawing primitives it provides, and a demonstration of
its use
A variety of factors inspire people to write software programs. I believe that for many,
motivation springs from a desire to create graphics, to manipulate images, or to animate.
Whether they want to create arcade games, ight simulators, or CAD packages, developers often
begin by learning to draw.
The graphics toolbox within the Abstract Windowing Toolkit (or AWT) makes it possible for a
Java programmer to draw simple geometric shapes, print text, and position images within the
borders of a component, such as a frame, panel, or canvas.
This column is my rst on the topic of graphics. It will focus on the Graphics class and its
methods for drawing simple geometric shapes, and will introduce the process by which painting
(and repainting) occurs.
In order to draw, a program requires a valid graphics context (represented by an instance of the
Graphics class). Because the Graphics class is an abstract base class, it cannot be
instantiated directly. An instance is typically created by a component, and handed to the
program as an argument to a component's update() and paint() methods. These two methods,
along with the repaint() method, are discussed in the next section.
The methods
The following three methods are involved in displaying graphics. Default versions of each are
provided by class Component. Methods update() and paint() should be rede ned to perform the
desired graphics operations.
[ Learn Java from beginning concepts to advanced design patterns in this comprehensive 12-part
course! ]
repaint()
public void repaint() public void repaint(long tm) public void repaint(int x, int y, int w, int h) public
void repaint(long tm, int x, int y, int w, int h)
The repaint() method requests that a component be repainted. The caller may request that repainting occur as soon
The code in the mouseDown() event handler recalculates the position of an object in a display based on the positio
update()
paint()
The paint() method is called from an update() method, and is responsible for actually draw
To reduce the time required to repaint the display, the AWT takes two shortcuts:
First, the AWT repaints only those components that need repainting, either because they have been
uncovered, or because they asked to be repainted.
https://www.javaworld.com/article/2077282/learn-java/using-the-graphics-class.html 3/7
2/23/2019 Using the Graphics class | JavaWorld
Second, if a component was covered and is uncovered, the AWT repaints Sign
onlyInthe
| Register
portio
The applet in Figure 1 allows you to observe this process as it occurs. Ignore the text area at the top of t
The methods described in the following section take, as parameters, values that specify how a shape is to
A coordinate system is a method for unambiguously specifying the location of points in space. In the cas
This section introduces methods for drawing lines, rectangles, ovals and arcs, and polygons. Since these
lines
https://www.javaworld.com/article/2077282/learn-java/using-the-graphics-class.html 4/7
2/23/2019 Using the Graphics class | JavaWorld
Sign Inthe| spe
Register
This is the simplest of all graphics methods. It draws a straight line, a single pixel wide, between
The applet in Figure 3 demonstrates the drawLine() method in action. The source code is available here.
rectangles
void drawRect(int x, int y, int w, int h) void fillRect(int x, int y, int w, int h) v
Each of these graphics methods require, as parameters, the x and y coordinates at which to begin the rect
The rounded-rectangle graphics methods require two additional parameters, an arc width and an arc heig
The applet in Figure 4 demonstrates these methods in action. The source code is available here.
void drawOval(int x, int y, int w, int h) void fillOval(int x, int y, int w, int h) v
Each of these graphics methods require, as parameters, the x and y coordinates of the center of the oval o
The arc graphics methods require two additional parameters, a start angle and an arc angle, to specify th
https://www.javaworld.com/article/2077282/learn-java/using-the-graphics-class.html 5/7
2/23/2019 Using the Graphics class | JavaWorld
Sign In | Register
Figure 5: Angle speci cation
The applet in Figure 6 demonstrates these methods in action. The source code is available here.
polygons
Polygons are shapes formed from a sequence of line segments. Each of the polygon graphics methods re
The applet in Figure 7 demonstrates these methods in action. The source code is available here.
Conclusion
Believe it or not, these few simple graphics primitives, combined with all we've covered in the last severa
Stay tuned.
Todd Sundsted has been writing programs since computers became available in desktop models. Though
originally interested in building distributed object applications in C++, Todd moved to the Java programming
language when Java became the obvious choice for that sort of thing. In addition to writing, Todd provides
Internet and Web consulting services to companies in the southeastern United States. :END_BIO
https://www.javaworld.com/article/2077282/learn-java/using-the-graphics-class.html 7/7