Presentation is loading. Please wait.

Presentation is loading. Please wait.

Graphics in Java Dr. Andrew Wallace PhD BEng(hons) EurIng

Similar presentations


Presentation on theme: "Graphics in Java Dr. Andrew Wallace PhD BEng(hons) EurIng"— Presentation transcript:

1 Graphics in Java Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se

2 Overview Events Graphics Swing Model – view – controller

3 Events Event driven Event handler Action Listener Component Display Event

4 Events State Event

5 Events State

6 Events switch(m_nState) { case STATE_A: handleStateA(); case STATE_B: handleStateB(); … }

7 Events private class MyEventListener implements ActionListener //add listener.addActionListener(this); public void actionPerformed(ActionEvent e) { … }

8 Events Multiple listeners per event Multiple events per listener Need to test for event source EventObject has method getSource which returns an Object

9 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)

10 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

11 Quiz What does an event signify in Java GUIs? How does event driven GUIs work?

12 Graphics AWT, Swing and JavaFX Abstract Windows Tool Kit Old version java.awt Swing Newer version More sophisticated javax.swing JavaFX Even newer!

13 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

14 Parts of Swing Containers JFrame (extends awt Frame) JPanel (extends JComponent) JScrollPane (extends JComponent) Components Jbutton JTextArea JMenu Containers

15 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

16 Quiz How does swing differ from AWT? What are the two main parts of Swing?

17 Model – View - Controller Design method for GUIs Decouple data access / logic from display Model Data and rules View Display Controller Translate user input

18 Model – View - Controller Controller Model View UI events Change event Model change events

19 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

20 Model – View - Controller Controller ModelView

21 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

22 Model – View - Controller public class MyModel extends AbstractModel { //Data //Reset model //Access methods }

23 Model – View - Controller public abstract class AbstractModel { protected PropertyChangeSupport m_PropChangSup; //add property change listener //remove property change listener //fire property change listener }

24 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 }

25 Model – View - Controller Method method = model.getClass().getMethod(“set” + m_strPropertyName, new Class[] {newValue.getClass()}); method.invoke(model, newValue);

26 Model – View - Controller public class DefaultController extends AbstractController { //change element functions }

27 Model – View - Controller public class MyView extends view { //set the controller //model property change method //event handlers. Send events to controller }

28 Model – View - Controller Problem: Infinite loops Autonomous swing components can up date themselves Check values of update and current state

29 Quiz What does the parts of MVC do? How does MVC decouple objects?

30 Questions?


Download ppt "Graphics in Java Dr. Andrew Wallace PhD BEng(hons) EurIng"

Similar presentations


Ads by Google