Chapter 13 Graphics: Chapter 12 GUI Basics Abstract Classes," in Chapter 10
Chapter 13 Graphics: Chapter 12 GUI Basics Abstract Classes," in Chapter 10
Chapter 13 Graphics
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All righ
ts reserved. 0-13-222158-6 1
Objectives
To understand Java coordinate systems (§13.2).
To draw things using the methods in the Graphics class (§13.3).
To obtain a graphics context using the getGraphics() method
(§13.3).
To override the paintComponent method to draw things on a
graphical context (§13.4).
To use a panel as a canvas to draw things (§13.5).
To draw strings, lines, rectangles, ovals, arcs, and polygons
(§§13.6, 13.8-13.9).
To obtain font properties using FontMetrics and know how to
center a message (§13.10).
To display image in a GUI component (§13.13).
To develop reusable GUI components FigurePanel, MessagePanel,
StillClock, and ImageViewer (§§13.7, 13.11, 13.12, 13.14).
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All righ
ts reserved. 0-13-222158-6 2
Java Coordinate System
x
Y Axis
(0, 0) X Axis
y
(x, y)
(0, 0) X Axis
Java Coordinate Conventional
System Coordinate
System
Y Axis
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All righ
ts reserved. 0-13-222158-6 3
Each GUI Component Has its
Own Coordinate System
(c2.getX(), (x, y)
(0, 0) Component c2 c2’s coordinate
c2.getY())
system
(x, y)
(0, 0) Component c1
(c1.getX(), c1’s coordinate
c1.getY()) system
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All righ
ts reserved. 0-13-222158-6 4
The Graphics Class
java.awt.Graphics
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All righ
ts reserved. 0-13-222158-6 5
Obtaining Graphics Object
The Graphics class is an abstract class that provides a
device-independent graphics interface for displaying figures
and images on the screen on different platforms. Whenever a
component (e.g., a button, a label, a panel) is displayed, a
Graphics object is created for the component on the native
platform. This object can be obtained using the
getGraphics() method. For example, the graphics context for
a label object jlblBanner can be obtained using
Graphics graphics = jlblBanner.getGraphics();
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All righ
ts reserved. 0-13-222158-6 6
A Drawing Example
(0, 0)
(50, 50)
TestGetGraphics Run
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All righ
ts reserved. 0-13-222158-6 7
Two Problems With the Preceding Example
If you resize the frame, the line is gone.
It is awkward to program because you have to make
sure that the component to be displayed before
obtaining its graphics context using the getGraphics()
method. For this reason, Lines 20 and 21 are placed
after the frame is displayed in Line 17.
To fix the first problem, you need to know its cause. When you
resize the frame, the JVM invokes the paintComponent method
of a Swing component (e.g., a label) to redisplay the graphics
on the component. Since you did not draw a line in the
paintComponent method, the line is gone when the frame is
resized. To permanently display the line, you need to draw the
line in the paintComponent method.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All righ
ts reserved. 0-13-222158-6 8
The paintComponent Method
The paintComponent method is defined in JComponent,
and its header is as follows:
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All righ
ts reserved. 0-13-222158-6 9
paintComponent Example
In order to draw things on a component (e.g., a JLabel),
you need to declare a class that extends a Swing GUI
component class and overrides its paintComponent method
to specify what to draw. The first program in this chapter
can be rewritten using paintComponent.
TestPaintComponent Run
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All righ
ts reserved. 0-13-222158-6 10
Drawing on Panels
JPanel can be used to draw graphics (including text) and
enable user interaction.
TestPanelDrawing Run
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All righ
ts reserved. 0-13-222158-6 11
Drawing Geometric Figures
Drawing Strings
Drawing Lines
Drawing Rectangles
Drawing Ovals
Drawing Arcs
Drawing Polygons
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All righ
ts reserved. 0-13-222158-6 12
Drawing Strings
(x1, y1)
drawString(String s, int x, int y); drawLine(int x1, int y1, int x2, int y2);
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All righ
ts reserved. 0-13-222158-6 13
Drawing Rectangles
drawRect(int x, int y, int w, int h);
fillRect(int x, int y, int w, int h);
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All righ
ts reserved. 0-13-222158-6 14
Drawing Rounded Rectangles
drawRoundRect(int x, int y, int w, int h, int aw, int ah);
fillRoundRect(int x, int y, int w, int h, int aw, int ah);
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All righ
ts reserved. 0-13-222158-6 15
Drawing Ovals
drawOval(int x, int y, int w, int h);
fillOval(int x, int y, int w, int h);
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All righ
ts reserved. 0-13-222158-6 16
Case Study: The FigurePanel Class
This example develops a useful class for displaying various
figures. The class enables the user to set the figure type and
specify whether the figure is filled, and displays the figure on a
panel. javax.swing.JPanel
-char token
+getToken FigurePanel
+setToken
+paintComponet LINE, RECTANGLE,
+LINE = 1
+mouseClicked ROUND_RECTANGLE, and OVAL are
+RECTANGLE = 2 constants.
+ROUND_RECTANGLE = 3
+OVAL = 4
-type: int Specifies the figure type (default: 1).
-filled: boolean Specifies whether the figure is filled (default: false).
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All righ
ts reserved. 0-13-222158-6 17
Test FigurePanel
This example develops a useful class for displaying various
figures. The class enables the user to set the figure type and
specify whether the figure is filled, and displays the figure on a
panel.
TestFigurePanel Run
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All righ
ts reserved. 0-13-222158-6 18
Drawing Arcs
drawArc(int x, int y, int w, int h, int angle1, int angle2);
fillArc(int x, int y, int w, int h, int angle1, int angle2);
Angles are in
degree
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All righ
ts reserved. 0-13-222158-6 19
Drawing Arcs Example
DrawArcs Run
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All righ
ts reserved. 0-13-222158-6 20
Drawing Polygons and Polylines
int[] x = {40, 70, 60, 45, 20};
int[] y = {20, 40, 80, 45, 60}; g.drawPolyline(x, y, x.length);
g.drawPolygon(x, y, x.length);
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All righ
ts reserved. 0-13-222158-6 21
Drawing Polygons Using the
Polygon Class
Polygon polygon = new Polygon();
polygon.addPoint(40, 59);
polygon.addPoint(40, 100);
polygon.addPoint(10, 100);
g.drawPolygon(polygon);
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All righ
ts reserved. 0-13-222158-6 22
Drawing Polygons Example
DrawPolygon Run
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All righ
ts reserved. 0-13-222158-6 23
Centering Display Using the FontMetrics Class
You can display a string at any location in a panel. Can you display
it centered? To do so, you need to use the FontMetrics class to
measure the exact width and height of the string for a particular
font. A FontMetrics can measure the following attributes:
public int getAscent() public int getHeight()
public int getDescent() public int stringWidth(String str)
public int getLeading()
getLeading()
getHeight()
getAscent()
getDescent()
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All righ
ts reserved. 0-13-222158-6 24
The FontMetrics Class
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All righ
ts reserved. 0-13-222158-6 25
panel
stringWidth
getHeight()
stringAscent Welcome to Java
getWidth()
TestCenterMessage Run
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All righ
ts reserved. 0-13-222158-6 26
Case Study: MessagePanel
javax.swing.JPanel
The get and set methods for these data This case study
fields are provided in the class, but
-char token
omitted in the UML diagram develops a useful class
+getToken MessagePanel
+setToken
that displays a message
+paintComponet
-xCoordinate: int
+mouseClicked The x-Coordinate for the message (default 20). in a panel. The class
-yCoordinate: int
-centered: boolean
The y-Coordinate for the message (default 20).
Specifies whether the message is displayed centered.
enables the user to set
-message: String The message to be displayed. the location of the
-interval: int The interval to move the message in the panel.
message, center the
+MessagePanel()
+MessagePanel(message: String)
Constructs a default message panel.
Constructs a message panel with a specified string.
message, and move the
+moveLeft(): void Moves the message to the left. message with the
+moveRight(): void Moves the message to the right.
+moveUp(): void Moves the message up. specified interval.
+moveDown(): void Moves the message down.
+getToken StillClock
+setToken
+paintComponet
-hour: int
+mouseClicked The hour in the clock.
-minute: int The minute in the clock.
-second: int The second in the clock.
second (2/60)
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All righ
ts reserved. 0-13-222158-6 29
Drawing Clock, cont.
xEnd = xCenter + handLength sin()
yEnd = yCenter - handLength cos()
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All righ
ts reserved. 0-13-222158-6 31
Displaying Image Icons
You learned how to create image icons and display image icons in
labels and buttons. For example, the following statements create an
image icon and display it in a label:
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All righ
ts reserved. 0-13-222158-6 32
Displaying Images
Using a label as an area for displaying images is simple and
convenient, but you don't have much control over how the image is
displayed. A more flexible way to display images is to use the
drawImage method of the Graphics class on a panel. Four versions
of the drawImage method are shown here.
java.awt.Graphics
+drawImage(image: Image, x: int, y: int, Draws the image in a specified location. The image's top-left corner is at
bgcolor: Color, observer: (x, y) in the graphics context's coordinate space. Transparent pixels in
ImageObserver): void the image are drawn in the specified color bgcolor. The observer is the
object on which the image is displayed. The image is cut off if it is
larger than the area it is being drawn on.
+drawImage(image: Image, x: int, y: int, Same as the preceding method except that it does not specify a background
observer: ImageObserver): void color.
+drawImage(image: Image, x: int, y: int, Draws a scaled version of the image that can fill all of the available space
width: int, height: int, observer: in the specified rectangle.
ImageObserver): void
+drawImage(image: Image, x: int, y: int, Same as the preceding method except that it provides a solid background
width: int, height: int, bgcolor: Color, color behind the image being drawn.
observer: ImageObserver): void
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All righ
ts reserved. 0-13-222158-6 33
Displaying Images Example
This example gives the code that displays an image from
image/us.gif. The file image/us.gif is under the class directory. The
Image from the file is created in the program. The drawImage
method displays the image to fill in the whole panel, as shown in the
figure.
DisplayImage Run
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All righ
ts reserved. 0-13-222158-6 34
Case Study: ImageViewer Class
Displaying an image is a common task in Java programming. This
case study develops a reusable component named ImageViewer that
displays an image in a panel. The ImageViewer class contains the
properties image, imageFilename, stretched, xCoordinate, and
yCoordinate.
javax.swing.JPanel The get and set methods for these data
fields are provided in the class, but
omitted in the UML diagram for brevity.
ImageViewer
-image: Image Image in the image viewer.
-stretched: boolean True if the image is stretched in the viewer.
-xCoordinate: int x-coordinate of the upper-left corner of the image in the viewer.
-yCoordinate: int y-coordinate of the upper-left corner of the image in the viewer.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All righ
ts reserved. 0-13-222158-6 35
ImageView Example
This example gives an example that creates six images using the
ImageViewer class.
SixFlags Run
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All righ
ts reserved. 0-13-222158-6 36