{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

DesignPatterns - Design Pa*erns Anima/on Undo/Redo...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Design Pa*erns + Anima/on Undo/Redo Graphics and Hints Design Pa*erns •  Design: –  the planning –  that lays the basis for the making –  of every object –  or system •  Pa*ern: –  a type of theme –  of recurring –  events or objects Design Pa*erns in OOP •  Recipes for solving standardized problems •  Abstrac/ons of concrete problems –  How to make a class that allows only one instance of itself: •  The Singleton Design Pa*ern •  Provides a general solu/on that needs to be implemented Started from Architecture •  Architectural Pa*erns –  Lecture rooms –  Studies –  Town Squares –  Restaurants –  Etc. •  Adopted in many non ­related areas Design pa*erns •  •  •  •  •  describe… how objects communicate… without becoming entangled… in each other’s data models… and/or methods. What is a design pa*ern? •  A general recipe •  A framework •  A template •  Pre ­made solu/on Design Pa*ern Content •  •  •  •  Intent – what is the goal of the pa*ern? Applicability – when should it be used? Structure – the pa*ern descrip/on Consequences – pros and cons of using the pa*ern. Example •  The ”Singleton pa*ern” •  Ensures that one and only one instance can be created from a class private sta/c Singleton m_instance; private Singleton() { … } // private constructor public sta/c synchronized Singleton getInstance() { if (m_instance == null) // only create new first /me m_instance = new Singleton(); } return m_instance; // return the single instance Design Pa*erns vs. Libraries •  Libraries contain concrete classes –  Implement concrete solu/ons •  Design Pa*erns are abstract solu/ons –  Advice on general solu/ons •  You can’t import a design pa*ern DP Factory •  Another interes/ng Design Pa*ern •  What is a factory? –  Produc/on unit •  produces instances •  mul/ple instances of different classes –  Ask for a type and you get it! Factory Example •  A graphic Shape –  circles, rectangles, stars, etc. –  All shapes have size, and can be drawn! •  The Shape interface defines methods •  Use a ShapeFactory: public Shape getShape(String type, int size) {…} ShapeFactory, Why? •  One class provides all shapes! •  Dynamic crea/on! •  Consistent usage of Shapes –  Unless explicitly needed, we don’t have to know which Shape we have created aderwards UNDO •  The Undo/Redo mechanism should provide: –  Single Undo –  Single Redo (of Undone ac/vity) •  and, if possible: –  Undo and redo of several consecu/ve ac/on •  How can this be acheived? Command Design Pa*ern •  One way to handle queues of ac/ons! •  Important DP for the opera/on of UNDO •  Simple principle Command •  A very simple interface! –  One method execute() public interface Command { public void execute(); } Command •  All ac/vi/es are driven by the execute() method –  Encapsulate all user ac/vi/es in ”Command objects” Simple example: Macro public class IAm implements Command { public void execute() { System.out.println("I'm the command pa*ern!"); } } // An object that holds commands: public class Macro { private List commands = new ArrayList(); public void add(Command c) { commands.add(c); } public void run() { Iterator it = commands.iterator(); while(it.hasNext()) ((Command) it.next()).execute(); // Cas/ng! } } UNDO? •  The execute() method describes what happens during an edit ac/vity… •  UNDO means reversing the edi/ng ac/vity… •  In the command interface add: –  unexecute() –  reexecute() –  Describes the backward process public interface Command { public void execute(); public void unexecute(); // undo public void reexecute(); // redo } UndoManager •  UndoManager is support for UNDO in Swing •  ”edit types”  ­ the effect of a user invoked command –  each edit type must s/ll have a defini/on of the edit and its effects •  Simplifyes the management of the Undo/Redo Swing Undo •  •  •  •  •  •  UndoableEdit (Interface) AbstractUndoableEdit (Abstract class) CompoundEdit (class for sequences of undoables) UndoableEditListener (Interface) UndoableEditEvent (no/fica/on object) UndoableManager (Queue manager) –  Like EventManager •  UndoableEditSupport (Support class) Undo/Redo ­queue UndoManager op1 op2 op3 op4 Undo/Redo ­queue UndoManager op1 op2 op3 op4 op1 op2 op3 op4 Undo/Redo ­queue UndoManager op1 op2 op3 op4 op1 op2 op3 op4 op1 op2 op5 Undo •  Good example program in linked page on course homepage (Assignment 2) Add an undo/redo func/on to your Java apps with Swing Anima/on Driver Class •  Many Good Sugges/ons (these will be commented groupwise in separate email) •  Good example of a Design Pa*ern •  How to ensure OOP principles? Purspose of Anima/on Driver •  One or more Anima/ons –  Simula/on –  Real/me game (e.g. Ba*lebots) –  Other dynamic real/me anima/on with several animated objects •  One Actor controls the Anima/on process –  Singleton (suggested by one group) –  Controls Anima/on Progress Decisions? •  The amount of control in driver? •  Different anima/on speeds? •  Which objects to animate? •  How is the driver controlled? OOP Principle •  The driver needs to be free of knowledge about the respec/ve anima/ons •  Each animated object responsible for its own anima/on, e.g.: –  Driver controls general anima/on speed –  Animated object controls its own rela/ve anima/on speed Animator Pa*ern •  Interface AnimatedObject –  One primary method: /ck() –  Each /ck is one cycle in general Anima/on •  Animator –  Tracks (manages) the animated objects –  Sends /cks to all its Animated Objects –  Methods: •  addObject(AnimatedObject ao) •  removeObject(AnimatedObject ao) •  /ck() // sends /cks to all animated objects Animator Pa*ern •  Animated Object could be any class –  As long as it implements the AnimatedObject interface •  Does not have to be a graphic component! –  In fact it is easier to draw if it is not… •  A panel can host many animated graphics –  defines only paintComponent(Graphics g)! –  the graphic adds its own drawing to the Graphics environment! Animated Graphics •  Uses the paintComponent of (e.g.) Jpanel •  Implements Drawable interface: –  method: draw(Graphics g) –  the method should specify the graphic commands for the graphic •  Does not specify the paintComponent –  is not a Component, but a ”loose” graphic Jpanel paintComponent •  Each Drawable object implements the draw() method. @Override public void paintComponent(Graphics g) { Drawable d; for (Iterator<?> it = graphicObjects.iterator(); it.hasNext(); ) d = (Drawable) it.next(); // Cas/ng to Drawable d.draw(g); } Complex graphics •  Every graphic uses draw(Graphics g) to add itself to the graphics environment •  A single call to paintComponent() Animated Complex Graphics •  If we want to implement graphics that move, the class has to implement both interfaces. public class MovingStar implements Drawable, AnimatedObject { … } //here the drawing is //made in //draw(Graphics g) Anima/on Process •  Create graphic Container (JPanel?) •  Collect all graphic objects (not Swing Components) •  For every /ck: –  update the graphic objects (posi/on, size, colour, etc.) –  In paintComponent(Graphics g) draw all graphic objects on the Graphics environment g (Detour) Mixing graphics and Swing components? Don’ts in GUIs •  We will be able to do almost anything i GUI design •  But some things maybe we shouldn’t •  (but no rule without excep/on, of course) Move things for the user •  You can control the mouse pointer for the user –  java.awt.Robot library •  java.awt.Robot •  mouseMove(int x, int y); •  Should you? –  Most of the /me – NO! –  Bad GUI! •  One possible excep/on: Guding help systems The Robot is s/ll Useful! Can make screenCaptures! createScreenCapture(Rectangle screenRect) returns a BufferedImage Remove Title Bar of Window •  You can remove the Titlebar of a window •  Should you? –  Maybe, e.g. in AboutBoxes •  But remember to provide the user with control of the window! Use Anima/ons •  You can use anima/ons •  Should you? –  Yes, some/mes –  But restric/vely •  Beware of percep/on exhaus/on Use Undo •  You will be able to Undo things •  Should you? –  Yes, but only significant changes –  Don’t undo single character inser/on •  What is significant? Use graphic effects •  Swing allows you to use graphic effects –  E.g. GradientPaint, Transparency •  Should you? • Yes, but always provide an alterna/ve skin that is clear and dis/nct •  Make sure to test that the effects work on the platorm –  Make it easy to access Be Crea/ve •  It is (in principle) your imagina/on (and available /me) that sets the limits for what you can do. •  Should you? –  Yes, as long as it is good User Interface Design –  Yes, as long as it is possible to understand –  Yes, as long as it makes sense for the intended user groups Prac/ce Anima/on •  You have a large toolbox with graphics! Prac/ce using it! •  Should you? •  Yes, even if it not all will be used in the Calendar, the more you prac/ce, the more you will know about how and when you should use it! ...
View Full Document

{[ snackBarMessage ]}