Continuation: GUI’s and eventhandlers in java +Some on design patterns. - PowerPoint PPT Presentation

continuation gui s and eventhandlers in java some on design patterns n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Continuation: GUI’s and eventhandlers in java +Some on design patterns. PowerPoint Presentation
Download Presentation
Continuation: GUI’s and eventhandlers in java +Some on design patterns.

play fullscreen
1 / 98
Continuation: GUI’s and eventhandlers in java +Some on design patterns.
111 Views
Download Presentation
victoir
Download Presentation

Continuation: GUI’s and eventhandlers in java +Some on design patterns.

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Continuation:GUI’s and eventhandlers in java+Some on design patterns. Lecturer: Martin Jagersand Department of Computing Science University of Alberta Notes based on previous courses by Ken Wong, Eleni Stroulia Zach Dodds, Martin Jagersand

  2. Today • Example GUI application: Calculator • Some on design patterns and strategies

  3. GUI practicalities • Divide task into subtasks/abstractions. • Decide how user interacts with each subtask • Design and implement drawing of GUI components, buttons etc. • Connecting buttons, sliders etc to code. • Eventhandlers

  4. Containers: • Frame • top level container (window) • Has typical window components such as close buttons, possible scroll down menus etc • Panel • intermediate container • Organizes contained components • Atomic components • Buttons, labels etc. • Presents bits of info, allow interactio

  5. Layouts • Layouts arrange items by specifying the relative locations in a pane. • Why layouts? – Make interfaces flexible to different windows, resizing etc. • Alternative: use absolute positioning of each item • Tedious for the programmer • Inflexible for the user (can’t stretch window to see more)

  6. Layout examples

  7. Event handling • How to connect graphics to action • Handled though interfaces “ActionListeners” • Programmers implement and specialize these for their application

  8. Event handling • Source object: e.g. button • Target object: programmed to react appropriately

  9. Case study:Calculator GUI • Was assignment 1 a couple of years ago…

  10. Design idea 1 • Use several nested panes for structurally different components: Jframe Command history display Immediate results display Alfa keypad Numeric keypad

  11. Small Big Flexible layout through the layout manager

  12. Structure • Abstract into two classes: • Extend Jframe to set up the window and menu bars • Extend Jpanel to set up and arrange the calculator buttons and displays

  13. JFrame Class JavaCalcFrame extends Jframe //constructor for menu etc : // misc events handling, e.g.: addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } }); : //Instantiate main panel Container contentpane = getContentPane(); CalculatorPanel panel = new CalculatorPanel(); contentPane.add(panel); Example of anonymous class implementing listener

  14. JPanelCalculator panel class CalculatorPanel extends JPanel implements ActionListener, KeyListener • Implements reactive pattern.

  15. Hierarchy of panels class CalculatorPanel extends Jpanel : setLayout(new BorderLayout()); //Component and panel setup JPanel eastPanel = new JPanel(); eastPanel.setLayout(new BorderLayout()); //display display = new JTextField(12); display.setHorizontalAlignment(JTextField.RIGHT); display.setEditable(false); Color col = new Color(255,255,255); display.setBackground(col); display.addKeyListener(this); Font f = new Font("MonoSpaced", Font.BOLD, 10); display.setText("0"); eastPanel.add(display, "North");

  16. Hierarchy of panels 2 //scroll capability JScrollPane scrollPane = new JScrollPane(numList); scrollPane.setFont(f); add(scrollPane, "West"); scrollPane.addKeyListener(this); //button panels JPanel moreButs = new JPanel(); moreButs.setLayout(new GridLayout(4, 2)); : eastPanel.add(moreButs, "West"); : centerpanel = new JPanel(); centerpanel.setLayout(new GridLayout(4, 4)); eastPanel.add(centerpanel, "Center"); : add(eastPanel, ("Center")); Add history window, alfa and numeric buttons to intermediate pane Add intermediate pane

  17. Adding atomic items private void addButton(Container c, String s) { JButton b = new JButton(s); c.add(b); b.addKeyListener(this); b.addActionListener(this); but = b; } Take care of event handling in same class (Reactive pattern)

  18. Adding atomic items //button panels JPanel moreButs = new JPanel(); moreButs.setLayout(new GridLayout(4, 2)); String moreButtons[] = {"C ", "CE", "M+", "M-", "MR", "MC", SQRT, "%"}; for (i = 0; i < 8; i++) { addButton(moreButs, moreButtons[i]); } eastPanel.add(moreButs, "West"); centerpanel = new JPanel(); centerpanel.setLayout(new GridLayout(4, 4)); String buttons = "789/456x123-0.+="; for (i = 0; i < buttons.length(); i++) addButton(centerpanel, buttons.substring(i, i + 1)); centerpanel.setFont(f); eastPanel.add(centerpanel, "Center");

  19. EventhandlersMouse events //mouse events public void actionPerformed(ActionEvent evt) { st = evt.getActionCommand(); doAction(st); } Required in interface Call calculation procedure with string of events

  20. EventhandlersKey events //Event handlers public void keyAction(KeyEvent e) { char c = e.getKeyChar(); //handy substitutions if (c == '*') c = 'x'; else if (c == 'p') c = '+'; //enable the Enter keys else if (c == '\n' || c==141) c = '='; else if (c == 'c') c = '%'; st = c + ""; if (c == 'q') st = SQRT; else st = c + ""; doAction(st); }

  21. Remains • Write the method “doAction” to perform the actual calculations based on the string of events. (exercise)

  22. Resulting interface:

  23. Lecture 8, Part 2Designing Classes • Heuristics and guidelines: • discovering a design • evaluating a design

  24. Designing Classes • Issues: • process is iterative • introduce classes • consider alternative ideas • requires good judgement • comes with experience

  25. Discovering Class Design • Inspirations for classes: • behavioral perspective • focus on actions on the system • structural perspective • focus on relationships among components • information perspective • focus on the role of information and its manipulation

  26. Behavioral Perspective • Identify actions(what the system does). • Reveal objects to perform the actions. • Form collaborations of objects for more complex actions.

  27. Behavioral Perspective • Questions to ask: • What object initiates the action? • What objects collaborate in performing the action? • What objects are altered in performing the action? • What objects are queried during the action?

  28. Behavioral Perspective • Categories of objects often associated with action: • actor • reactor • transformer • agent

  29. Behavioral Perspective • Actor: • has a specific goal or mission • drives the system to achieve the expected result • may enforce the sequencing of activities • may cope with exceptional conditions

  30. Behavioral Perspective • Reactor: • responds to events:internal, external, and user interface • determines the appropriate reaction • initiates the response • may require state or be stateless • e.g., event handler, action listener

  31. Behavioral Perspective • Transformer: • alters input data in some way and produces output • may be sequenced or pipelined • kinds:formatter, filters

  32. Behavioral Perspective • Formatting transformer: • displayer • render data in human-readable form • e.g., stream I/O, text layout, etc. • marshaller • flatten or linearize structured data • e.g., converting data to file blocks • encoder • convert data into a standard format or encrypt the data for secure transfer

  33. Behavioral Perspective • Filtering transformer: • screens the input data according to some criteria • e.g., pattern matching

  34. Behavioral Perspective • Agent: • assists other objects in some way • relieves other objects of a responsibility • hides information • kinds:messenger, server, finder, communicator

  35. Behavioral Perspective • Messenger agent: • relays information from one part of the system to another • relieves the sender from the responsibilities of locating the receiver, delivering the information, etc.

  36. Behavioral Perspective • Server agent: • produces and/or consumes data • relieves clients from the responsibilities of managing the data • e.g., database, web, etc.

  37. Behavioral Perspective • Finder agent: • locates other objects or data • contains knowledge of how and where to search • e.g., search engine, tool registry

  38. Behavioral Perspective • Communicator agent: • interacts with the user • e.g., present a file dialog

  39. II: Structural Perspective • Identify relationships implied by the specification(e.g., consists of, uses, maintains, etc.). • Form relationships among candidate entities (classes).

  40. Structural Perspective • Questions to ask: • What objects are involved in the relationship? • What objects are necessary to sustain the relationship? • What objects not in the relationship are aware of and exploit the relationship? • What objects not in the relationship are used by the related objects?

  41. Structural Perspective • Categories of structure: • acquaintance • containment • collection

  42. Structural Perspective • Acquaintance structure: • association of objects • some objects know about one or more of the other objects • kinds:symmetric or asymmetric,persistent or transitory,direct or indirect

  43. Structural Perspective • Containment structure: • aggregation of parts into a whole • “contains”, “consists of”, “has” • kinds:collaborator, controller

  44. Structural Perspective • Collaborator containment: • whole is created by the interaction of its parts • e.g., StopWatch

  45. Structural Perspective • Controller containment: • controller object may have total control over contained objects • e.g., boss/worker model • boss delegates tasks to workers • workers are independent of each other • workers are invisible to the client

  46. Structural Perspective • Collection structure: • like containment, but collection does not totally conceal the grouped objects • objects might be shared across collections • kinds:peer, iteration, coordinator

  47. Structural Perspective • Peer collection: • collection object imposes no control and simply gives a name to the grouping • grouped objects have equal status

  48. Structural Perspective • Iterator collection: • collection object provides selection, ordering, or traversal over its objects • grouped objects may need to support a common interface(e.g., Comparable)

  49. Structural Perspective • Coordinator collection: • collection object maintains some invariant condition among its objects • e.g., RadioButton collection

  50. III: Information Perspective • Study the information content and manipulations of the system. • Distinguish betweendata (info to be processed) and state (info used to control processing).