Chapter 12 - Graphics and Java 2D: 2003 Prentice Hall, Inc. All Rights Reserved
Chapter 12 - Graphics and Java 2D: 2003 Prentice Hall, Inc. All Rights Reserved
Chapter 12 - Graphics and Java 2D: 2003 Prentice Hall, Inc. All Rights Reserved
12.1 Introduction
12.1 Introduction
measured in pixels
+x
(0 , 0) X a xis
(x , y )
+y
Y a xis
• Graphics context
– Enables drawing on screen
– Graphics object manages graphics context
• Controls how information is drawn
– Class Graphics is abstract
• Cannot be instantiated
• Contributes to Java’s portability
– Class Component method paint takes Graphics object
public void paint( Graphics g )
– Called through method repaint
• Class Color
– Defines methods and constants for manipulating colors
– Colors are created from red, green and blue components
• RGB values
values
Graphics methods
Method Description
Color constructors and methods
ShowColors2.jav
a
• Class Font
– Contains methods and constants for font control
– Font constructor takes three arguments
• Font name
– Monospaced, SansSerif, Serif, etc.
• Font style
– Font.PLAIN, Font.ITALIC and Font.BOLD
• Font size
– Measured in points (1/72 of inch)
• Font metrics
– Height
– Descent (amount character dips below baseline)
– Ascent (amount character rises above baseline)
– Leading (difference between descent and ascent)
Xy1Õ
le a d in g
h e ig h t a sc e n t
b a se lin e
de sc e nt
Method Description
FontMetrics methods
Metrics.java
• Class Graphics
– Provides methods for drawing lines, rectangles and ovals
• All drawing methods require parameters width and height
Draws a line between the point (x1, y1) and the point (x2, y2).
public void drawRect( int x, int y, int width, int height )
Draws a rectangle of the specified width and height. The top-left corner of
the rectangle has the coordinates (x, y).
public void fillRect( int x, int y, int width, int height )
Draws a solid rectangle with the specified width and height. The top-left
corner of the rectangle has the coordinate (x, y).
public void clearRect( int x, int y, int width, int height )
Draws a solid rectangle with the specified width and height in the current
background color. The top-left corner of the rectangle has the coordinate (x, y).
public void drawRoundRect( int x, int y, int width, int height,
int arcWidth, int arcHeight )
Draws a rectangle with rounded corners in the current color with the specified
width and height. The arcWidth and arcHeight determine the rounding of
the corners (see Fig. 12.15).
public void fillRoundRect( int x, int y, int width, int height,
int arcWidth, int arcHeight )
Draws a solid rectangle with rounded corners in the current color with the
specified width and height. The arcWidth and arcHeight determine the
rounding of the corners (see Fig. 12.15).
Method Description
public void draw3DRect( int x, int y, int width, int height, boolean b )
Draws a three-dimensional rectangle in the current color with the specified
width and height. The top-left corner of the rectangle has the coordinates (x,
y). The rectangle appears raised when b is true and lowered when b is false.
public void fill3DRect( int x, int y, int width, int height, boolean b )
Draws a filled three-dimensional rectangle in the current color with the
specified width and height. The top-left corner of the rectangle has the
coordinates (x, y). The rectangle appears raised when b is true and lowered
when b is false.
public void drawOval( int x, int y, int width, int height )
Draws an oval in the current color with the specified width and height. The
bounding rectangle’s top-left corner is at the coordinates (x, y). The oval
touches all four sides of the bounding rectangle at the center of each side (see
Fig. 12.16).
public void fillOval( int x, int y, int width, int height )
Draws a filled oval in the current color with the specified width and height.
The bounding rectangle’s top-left corner is at the coordinates (x, y). The oval
touches all four sides of the bounding rectangle at the center of each side (see
Fig. 12.16).
( x, y)
a rc h e ig h t
a rc wid t h h e ig h t
w id th
( x , y)
h e ig h t
w id th
• Arc
– Portion of oval
– Measured in degrees
– Sweeps the number of degrees in arc angle
– Sweep starts at starting angle
• Counterclockwise sweep is measure in positive degrees
• Clockwise sweep is measure in negative degrees
180° 0° 180° 0°
270° 270°
Method Description
public void drawArc( int x, int y, int width, int height, int startAngle,
int arcAngle )
Draws a solid arc (i.e., a sector) relative to the bounding rectangle’s top-left
coordinates (x, y) with the specified width and height. The arc segment is
drawn starting at startAngle and sweeps arcAngle degrees.
• Class Polygon
– Polygons
• Multisided shapes
– Polylines
• Series of connected points
Method Description
Polygon constructors and methods
public Polygon()
Constructs a new polygon object. The polygon does not contain any points.
public Polygon( int xValues[], int yValues[], int numberOfPoints )
Constructs a new polygon object. The polygon has numberOfPoints sides,
with each point consisting of an x-coordinate from xValues and a y-coordinate
from yValues.
public void addPoint( int x, int y )
DrawPolygons.ja
va
• Java 2D API
– Provides advanced 2D graphics capabilities
• java.awt
• java.awt.image
• java.awt.color
• java.awt.font
• java.awt.geom
• java.awt.print
• java.awt.image.renderable
– Uses class java.awt.Graphics2D
• Extends class java.awt.Graphics
• Java 2D shapes
– Package java.awt.geom
• Ellipse2D.Double
• Rectangle2D.Double
• RoundRectangle2D.Double
• Arc3D.Double
• Lines2D.Double
Shapes2.java
Person JavaInterface
DoorListener
- ID : Integer
- moving : Boolean = true
- location : Location
+ doorOpened( doorEvent : DoorEvent ) : void
+ doorClosed( doorEvent : DoorEvent ) : void
+ doorOpened( ) : void
+ doorClosed( ) : void
Person
DoorListener
- ID : Integer
- moving : Boolean = true
- location : Location
+ doorOpened( ) : void
+ doorClosed( ) : void
Elevator
ElevatorShaft Person
ElevatorMoveListener
JavaInterface
ButtonListener