当前位置:文档之家› 3D教案13.ppt

3D教案13.ppt


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).
Draws a rectangle with specified upper-left corner poinБайду номын сангаас 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.
4
The Graphics Class
You can draw strings, lines, rectangles, ovals, arcs, polygons, and polylines, using the methods in the Graphics class.
java.awt.Graphics
(x, y) (0, 0) (x, y) (0, 0)
Component c2 Component c1
c3’s coordinate system
c2’s coordinate system
c1’s coordinate system
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
X Axis
y Y Axis
(x, y)
Java Coordinate System
Y Axis
Conventional Coordinate System
(0, 0)
X Axis
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
3
Each GUI Component Has its Own Coordinate System
(c3.getX(), c3c.3g.egteYtX()())
(x, y) (0, 0)
Component c3
(c2.getX(), c2.getY())
(c1.getX(), c1.getY())
Chapter 13 Graphics
Chapter 12 GUI Basics Chapter 13 Graphics Chapter 14 Event-Driven Programming Chapter 15 Creating User Interfaces Chapter 16 Applets and Multimedia
§10.2, “Abstract Classes,” in Chapter 10 §10.4, “Interfaces,” in Chapter 10
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
2
Java Coordinate System
x (0, 0)
+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
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
相关主题