Programming
Chapter 12 GUI Basics
Chapter 13 Graphics
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
Objectives
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
Taste of Event-Driven
Programming
SimpleEventDemo
Run
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
Events
An
The
Event Classes
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
Event Information
An event object contains whatever properties are
pertinent to the event. You can identify the source
object of the event using the getSource() instance
method in the EventObject class. The subclasses of
EventObject deal with special types of events,
such as button actions, window events, component
events, mouse movements, and keystrokes. Table
14.1 lists external user actions, source objects, and
event types generated.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
Source
Object
Event Type
Generated
Click a button
JButton
ActionEvent
JCheckBox
ItemEvent, ActionEvent
JRadioButton
ItemEvent, ActionEvent
JTextField
ActionEvent
JComboBox
ItemEvent, ActionEvent
Window
WindowEvent
Component
MouseEvent
Component
KeyEvent
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
source: SourceClass
XListener
+addXListener(listener: XListener)
+handler(event: XEvent)
Register by invoking
source.addXListener(listener);
listener: ListenerClass
source: JButton
ActionListener
+addActionListener(listener: ActionListener)
+actionPerformed(event: ActionEvent)
Register by invoking
source.addActionListener(listener);
listener: CustomListenerClass
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
source: JButton
+addXListener(XListener listener)
An event is
triggered
event: XEvent
Invoke
listener1.handler(event)
listener2.handler(event)
listenern.handler(event)
+addActionListener(ActionListener listener)
Keep it a list
An event is
triggered
listener1
listener2
listenern
event:
ActionEvent
Keep it a list
Invoke
listener1.actionPerformed(event)
listener2.actionPerformed(event)
listenern.actionPerformed(event)
listener1
listener2
listenern
+handler(
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
+handler(
10
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
11
Listener Interface
ActionEvent
ItemEvent
WindowEvent
ActionListener
ItemListener
WindowListener
ContainerEvent
ContainerListener
MouseEvent
MouseListener
KeyEvent
KeyListener
actionPerformed(ActionEvent)
itemStateChanged(ItemEvent)
windowClosing(WindowEvent)
windowOpened(WindowEvent)
windowIconified(WindowEvent)
windowDeiconified(WindowEvent)
windowClosed(WindowEvent)
windowActivated(WindowEvent)
windowDeactivated(WindowEvent)
componentAdded(ContainerEvent)
componentRemoved(ContainerEvent)
mousePressed(MouseEvent)
mouseReleased(MouseEvent)
mouseClicked(MouseEvent)
mouseExited(MouseEvent)
mouseEntered(MouseEvent)
keyPressed(KeyEvent)
keyReleased(KeyEvent)
keyTypeed(KeyEvent)
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
12
java.awt.event.ActionEvent
java.util.EventObject
+getSource(): Object
java.awt.event.AWTEvent
java.awt.event.ActionEvent
+getActionCommand(): String
+getModifiers(): int
Returns the modifier keys held down during this action event.
+getWhen(): long
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
13
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
14
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.
15
public class A {
...
}
(a)
// Inner class
public class A {
...
}
(b)
// An inner class
class InnerClass {
/** A method in the inner class */
public void mi() {
// Directly reference data and method
// defined in its outer class
data++;
m();
}
}
(c)
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
16
17
18
Revising SimpleEventDemo
Using Inner Classes
SimpleEventDemoInnerClass
Run
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
19
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
20
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
21
Revising SimpleEventDemo
Using Anonymous Inner Classes
SimpleEventDemoAnonymousInnerClass
Run
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
22
23
jbtOK: JButton
jbtCancel: JButton
btListener: ButtonListener
1. addActionListener
2. addActionListener
3. actionPerformed
4. actionPerformed
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
24
TestWindowEvent
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
Run
25
Run
26
MouseEvent
java.awt.event.InputEvent
+getWhen(): long
+isAltDown(): boolean
+isControlDown(): boolean
+isMetaDown(): boolean
+isShiftDown(): boolean
java.awt.event.MouseEvent
+getButton(): int
+getClickCount(): int
+getPoint(): java.awt.Point
+getX(): int
+getY(): int
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
27
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
28
Invoked when the mouse button has been clicked (pressed and
released) on the source component.
java.awt.event.MouseMotionListener
+mouseDragged(e: MouseEvent): void
+mouseMoved(e: MouseEvent): void
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
29
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.
MoveMessageDemo
Run
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
30
Example: (Omitted)
Handling Complex Mouse Events
Objective: Create a
program for drawing
using a mouse. Draw
by dragging with the
left mouse button
pressed; erase by
dragging with the
right button pressed.
ScribbleDemo
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
Run
31
keyPressed(KeyEvent e)
keyReleased(KeyEvent e)
keyTyped(KeyEvent e)
32
Methods:
getKeyChar() method
getKeyCode() method
Keys:
Home
End
Page Up
Page Down
etc...
VK_HOME
VK_END
VK_PGUP
VK_PGDN
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
33
+getKeyCode(): int
Returns the integer keyCode associated with the key in this event.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
34
Run
35
Optional
+addActionListener(listener:
ActionListener): void
+start(): void
+stop(): void
The Timer class can be used to control animations. For example, you
can use it to display a moving message.
AnimationDemo
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
Run
36
Clock Animation
In Chapter 12, you drew a StillClock to show the current
time. The clock does not tick after it is displayed. What can
you do to make the clock display a new current time every
second? The key to making the clock tick is to repaint it
every second with a new current time. You can use a timer
to control how to repaint the clock.
ClockAnimation
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
Run
37