Graphics in Java

Download Report

Transcript Graphics in Java

Graphics in Java
Dr. Andrew Wallace PhD BEng(hons) EurIng
[email protected]
Overview
• Events
• Graphics
• Swing
• Model – view – controller
Events
• Event driven
• Event handler
• Action Listener
Component
Event
Display
Events
State
Event
Event
State
Events
State
State
Events
switch(m_nState)
{
case STATE_A:
handleStateA();
case STATE_B:
handleStateB();
…
}
Events
private class MyEventListener implements ActionListener
//add listener
<component>.addActionListener(this);
public void actionPerformed(ActionEvent e)
{
…
}
Events
• Multiple listeners per event
• Multiple events per listener
• Need to test for event source
• EventObject has method getSource which returns an Object
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)
Events
• Not all interface events are of interest
• Adapters
• Implements listener’s methods with empty methods
• Inherited and use only those of interest
• Or use inner classes
Quiz
• What does an event signify in Java GUIs?
• How does event driven GUIs work?
Graphics
• AWT, Swing and JavaFX
• Abstract Windows Tool Kit
• Old version
• java.awt
• Swing
• Newer version
• More sophisticated
• javax.swing
• JavaFX
• Even newer!
Graphics
• Swing builds upon AWT
• public abstract class JComponent extends Container implements
Serializable
• System independent
• Written in Java
• Executes in own thread
• Restrictions on updating screen
• Revalidate and repaint
Parts of Swing
• Containers
• JFrame (extends awt Frame)
• JPanel (extends JComponent)
• JScrollPane (extends JComponent)
• Components
• Jbutton
• JTextArea
• JMenu
Containers
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 coords
Quiz
• How does swing differ from AWT?
• What are the two main parts of Swing?
Model – View - Controller
• Design method for GUIs
• Decouple data access / logic from display
• Model
• Data and rules
• View
• Display
• Controller
• Translate user input
Model – View - Controller
Change event
Controller
UI events
Model
View
Model change events
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 methods
Model – View - Controller
Controller
View
Model
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 decoupling
Model – View - Controller
public class MyModel extends AbstractModel
{
//Data
//Reset model
//Access methods
}
Model – View - Controller
public abstract class AbstractModel
{
protected PropertyChangeSupport m_PropChangSup;
//add property change listener
//remove property change listener
//fire property change listener
}
Model – View - Controller
public 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
}
Model – View - Controller
• Method method = model.getClass().getMethod(“set” +
m_strPropertyName, new Class[] {newValue.getClass()});
• method.invoke(model, newValue);
Model – View - Controller
public class DefaultController extends AbstractController
{
//change element functions
}
Model – View - Controller
public class MyView extends view
{
//set the controller
//model property change method
//event handlers. Send events to controller
}
Model – View - Controller
• Problem:
• Infinite loops
• Autonomous swing components can up date themselves
• Check values of update and current state
Quiz
• What does the parts of MVC do?
• How does MVC decouple objects?
Questions?