2 Graphics
2 Graphics
1
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
2
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
3
The Graphics Class
You can draw strings, java.awt.Graphics
lines, rectangles,
+setColor(color: Color): void Sets a new color for subsequent drawings.
+setFont(font: Font): void Sets a new font for subsequent drwings.
ovals, arcs, polygons, +drawString(s: String, x: int, y: int): void 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).
and polylines, using +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.
the methods in the +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.
Graphics class.
+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 Draws a filled polygon defined by a Polygon object.
+drawPolyline(xPoints: int[], yPoints: int[], Draws a polyline defined by arrays of x and y coordinates.
nPoints: int): void Each pair of (x[i], y[i]) coordinates is a point.
4
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();
You can then apply the methods in the Graphics class to draw things on the
label’s graphics context.
5
A Drawing Example
(0, 0)
(50, 50)
TestGetGraphics
6
The paintComponent Method
The paintComponent method is defined in JComponent, and
its header is as follows:
7
Drawing Geometric Figures
Drawing Strings
Drawing Lines
Drawing Rectangles
Drawing Ovals
Drawing Arcs
Drawing Polygons
8
Drawing Strings
(x1, y1)
drawString(String s, int x, int y); drawLine(int x1, int y1, int x2, int y2);
9
Drawing Rectangles
10
Drawing Rounded Rectangles
11
Drawing Ovals
drawOval(int x, int y, int w, int h);
fillOval(int x, int y, int w, int h);
12
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).
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
14
Drawing Arcs
Angles are in
degree
15
Drawing Arcs Example
DrawArcs
16
Drawing Polygons and Polylines
17
Drawing Polygons Using the Polygon Class
18
Drawing Polygons Example
DrawPolygon
19
Case Study: StillClock
+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.
StillClock DisplayClock
20
Drawing Clock
second (2/60)
21
Drawing Clock, cont.
22
Drawing Clock, cont.
xEnd = xCenter + handLength sin()
yEnd = yCenter - handLength cos()
23