- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
6
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.
+drawPolyline(xPoints: int[], yPoints: int[], nPoints: int): void
Draws a polyline defined by arrays of x and y coordinates. Each pair of (x[i], y[i]) coordinates is a point.
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
4
Each GUI Component Has its Own Coordinate System
(x3, y3) (0, 0)
Component c3
(x2, y2) (0, 0) (x1, y1) (0, 0)
Component c2 Component c1
c3’s coordinate system
+setColor(color: Color): void
Sets a new color for subsequent drawings.
+setFont(font: Font): void
Sets a new font for subsequent drwings.
+drawString(s: String, x: int, y: int): void
+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
TestPaintComponent
Run
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
7
Drawing Geometric Figures
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
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.
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
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
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
c2’s coordinate system
c1’s coordinate system
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
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
Chapter 15 Graphics
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
1
Motivations
5
The Graphics Class
You can draw strings, lines, rectangles, ovals, arcs, polygons, and polylines, using the methods in the Graphics class.
java.awt.Graphics
+fillPolygon(xPoints: int[], yPoints: int[], nPoints: int): void
Draws a filled polygon defined by arrays of x and y coordinates. Each pair of (x[i], y[i]) coordinates is a point.
X Axis
y Y Axis
(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
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
3
Java Coordinate System
x (0, 0)
+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
+drawPolygon(g: Polygon): void
Draws a closed polygon defined by a Polygon object.
+fillPolygon(g: Polygon): void