JAVA
JAVA
Advanced Graphics
MortgageApplet
Run Applet Viewer
Writing Applets
DisplayMessage
Run Applet Viewer
Similarities
SincetheybotharesubclassesoftheContainer
class,alltheuserinterfacecomponents,
layoutmanagers,andeventhandlingfeatures
arethesameforbothclasses.
Differences
ApplicationsareinvokedbytheJava
interpreter,andappletsareinvokedbytheWeb
browser.
Appletshavesecurityrestrictions
Webbrowsercreatesgraphicalenvironmentfor
applets,GUIapplicationsareplacedina
frame.
Conversions Between
Applications and Applets
Example 12.3
Running a Program as an Applet
and as an Application
Run as Applet
Example 12.4
Moving Message Using Mouse
apanel.Youcanusethemousetomovethe
message.Themessagemovesasthemouse
dragsandisalwaysdisplayedatthemouse
point.
MoveMessageDemo
Run as Application
Run as Applet
Example 12.5
Handling Complex Mouse Events
keyPressed(KeyEvent e)
keyReleased(KeyEvent e)
keyTyped(KeyEvent e)
Methods:
getKeyChar() method
getKeyCode() method
Keys:
Home
EndVK_End
Page Up
Page Down
etc...
VK_HOME
VK_PGUP
VK_PGDN
Example 12.6
Keyboard Events Demo
Sequence diagrams
Sequencediagramsdescribeinteractions
amongobjectsbydepictingthetime
orderingofmethodinvocations.
Class role
anObject:
TheClass
aMethod()
anotherObject:
Method Invocation
Activation
Method Invocation
anotherMethod()
TheOtherClass
ButtonDemo
messagePanel:
setXCoordinate
repaint
MessagePanel
Statechart diagrams
Statechartdiagramsdescribe
flowofcontroloftheobject.
Indicate
Initial State
State1
Transition
State2
stop()
stop()
Started
Stopped
start()
destroy()
Destroyed
Example 12.7
The TicTacToe Game
JApplet
JPanel
-char token
-char token
+getToken
TicTacToe
+setToken
+paintComponet
-whoseTurn: char
+mouseClicked
-cell Cell[][] = new Cell[3][3]
-JLabel jlblStatus: JLabel
+init(): void
+isFull(): boolean
+isWon(): boolean
TicTacToe
MouseListener
+getToken
+setToken
+paintComponet
-token: char
+mouseClicked
-char token
Cell
+getToken
+setToken
+paintComponet
+mouseClicked
+getToken():char
+setToken(token: char): void
+paintComponet(g: Graphics): void
+mouseClicked(e: MouseEvent): void
Run as Application
Run as Applet
Advanced Layout
(Optional from here on)
CardLayout
GridBagLayout
Using
No Layout Manager
EventAdapters
(Optional)
EventAdapters
(Optional)
Standard
adapters
Anonymous adapters
CardLayout
void first(container)
void last(container)
void next(container)
void previous(container)
Example 12.10
Testing CardLayout Manager
GridBagLayout
The GridBagLayout manager is the most flexible and
the most complex. It is similar to the GridLayout
manager in the sense that both layout managers arrange
components in a grid. The components can vary in size,
however, and can be added in any order in
GridBagLayout.
Run
Run