Chapter 14, 15, 16
Agenda
✦
Graphics
–
Coordinate system
–
Graphics Class
–
paintComponent Method
✦
Events
–
Event-Driven Programming
–
Inner Classes
✦
GUI Applications
–
Buttons, Checkboxes, Radiobuttons, and Textfields
Java Coordinate System
(0, 0)
X Axis
Y Axis
(x, y)
x
y
Java Coordinate
System
X Axis
Conventional
Coordinate
System
Each GUI Component Has its
Own Coordinate System
(0, 0)
Component c2
Component c1
(0, 0)
(0, 0)
(x1, y1)
(x2, y2)
(x3, y3)
Component c3
c3’s coordinate
system
c2’s coordinate
system
The Graphics Class
You can draw strings,
lines, rectangles,
ovals, arcs, polygons,
and polylines, using
the methods in the
Graphics class.
java.awt.Graphics
+setColor(color: Color): void +setFont(font: Font): void
+drawString(s: String, x: int, y: int): void +drawLine(x1: int, y1: int, x2: int, y2: int): void +drawRect(x: int, y: int, w: int, h: int): void
+fillRect(x: int, y: int, w: int, h: int): void
+drawRoundRect(x: int, y: int, w: int, h: int, aw: int, ah: int): void
+fillRoundRect(x: int, y: int, w: int, h: int, aw: int, ah: int): void
+draw3DRect(x: int, y: int, w: int, h: int, raised: boolean): void
+fill3DRect(x: int, y: int, w: int, h: int, raised: boolean): void
+drawOval(x: int, y: int, w: int, h: int): void
+fillOval(x: int, y: int, w: int, h: int): void
+drawArc(x: int, y: int, w: int, h: int, startAngle: int, arcAngle: int): void
+fillArc(x: int, y: int, w: int, h: int, startAngle: int, arcAngle: int): void
+drawPolygon(xPoints: int[], yPoints: int[], nPoints: int): void
+fillPolygon(xPoints: int[], yPoints: int[], nPoints: int): void
+drawPolygon(g: Polygon): void +fillPolygon(g: Polygon): void
Sets a new color for subsequent drawings. Sets a new font for subsequent drwings. Draws a string starting at point (x, y). Draws a line from (x1, y1) to (x2, y2).
Draws a rectangle with specified upper-left corner point at (x, y) and width w and height h.
Draws a filled rectangle with specified upper-left corner point at (x, y) and width w and height h.
Draws a round-cornered rectangle with specified arc width aw and arc height ah.
Draws a filled round-cornered rectangle with specified arc width aw and arc height ah.
Draws a 3-D rectangle raised above the surface or sunk into the surface.
Draws a filled 3-D rectangle raised above the surface or sunk into the surface.
Draws an oval bounded by the rectangle specified by the parameters x, y, w, and h.
Draws a filled oval bounded by the rectangle specified by the parameters x, y, w, and h.
Draws an arc conceived as part of an oval bounded by the rectangle specified by the parameters x, y, w, and h. Draws a filled arc conceived as part of an oval bounded by the
rectangle specified by the parameters x, y, w, and h. Draws a closed polygon defined by arrays of x and y
coordinates. Each pair of (x[i], y[i]) coordinates is a point. Draws a filled polygon defined by arrays of x and y
The paintComponent Method
The paintComponent method is defined in JComponent,
and its header is as follows:
protected void paintComponent(Graphics g)
This method, defined in the JComponent class, is invoked
whenever the component is first displayed or
redisplayed. The Graphics object g is created
automatically by the JVM for every visible GUI
Drawing on Panels (p. 478)
✦
JPanel can be used to draw graphics (including text) and
enable user interaction.
✦
To draw in a panel, you create a new class that extends
JPanel and override the paintComponent method to tell
the panel how to draw things. You can then display
strings, draw geometric shapes, and view images on
the panel.
Drawing Geometric Figures
✦
Drawing Strings
✦
Drawing Lines
✦
Drawing Rectangles
✦
Drawing Ovals
✦
Drawing Arcs
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);
Drawing Arcs Example (p. 484)
Case Study: MessagePanel
MessagePanel
-xCoordinate: int
-yCoordinate: int
-centered: boolean
-message: String
-interval: int
+MessagePanel()
+MessagePanel(message: String)
+moveLeft(): void
+moveRight(): void
+moveUp(): void
+moveDown(): void
javax.swing.JPanel
-char token +getToken +setToken +paintComponet +mouseClickedThe 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.
MessagePanel
Run
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.
Procedural vs. Event-Driven
Programming
✦
Procedural programming
is executed in
procedural order.
✦
In event-driven programming, code is executed
Events
✦
An
event
can be defined as a type of signal to the
program that something has happened.
✦
The event is generated by external user actions such
as mouse movements, mouse clicks, and keystrokes,
or by the operating system, such as a timer.
✦
Listeners handle events
Inner Class Listeners
A listener class is designed specifically to
create a listener object for a GUI
component (e.g., a button). It will not be
shared by other applications. So, it is
Inner Classes
Inner class: A class is a member of another class.
Advantages: In some applications, you can use an
inner class to make programs simple.
✦
An inner class can reference the data and
methods defined in the outer class in which it
nests, so you do not need to pass the reference
of the outer class to the constructor of the inner
class.
Inner Classes, cont.
public class
Test {
...
}
public class
A {
...
}
public class
Test {
...
// Inner class
public class
A {
...
}
}
(a)
(b)
// OuterClass.java: inner class demo
public class
OuterClass {
private int
data;
/** A method in the outer class */
public
void
m() {
// Do something
}
// An inner class
class
InnerClass {
/** A method in the inner class */
public void
mi() {
// Directly reference data and method
// defined in its outer class
Inner Classes (cont.)
✦
Inner classes can make programs simple
and concise.
✦
An inner class supports the work of its
containing outer class and is compiled
into a class named
OuterClassName
$
InnerClassName
.class.
For example, the inner class InnerClass in
OuterClass is compiled into
Inner Classes (cont.)
✦
An inner class can be declared public,
protected, or private subject to the same
visibility rules applied to a member of the
class.
✦
An inner class can be declared static. A
static inner class can be accessed using the
outer class name. A static inner class
Anonymous Inner Classes
✦
An anonymous inner class must always extend a superclass or
implement an interface, but it cannot have an explicit extends or
implements clause.
✦
An anonymous inner class must implement all the abstract
methods in the superclass or in the interface.
✦
An anonymous inner class always uses the no-arg constructor
from its superclass to create an instance. If an anonymous inner
class implements an interface, the constructor is Object().
✦
An anonymous inner class is compiled into a class named
OuterClassName$n.class. For example, if the outer class Test
has two anonymous inner classes, these two classes are
Anonymous Inner Classes (cont.)
Inner class listeners can be shortened using anonymous
inner classes. An
anonymous inner class
is an inner
class without a name. It combines declaring an inner
class and creating an instance of the class in one step.
An anonymous inner class is declared as follows:
new
SuperClassName/InterfaceName() {
// Implement or override methods in superclass or interface
// Other methods if necessary
Example: Controlling Balls (p. 522)
ControlBall
Run
Example: Moving Message Using
Mouse (p. 528)
Objective: Create a
program to display
a message in a
panel. You can use
the mouse to move
the message. The
message moves as
the mouse drags
and is always
displayed at the
mouse point.
Example: Using Buttons (p. 546)
Write a program that displays a
message on a panel and uses
two buttons, <= and =>, to move
the message on the panel to the
left or right.
Run
ButtonDemo
Example: Using Check Boxes
(p. 549)
Add three check boxes named
Centered
,
Bold
, and
Italic
into Example 16.1 to let
the user specify whether
the message is centered,
bold, or italic.
CheckBoxDemo
Run
ButtonDemo
Example: Using Radio Buttons
(p. 552)
Add three radio buttons
named
Red
,
Green
,
and
Blue
into the
preceding example to
let the user choose the
color of the message.
Run
RadioButtonDemo
ButtonDemo
CheckBoxDemo
Example: Using Text Fields
(p. 555)
Add a text field to the
preceding example
to let the user set a
new message.
Run
TextFieldDemo
Summary of Lecture
✦
Graphics
–
Coordinate system
–
Graphics Class
–
paintComponent Method
✦
Events
–
Event-Driven Programming
–
Inner Classes
✦
GUI Applications
–
Buttons, Checkboxes, Radiobuttons, and Textfields