Professional Documents
Culture Documents
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Motivations
If you want to draw shapes such as a bar chart, a clock, or a stop sign, how do you do it?
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Objectives
To describe Java coordinate systems in a GUI component (15.2). To draw things using the methods in the Graphics class (15.3). To override the paintComponent method to draw things on a GUI component (15.3). To use a panel as a canvas to draw things (15.3). To draw strings, lines, rectangles, ovals, arcs, and polygons (15.4, 15.6-15.7). To obtain font properties using FontMetrics and know how to center a message (15.8). To display an image in a GUI component (15.11). To develop reusable GUI components FigurePanel, MessagePanel, StillClock, and ImageViewer (15.5, 15.9, 15.10, 15.12).
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
y (x, y) Java Coordinate System Y Axis Conventional Coordinate System (0, 0) X Axis
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Component c2 Component c1
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
You can draw strings, lines, rectangles, ovals, arcs, polygons, and polylines, using the methods in the Graphics class.
+setColor(color: Color): void +setFont(font: Font): void +drawString(s: String, x: int, y: int): void
Sets a new color for subsequent drawings. Sets a new font for subsequent drwings.
Draws a string starting at point (x, y). +drawLine(x1: int, y1: int, x2: int, y2: int): void Draws a line from (x1, y1) to (x2, y2). +drawRect(x: int, y: int, w: int, h: int): void Draws a rectangle with specified upper-left corner point at (x, y) and width w and height h. +fillRect(x: int, y: int, w: int, h: int): void Draws a filled rectangle with specified upper-left corner point at (x, y) and width w and height h. +drawRoundRect(x: int, y: int, w: int, h: int, aw: Draws a round-cornered rectangle with specified arc width aw int, ah: int): void and arc height ah. +fillRoundRect(x: int, y: int, w: int, h: int, aw: Draws a filled round-cornered rectangle with specified arc int, ah: int): void width aw and arc height ah. +draw3DRect(x: int, y: int, w: int, h: int, raised: Draws a 3-D rectangle raised above the surface or sunk into the boolean): void surface. +fill3DRect(x: int, y: int, w: int, h: int, raised: Draws a filled 3-D rectangle raised above the surface or sunk boolean): void into the surface. +drawOval(x: int, y: int, w: int, h: int): void Draws an oval bounded by the rectangle specified by the parameters x, y, w, and h. +fillOval(x: int, y: int, w: int, h: int): void Draws a filled oval bounded by the rectangle specified by the parameters x, y, w, and h. +drawArc(x: int, y: int, w: int, h: int, startAngle: Draws an arc conceived as part of an oval bounded by the int, arcAngle: int): void rectangle specified by the parameters x, y, w, and h. +fillArc(x: int, y: int, w: int, h: int, startAngle: Draws a filled arc conceived as part of an oval bounded by the int, arcAngle: int): void rectangle specified by the parameters x, y, w, and h. +drawPolygon(xPoints: int[], yPoints: int[], Draws a closed polygon defined by arrays of x and y nPoints: int): void coordinates. Each pair of (x[i], y[i]) coordinates is a point. +fillPolygon(xPoints: int[], yPoints: int[], Draws a filled polygon defined by arrays of x and y nPoints: int): void coordinates. Each pair of (x[i], y[i]) coordinates is a point. +drawPolygon(g: Polygon): void Draws a closed polygon defined by a Polygon object. +fillPolygon(g: Polygon): void +drawPolyline(xPoints: int[], yPoints: int[], nPoints: int): void Draws a filled polygon defined by a Polygon object. Draws a polyline defined by arrays of x and y coordinates. Each pair of (x[i], y[i]) coordinates is a point.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
paintComponent Example
In order to draw things on a component, you need to define a class that extends JPanel and overrides its paintComponent method to specify what to draw. The first program in this chapter can be rewritten using paintComponent.
TestPaintComponent
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Run
7
Drawing Strings
Drawing Lines Drawing Rectangles
Drawing Ovals
Drawing Arcs Drawing Polygons
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Drawing Strings
(0, 0) (getWidth(), 0) (0, 0) (x1, y1) (x, y) (getWidth(), 0)
s is display here
(x2, y2) (getWidth(), getHeight()) (getWidth(), getHeight())
(0, getHeight())
(0, getHeight())
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Drawing Rectangles
drawRect(int x, int y, int w, int h);
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
10
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
11
Drawing Ovals
drawOval(int x, int y, int w, int h); fillOval(int x, int y, int w, int h);
(x, y)
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
12
+RECTANGLE = 2 +ROUND_RECTANGLE = 3 +OVAL = 4 -type: int -filled: boolean +FigurePanel() +FigurePanel(type: int) +FigurePanel(type: int, filled: boolean) +getType(): int
Specifies the figure type (default: 1). Specifies whether the figure is filled (default: false). Creates a default figure panel. Creates a figure panel with the specified type. Creates a figure panel with the specified type and filled property. Returns the figure type. Sets a new figure type. Checks whether the figure is filled with a color. Sets a new filled property.
FigurePanel
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
13
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
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Run
14
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);
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
15
DrawArcs
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Run
16
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
17
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
18
DrawPolygon
Run
19
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
getLeading()
getHeight()
getAscent()
getDescent()
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
20
21
panel
Welcome to Java
getWidth()
TestCenterMessage
Run
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
22
The get and set methods for these data fields are provided in the class, but omitted in the UML diagram
The x-Coordinate for the message (default 20). The y-Coordinate for the message (default 20). Specifies whether the message is displayed centered. The message to be displayed. The interval to move the message in the panel. Constructs a default message panel. Constructs a message panel with a specified string. Moves the message to the left. Moves the message to the right. Moves the message up. Moves the message down.
-yCoordinate: int -centered: boolean -message: String -interval: int +MessagePanel() +MessagePanel(message: String) +moveLeft(): void +moveRight(): void +moveUp(): void +moveDown(): void
This case study develops a useful class that displays a message in a panel. The class enables the user to set the location of the message, center the message, and move the message with the specified interval.
MessagePanel
TestMessagePanel
Run
23
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
The get and set methods for these data fields are provided in the class, but omitted in the UML diagram
The hour in the clock. The minute in the clock. The second in the clock. Constructs a default clock for the current time.
+StillClock(hour: int, minute: int, Constructs a clock with a specified time. second: int) Sets time to current time. +setCurrentTime(): void
StillClock
DisplayClock
Run
24
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Drawing Clock
xEnd = xCenter + handLength sin() Since there are sixty seconds in one minute, the angle for yEnd = yCenter - handLength cos() the second hand is second (2/60)
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
25
26
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
27
28
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, bgcolor: Color, observer: ImageObserver): void Draws the image in a specified location. The image's top-left corner is at (x, y) in the graphics context's coordinate space. Transparent pixels in 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. Same as the preceding method except that it does not specify a background color. Draws a scaled version of the image that can fill all of the available space in the specified rectangle.
+drawImage(image: Image, x: int, y: int, observer: ImageObserver): void +drawImage(image: Image, x: int, y: int, width: int, height: int, observer: 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, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
29
DisplayImage
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Run
30
The get and set methods for these data fields are provided in the class, but omitted in the UML diagram for brevity.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
ImageView Example
This example gives an example that creates six images using the ImageViewer class.
SixFlags
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Run
32