Nov 2005 MSc Slide 1 - Another Example of a Structural Pattern Alternative way of adding Functionality to an existing class (alternative to a derived class)

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

Engr 691 Special Topics in Engineering Science Software Architecture Spring Semester 2004 Lecture Notes.
1111 Creating ASPX Controls Programatically Objectives You will be able to Dynamically add controls to a page. Dynamically alter properties of controls.
Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
2D Graphics Drawing Things. Graphics In your GUI, you might want to draw graphics E.g. draw lines, circles, shapes, draw strings etc The Graphics class.
Made with love, by Zachary Langley Applets The Graphics Presentation.
Web Design & Development Lecture 19. Java Graphics 2.
CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 21.
Laboratory Study II October, Java Programming Assignment  Write a program to calculate and output the distance traveled by a car on a tank of.
Mouse Listeners We continue our examination of GUIs by looking at how to interact with the mouse –Just as Java creates Events when the user interacts with.
LAB SESSION 7 Graphical user interface Applet fundamentals Methods in applets Execution of an applet Graphics class.
CSC1401 Drawing in Java - 2. Reminder from last class How do you save your modified picture? String filename = …; Picture stevePicture = new Picture(filename);
 2007 Dr. Natheer Khasawneh. Chapter 13. Graphical User Interface Concepts: Part 1.
Windows form application using System.Windows.Forms; using System.Drawing; class MyForm:Form{ public static void Main(){ Application.Run(new.
Graphics and Multimedia Session 13 Mata kuliah: M0874 – Programming II Tahun: 2010.
User Interface Programming in C#: Graphics
GUI and Swing, part 2 The illustrated edition. Scroll bars As we have previously seen, a JTextArea has a fixed size, but the amount of text that can be.
Swinging Into Swing Leo S. Primero III. Understanding what Swing Is Swing is a package that lets you create applications that use a flashy Graphical User.
2D Graphics in Java COMP53 Nov 14, Applets and Applications Java applications are stand-alone programs – start execution with main() – runs in JVM.
Advanced Java Class GUI – part 1. Intro to GUI GUI = Graphical User Interface -- “Gooey” Just because it’s “gooey” does not mean you may write messy code.
Graphics and Multimedia. Outline Introduction Graphics Contexts and Graphics Objects Color Control.
Graphics and Multimedia. Introduction The language contains many sophisticated drawing capabilities as part of namespace System.Drawing and the other.
Graphics Images – PictureBox control Drawing graphics - Graphics object Multimedia controls PictureBox control Image property – select image Choose how.
Software Engineering1 The.NET Framework and the CLI 2D Graphics with GDI+ Visual Studio.NET 2008.
CST238 Week 5 Questions / Concerns? Announcements – HW#1 due (Project ideas) – Check-off Take Home lab#4 Recap New topics – Drawing Coming up: – GUI Bloopers.
1 Windows Graphics. 2 Objectives You will be able to Use the Windows GDI+ to draw arbitrary figures and text on a Windows form. Add a handler for the.
Object Oriented Programming Graphical User Interfaces Dr. Mike Spann
Object Oriented Programming Graphics and Multimedia Dr. Mike Spann
CSC 298 Windows Forms.
BIM211 – Visual Programming Objects, Collections, and Events 1.
Learn about the types of Graphics that are available Develop a basic Graphics applet Develop a basic Graphics application Review the Java API and use.
Nov 2005 MSc Slide 1 First we look at a standard Dos based application Combining Decorator & Factory Patterns.
tiled Map Case Study: Rendering with JPanel © Allan C. Milne v
1 Scroll Bars Providing Scrollbars. 2 Objectives You will be able to: Use Windows Graphics to display tabular information on a Windows form. Add graphics.
Java is an object oriented programming language In this model of programming all entities are objects that have methods and fields Methods perform tasks.
Canvas and Graphics CS 21a. 9/26/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L17: Canvas.
CSTP FS99CS423 (cotter)1 Java Graphics java.awt.*;
© A+ Computer Science - Chicken yeller = new Chicken();
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
Object-Oriented Software Engineering Using Threads and simple Animation.
CSI 3125, Preliminaries, page 1 AWT. CSI 3125, Preliminaries, page 2 AWT Java AWT (Abstract Windowing Toolkit) is an API to develop GUI or window-based.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 27.1 Test-Driving the Drawing Shapes Application.
Bridge Bridge is used when we need to decouple an abstraction from its implementation so that the two can vary independently. This type of design pattern.
Introduction to Graphics. Graphical objects To draw pictures, we will use three classes of objects: –DrawingPanel : A window on the screen. –Graphics.
CPSC 481 – Week #7 Sowmya Somanath
Lecture # 6 Graphical User Interface(GUI). Introduction A graphical user interface (GUI) presents a user- friendly mechanism for interacting with an application.
 2002 Prentice Hall. All rights reserved. 1 Outline Mouse Event Handling Keyboard Event Handling Graphical User Interface Concepts:
Appendix A: Windows Forms. 2 Overview Describe the structure of a Windows Forms application –application entry point –forms –components and controls Introduce.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Georgia Institute of Technology Comic Strip Analysis and Design Inheritance, Abstract Classes, and Polymorphism part 2 Barb Ericson Georgia Institute of.
Georgia Institute of Technology Comic Strip Analysis and Design Inheritance, Abstract Classes, and Polymorphism part1 Barb Ericson Georgia Institute of.
10/20/2005week71 Graphics, mouse and mouse motion events, KeyEvent Agenda Classes in AWT for graphics Example java programs –Graphics –Mouse events –Mouse.
Dec 2005 MSc Slide 1 Pattern that are most specifically concerned with communication between objects Cat 3: Behavioral Patterns.
Java Applets.
Modern Programming Tools And Techniques-I
Object Oriented Programming
Java Swing.
Java Applets.
Graphics and Multimedia
Java Graphics The basic rendering mechanism is the drawing system that controls when and how programs can draw on a graphics component. When a component.
CASE Tools Graphical User Interface Programming Using C#
Java Applets.
Java Applets.
Lecture 9 GUI and Event Driven CSE /16/2019.
Object Oriented Programming
Decorator.
Decorator Pattern.
Presentation transcript:

Nov 2005 MSc Slide 1 - Another Example of a Structural Pattern Alternative way of adding Functionality to an existing class (alternative to a derived class) Favors Composition over Inheritance Often referred to as a Wrapper Advantage: No need for several levels of Inheritence (reduces Complexity) Decorator Pattern

Nov 2005 MSc Slide 2 Instead of: Decorator Pattern X Y Z Z p = new Z(); X YZ Z p=new Z(new Y(new X())); Can add functionality by adding more Objects

Nov 2005 MSc Slide 3 Typically used to change appearance of a Visual Component One big advantage is that you can dynamically change appearance/functionality of a component Decorator Pattern

Nov 2005 MSc Slide 4 Decorator Pattern Want to decorate a Button

Nov 2005 MSc Slide 5 using System; using System.Windows.Forms; using System.Drawing; abstract class Decorator : Panel { protected Control ctrl; public Decorator(Control c) { this.Size=c.Size; ctrl=c; Controls.Add(c); c.Paint += new PaintEventHandler( paint); } virtual public void paint(object sender, PaintEventArgs pe){} }

Nov 2005 MSc Slide 6 class SlashDecorator : Decorator { public SlashDecorator(Control c):base(c) {} override public void paint(object sender, PaintEventArgs pe){ Pen aPen = new Pen(Color.Black, 1); Graphics g = pe.Graphics; int x2 = this.Size.Width; int y2 = this.Size.Height; g.DrawLine(aPen,0,0,x2,y2); }

Nov 2005 MSc Slide 7 class GFrame:Form { private Button cbutton=new Button(); private Button dbutton=new Button(); private SlashDecorator d; public GFrame():base(){ cbutton.Text="cbutton"; dbutton.Text="dbutton"; d=new SlashDecorator(dbutton); cbutton.SetBounds(10,10,cbutton.Size.Width, cbutton.Size.Height); d.SetBounds(100,10,d.Size.Width,d.Size.Height); Controls.Add(cbutton); Controls.Add(d); }}

Nov 2005 MSc Slide 8 public class Test92{ public static void Main(string[] args){ Application.Run(new GFrame()); }

Nov 2005 MSc Slide 9 Example 2: This time we have a Decorated Label

Nov 2005 MSc Slide 10 using System; using System.Windows.Forms; using System.Drawing; abstract class Decorator : Panel { protected Control ctrl; public Decorator(Control c) { this.Size=c.Size; ctrl=c; Controls.Add(c); c.Paint += new PaintEventHandler( paint); } virtual public void paint(object sender, PaintEventArgs pe){} } As before

Nov 2005 MSc Slide 11 class CoolDecorator : Decorator { public CoolDecorator(Control c):base(c) { } override public void paint(object sender, PaintEventArgs pe){ Pen aPen = new Pen(Color.Red, 2); Graphics g = pe.Graphics; int x2 = this.Size.Width; int y2 = this.Size.Height; g.DrawRectangle(aPen,0,0,x2,y2); } Draw a Rectangle

Nov 2005 MSc Slide 12 class GFrame:Form { private Label l=new Label(); private CoolDecorator c; public GFrame():base(){ l.Text="MyLabel"; c=new CoolDecorator(l); c.SetBounds(10,10,c.Size.Width, c.Size.Height-5); Controls.Add(c); }

Nov 2005 MSc Slide 13 public class Test92{ public static void Main(string[] args){ Application.Run(new GFrame()); }

Nov 2005 MSc Slide 14 Now to add Dynamic Behaviour, change Colour to Blue

Nov 2005 MSc Slide 15 class CoolDecorator : Decorator { bool mouse_over=false; public CoolDecorator(Control c):base(c) { EventHandler evh = new EventHandler(mouseEnter); ctrl.MouseEnter+= evh; ctrl.MouseLeave += new EventHandler(mouseLeave); } public void mouseEnter(object sender, EventArgs e){ mouse_over = true; ctrl.Refresh (); } public void mouseLeave(object sender, EventArgs e){ mouse_over = false; this.Refresh (); }

Nov 2005 MSc Slide 16 override public void paint(object sender, PaintEventArgs pe){ Pen aPen = new Pen(Color.Red, 2); Pen bPen = new Pen(Color.Blue, 2); Graphics g = pe.Graphics; int x2 = this.Size.Width; int y2 = this.Size.Height; g.DrawRectangle(aPen,0,0,x2,y2); if (mouse_over == true){ g.DrawRectangle(bPen,0,0,x2,y2);} } }

Nov 2005 MSc Slide 17 UML Class Diagram Control CoolDecorator SlashDecorator GFrame DecoratorControl Panel

Nov 2005 MSc Slide 18 Big Advantage of Decorator(Wrapper) is that you can add functionality by adding Objects Label lab=new Label(); lab.Text="Example"; SlashDecorator c=new SlashDecorator(lab); Controls.Add(c);

Nov 2005 MSc Slide 19 Combining Decorators c=new CoolDecorator(b); Normally: Want: c=new SlashDecorator(new CoolDecorator(b));

Nov 2005 MSc Slide 20 Combining Decorators c=new CoolDecorator(null, b); Normal Wrapper: Embedded Wrapper: sd=new SlashDecorator(new CoolDecorator(null,b),b); In practice new a ref to parent and underlying control:

Nov 2005 MSc Slide 21 abstract class Decorator : Panel { protected Control ctrl; public Decorator(Control c) { this.Size=c.Size; ctrl=c; Controls.Add(c); c.Paint += new PaintEventHandler( paint); } virtual public void paint(object sender, PaintEventArgs pe){} } As before

Nov 2005 MSc Slide 22 class CoolDecorator : Decorator { bool mouse_over=false; SlashDecorator sdec; public CoolDecorator(SlashDecorator sdec,Control c) :base(c) { this.sdec = sdec; if (sdec !=null) c.Paint += new PaintEventHandler( sdec.paint); EventHandler evh = new EventHandler(mouseEnter); ctrl.MouseEnter+= evh; MouseEventHandler(mouseMove); ctrl.MouseLeave += new EventHandler(mouseLeave); } override public void paint(object sender, … As before

Nov 2005 MSc Slide 23 class SlashDecorator : Decorator { private CoolDecorator cd=null; public SlashDecorator(CoolDecorator cd,Control c) :base(c) { this.cd=cd; if (cd !=null) c.Paint += new PaintEventHandler( cd.paint); } override public void paint(object sender, … As before

Nov 2005 MSc Slide 24 class GFrame:Form { private Label l=new Label(); private SlashDecorator sd; public GFrame():base(){ l.Text="MyLabel"; sd=new SlashDecorator(new CoolDecorator(null,l),l); sd.SetBounds(100,10,sd.Size.Width,sd.Size.Height); Controls.Add(sd); this.Refresh();}

Nov 2005 MSc Slide 25 Exercise 0: Modify the first Decorator (test0.java) so its in the following format

Nov 2005 MSc Slide 26 Exercise 1: Modify Exercise 0 to include some dynamic Behaviour Red X