13 JavaAWT OK
13 JavaAWT OK
no
user
Determine which Can widget
event
Wait for next event widget should handle the
receive event event?
yes
no
Does the
Application yes application have a Widget responds
responds to the
callback for the to the event
event
event?
Panel Window
FlowLayout BorderLayout
java.awt.peer.ButtonPeer
java.awt.Button << interface >>
sun.awt.windows.WButtonPeer sun.awt.mac.MButtonPeer
<< native >> << native >>
java.awt.Toolkit
<< abstract >> java.awt.peer.ButtonPeer
<< singleton >> << interface >>
+$ getDefaultToolkit() : Toolkit
+ createButton() : ButtonPeer = 0
+ createScrollBar() : ScrollbarPeer = 0
<< etc. >>
sun.awt.windows.WToolkit sun.awt.windows.WButtonPeer
<< creates >>
<< native >> << native >>
<<abstract>>
java.awt.AWTEvent
getSource() : Object
java.awt.event.KeyEvent java.awt.event.MouseEvent
0..*
<<abstract>>
java.awt.AWTEvent
0..*
<<interface>>
java.util.EventListener
keyPressed(KeyEvent e) mouseClicked(MouseEvent e)
actionPerformed(Event e)
... ...
import java.applet.Applet;
import java.awt.*;
The CenterText application paints text, centered inside a rectangle. Note that
g.drawRect() uses a different origin point than g.drawString().
In this example, the class CenterText itself implements the WindowListener interface,
to listen for the WindowClosing event.
origin
ascent
descent
leading
.Wy baseline
height
CenterText( ) {
super( "Center Text" );
addWindowListener( this );
setSize( 222, 111 ); // works OK for Frame
show( );
}
There are listeners for most Events, which dump interesting info to The Java Console
(found by navigating through the browser’s menus).
In this example, a separate class, WindowHandler, implements the WindowListener
interface, to listen for the WindowClosing event.
Note: Some LayoutManagers respect preferred sizes, while others “stretch” the
components to “fill” the available space.
Sometimes it is necessary to experiment, since LayoutMangers do not always do exactly
what you would want them to do...
<HTML>
<HEAD><TITLE> RandomColors - Applet Version
</TITLE></HEAD>
<APPLET code =
"awtExamples.randomColors.RandomColors.class"
width=300 height=300>
</APPLET>
</HTML>
java.awt.Frame java.applet.Applet
RandomColors
+$ main()
+ init()
WindowAdapter
DynamicLayout
+$ main() WindowHandler
ExitDialog
CircleSprite TriangleSprite SquareSprite
http://www.gui.net/chaos.html
screenGraphics scratchGraphics
: Graphics : Graphics
View
Display the UI
Determine User app.
Intent events
Controller
<< observes >>
Application
Logic and
Model "Glue"
model
Domain "Data" updates
and Business
Rules
conductGame()
getNextPlayer()
AWT getMove()
thread
returns
return
Click on some GUI Widget to indicate desired move
moveMade( move )
User moveMade( move )
submit( move )
This design
uses
recursion to isValidMove( move )
provide the
[ isValidMove = true ]
logical processMove( move )
equivalent
of a while()
[ gameOver = false ]
loop.
conductgame()
Which class should be responsible for listening for the ItemStateChanged event?
ComputerPlayer?
Referee?
SticksView$DifficultyListener?
choose
"Very Difficult" notify observers
User itemStateChanged( ie )
getItem()
Mediator - The Fractal class acts as the Mediator for the entire Fractal program.
Model View Controller – The Fractal class is the Controller; the Calculators and
Drawings are the Model; the ControlPanel and the DrawingCanvas make up the View.
This is not a “pure” MVC design (there is no class called “model”), but nevertheless,
the essence (intent) of MVC is respected by the separation of concerns across the
implementing classes.
Strategy - There are various FractalCalculators.
Template Method - FractalCalculator.getColor() calls the abstract method testPoint(),
which is implemented by MandelbrotCalculator and JuliaCalculator.
0..*
Fractal
Drawing
color : String
colorNumbers : int [] []
complexRect : ComplexRectangle
current image : java.awt.Image
maxIterations : int
zoom : java.awt.Rectangle
JuliaDrawing
HelpDrawing
juliaPoint
fractal.Drawing
java.awt.Button … javax.swing.JButton
java.awt.Canvas … javax.swing.JPanel
java.awt.Checkbox … javax.swing.JCheckBox
java.awt.Label … javax.swing.JLabel
java.awt.Choice … javax.swing.JList
java.awt.Panel … javax.swing.JPanel
java.awt.TextField … javax.swing.JTextField
...