Sei sulla pagina 1di 7

2/23/2019 Using the Graphics class | JavaWorld

🔎
 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.

Let's begin at center stage -- the Graphics class.

The Graphics class


It's essential that programmers understand the Graphics class before they attempt to draw
images via Java. The Graphics class provides the framework for all graphics operations within
the AWT. It plays two different, but related, roles. First, it is the graphics context. The graphics
context is information that will affect drawing operations. This includes the background and
foreground colors, the font, and the location and dimensions of the clipping rectangle (the
region of a component in which graphics can be drawn). It even includes information about the
https://www.javaworld.com/article/2077282/learn-java/using-the-graphics-class.html 1/7
2/23/2019 Using the Graphics class | JavaWorld
eventual destination of the graphics operations themselves (screen or image). Second, the
 Sign In | Register
Graphics class provides methods for drawing simple geometric shapes, text, and images to the
graphics destination. All output to the graphics destination occurs via an invocation of one of
these methods.

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

boolean mouseDown(Event e, int x, int y) { selected_object.move(x, y); repaint(); }

Listing 1: Mouse-down event handler

The code in the mouseDown() event handler recalculates the position of an object in a display based on the positio

update()

public void update(Graphics g)


https://www.javaworld.com/article/2077282/learn-java/using-the-graphics-class.html 2/7
2/23/2019 Using the Graphics class | JavaWorld
 Sign In | Register
The update() method is called in response to a repaint() request, or in response to a portion of the component being

paint()

public void paint(Graphics g)

The paint() method is called from an update() method, and is responsible for actually draw

How components are repainted

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

Figure 1: Repaint browser

The Graphics coordinate system

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

Figure 2: The coordinate plane

The Graphics primitives

This section introduces methods for drawing lines, rectangles, ovals and arcs, and polygons. Since these

lines

void drawLine(int xBegin, int yBegin, int xEnd, int yEnd)

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.

Figure 3: Line drawing demonstration

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.

Figure 4: Rectangle drawing demonstration

ovals and arcs

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.

Figure 6: Oval and arc drawing demonstration

polygons

void drawPolygon(int xPoints[], int yPoints[], int nPoints) void drawPolygon(Polygon

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.

Figure 7: Polygon drawing demonstration

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

Learn more about this topic

The Java Class Graphics API


https://www.javaworld.com/article/2077282/learn-java/using-the-graphics-class.html 6/7
http://java.sun.com/products/JDK/CurrentRelease/api/java.awt.Graphics.html
2/23/2019 Using the Graphics class | JavaWorld
 (http://java.sun.com/products/JDK/CurrentRelease/api/java.awt.Graphics.html) Sign In | Register
Observer and Observable http://www.sun.com/javaworld/jw-10-1996/jw-10-howto.html (/javaworld/jw-10-
1996/jw-10-howto.html)
The effective user interface http://www.sun.com/javaworld/jw-09-1996/jw-09-userint.html (/javaworld/jw-09-
1996/jw-09-userint.html)
Java and event handling http://www.sun.com/javaworld/jw-08-1996/jw-08-event.html (/javaworld/jw-08-
1996/jw-08-event.html)
Introduction to the AWT http://www.sun.com/javaworld/jw-07-1996/jw-07-awt.html (/javaworld/jw-07-1996/jw-
07-awt.html)

Follow everything from JavaWorld    

Copyright © 2019 IDG Communications, Inc.

https://www.javaworld.com/article/2077282/learn-java/using-the-graphics-class.html 7/7

Potrebbero piacerti anche