Dr Andrew Wallace PhD BEng hons EurIng andrewcsumuse Overview Events Graphics Swing Model view controller Events Event driven Event handler Action Listener Component Display ID: 548033
Download Presentation The PPT/PDF document "Graphics in Java" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Graphics in Java
Dr. Andrew Wallace PhD
BEng
(hons)
EurIng
andrew@cs.umu.seSlide2
Overview
Events
Graphics
Swing
Model – view – controller Slide3
Events
Event driven
Event handler
Action Listener
Component
Display
EventSlide4
Events
State
State
Event
EventSlide5
Events
State
StateSlide6
Events
switch(
m_nState
)
{
c
ase STATE_A:
handleStateA
();case
STATE_B: handleStateB
();…
}Slide7
Events
private class
MyEventListener
implements
ActionListener
//add listener
<component>.
addActionListener
(this);
public void actionPerformed(
ActionEvent e){
…
}Slide8
Events
Multiple listeners per event
Multiple events per listener
Need to test for event source
EventObject
has method
getSource which returns an ObjectSlide9
Events
Design event listeners to be fast
Minimise operations
Execute in update thread
Event types:
Low level (mouse or key events)
Semantic (all other events)Slide10
Events
Not all interface events are of interest
Adapters
Implements listener’s methods with empty methods
Inherited and
use
only those of interestOr use inner classesSlide11
Quiz
What does an event signify in Java GUIs?
How does event driven GUIs work?Slide12
Graphics
AWT, Swing and
JavaFX
Abstract Windows Tool Kit
Old version
j
ava.awt
Swing
Newer versionMore sophisticated
javax.swingJavaFX
Even newer!Slide13
Graphics
Swing builds upon AWT
public abstract class
JComponent
extends Container implements
Serializable
System independent
Written in JavaExecutes in own thread
Restrictions on updating screenRevalidate and repaintSlide14
Parts of Swing
Containers
JFrame
(extends
awt
Frame)
JPanel (extends
JComponent)
JScrollPane (extends JComponent
)ComponentsJbutton
JTextArea
JMenu
ContainersSlide15
Parts of Swing
Layout managers
BoxLayout
(swing)
Allows for components to be laid out vertically or horizontally
BoarderLayout
(awt
)North, south, east, west and centre
GridLayout (awt
)Grid squares with coordsSlide16
Quiz
How does swing differ from AWT?
What are the two main parts of Swing?Slide17
Model – View - Controller
Design method for GUIs
Decouple data access / logic from display
Model
Data and rules
View
DisplayController
Translate user input Slide18
Model – View - Controller
Controller
Model
View
UI events
Change event
Model change eventsSlide19
Model – View - Controller
Implementation
Viewer registers as a listener on the model
Push model
Model doesn’t know of the viewer (just broadcasts events)
Bind the controller to the model
User actions invoke controller methodsSlide20
Model – View - Controller
Controller
Model
ViewSlide21
Model – View - Controller
Implementation
Viewer registers with the controller as a listener on the model
Events sent through the controller
Bind the controller to the model
User actions invoke controller methods
Increases the decouplingSlide22
Model – View - Controller
public class
MyModel
extends
AbstractModel
{
//Data
//Reset model
//Access methods}Slide23
Model – View - Controller
p
ublic abstract class
AbstractModel
{
protected
PropertyChangeSupport
m_PropChangSup;
//add property change listener //remove property change listener
//fire property change listener
}Slide24
Model – View - Controller
p
ublic abstract class
AbstractController
implements
PropertyChangeListener
{
//arrays holding views and models
//add / remove model
//add / remove view
//property change method to send event
//set model property method}Slide25
Model – View - Controller
Method
method
=
model.getClass
().
getMethod(“set” +
m_strPropertyName, new Class[] {newValue.getClass
()});
method.invoke(model, newValue
);Slide26
Model – View - Controller
public class
DefaultController
extends
AbstractController
{
//change element functions}Slide27
Model – View - Controller
public class
MyView
extends view
{
//set the controller
//model property change method //event handlers. Send events to controller
}Slide28
Model – View - Controller
Problem:
Infinite loops
Autonomous swing components can up date themselves
Check values of update and current stateSlide29
Quiz
What does the parts of MVC do?
How does MVC decouple objects?Slide30
Questions?