Presentation is loading. Please wait.

Presentation is loading. Please wait.

EVENTS Wiring together objects, code, and actions.

Similar presentations


Presentation on theme: "EVENTS Wiring together objects, code, and actions."— Presentation transcript:

1 EVENTS Wiring together objects, code, and actions

2 Events 2  An ‘event’ is something that the user does  move the mouse  push down on a key  let up on a key  click the mouse button  …  A program should usually respond to these events  This happens by attaching ‘code’ to an ‘event’

3 EventButton 3 «constructor» + EventButton( String txt) «query» + int getX( ) + int getY( ) + int getWidth( ) + int getHeight( ) + String getText( ) «update» + repaint() + setSize( int, int ) + setText( String ) «event handler» + actionPerformed( ActionEvent ) EventButton «constructor» + EventButton( String txt) «query» + int getX( ) + int getY( ) + int getWidth( ) + int getHeight( ) + String getText( ) «update» + repaint() + setSize( int, int ) + setText( String ) «event handler» + actionPerformed( ActionEvent )

4 A Solution 4 import java.awt.event.*; import java.awt.*; public class RightButton extends EventButton { private Oval dot; public RightButton(int x, int y, int w, int h, Oval c) { super(“Move Right”); setBounds(x,y,w,h); dot = c; } public void actionPerformed(ActionEvent e) { dot.setLocation(dot.getX() + 5, dot.getY()); dot.repaint(); } import java.awt.event.*; import java.awt.*; public class RightButton extends EventButton { private Oval dot; public RightButton(int x, int y, int w, int h, Oval c) { super(“Move Right”); setBounds(x,y,w,h); dot = c; } public void actionPerformed(ActionEvent e) { dot.setLocation(dot.getX() + 5, dot.getY()); dot.repaint(); }

5 The Client 5 import javax.swing.*; public class OvalController { public OvalController() { JFrame win = new JFrame(“Demo”); win.setSize(300, 300); win.setLayout(null); Oval oval = new Oval(50,50,50,50); win.add(oval); RightButton rButton = new RightButton(5, 5, 100, 20, oval); win.add(rButton, 0); win.repaint(); win.setVisible(true); } import javax.swing.*; public class OvalController { public OvalController() { JFrame win = new JFrame(“Demo”); win.setSize(300, 300); win.setLayout(null); Oval oval = new Oval(50,50,50,50); win.add(oval); RightButton rButton = new RightButton(5, 5, 100, 20, oval); win.add(rButton, 0); win.repaint(); win.setVisible(true); } Image courtesy http://www.flickr.com/photos/tmab2003/3464087290/sizes/z/in/photostream/ Can we make program with two buttons. One moving the circle left and another moving it right?

6 A Solution 6 import java.awt.event.*; import java.awt.*; public class LeftButton extends EventButton { private Oval dot; public LeftButton(int x, int y, int w, in th, Oval c) { super(“Move Left”); setBounds(x,y,w,h); dot = c; } public void actionPerformed(ActionEvent e) { dot.setLocation(dot.getX() - 5, dot.getY()); dot.repaint(); } import java.awt.event.*; import java.awt.*; public class LeftButton extends EventButton { private Oval dot; public LeftButton(int x, int y, int w, in th, Oval c) { super(“Move Left”); setBounds(x,y,w,h); dot = c; } public void actionPerformed(ActionEvent e) { dot.setLocation(dot.getX() - 5, dot.getY()); dot.repaint(); }

7 The Client 7 import javax.swing.*; public class OvalController { public OvalController() { JFrame win = new JFrame(“Demo”); win.setSize(300, 300); win.setLayout(null); Oval oval = new Oval(50,50,50,50); win.add(oval); RightButton rButton = new RightButton(5, 5, 100, 20, oval); win.add(rButton, 0); LeftButton lButton = new LeftButton(5, 30, 100, 20, oval); win.add(lButton); win.repaint(); win.setVisible(true); } import javax.swing.*; public class OvalController { public OvalController() { JFrame win = new JFrame(“Demo”); win.setSize(300, 300); win.setLayout(null); Oval oval = new Oval(50,50,50,50); win.add(oval); RightButton rButton = new RightButton(5, 5, 100, 20, oval); win.add(rButton, 0); LeftButton lButton = new LeftButton(5, 30, 100, 20, oval); win.add(lButton); win.repaint(); win.setVisible(true); } Image courtesy http://www.flickr.com/photos/tmab2003/3464087290/sizes/z/in/photostream/

8 Another Way (Callbacks) 8  It is often useful to write a button class that doesn’t know anything about an Oval (or any other object that it is meant to operate on)  The button might command another object to perform the operation.  The other object is typically the controller.

9 The Client 9 public class OvalController { Oval oval; public OvalController() { JFrame win = new JFrame(“Demo”); win.setSize(300, 300); win.setLayout(null); oval = new Oval(50,50,50,50); win.add(oval); RightButton rButton = new RightButton(5, 5, 100, 20, oval); win.add(rButton, 0); LeftButton lButton = new LeftButton(5, 30, 100, 20, oval); win.add(lButton); win.repaint(); win.setVisible(true); } public void moveLeft() { oval.setLocation(oval.getX()-5, oval.getY()); oval.repaint(); } public void moveRight() { oval.setLocation(oval.getX()+5, oval.getY()); oval.repaint(); } public class OvalController { Oval oval; public OvalController() { JFrame win = new JFrame(“Demo”); win.setSize(300, 300); win.setLayout(null); oval = new Oval(50,50,50,50); win.add(oval); RightButton rButton = new RightButton(5, 5, 100, 20, oval); win.add(rButton, 0); LeftButton lButton = new LeftButton(5, 30, 100, 20, oval); win.add(lButton); win.repaint(); win.setVisible(true); } public void moveLeft() { oval.setLocation(oval.getX()-5, oval.getY()); oval.repaint(); } public void moveRight() { oval.setLocation(oval.getX()+5, oval.getY()); oval.repaint(); } Image courtesy http://www.flickr.com/photos/tmab2003/3464087290/sizes/z/in/photostream/ Make the oval an instance variable so it can be used in two new methods. These methods are callback methods. The buttons will “call-back” to them when pushed.

10 Another Way 10 import java.awt.event.*; public class LeftButton extends EventButton { private OvalController controller; public LeftButton(int x, int y, int w, int h, OvalController c) { super(“Move Left”); setBounds(x, y, w, h); controller = c; } public void actionPerformed(ActionEvent e) { controller.moveLeft(); } import java.awt.event.*; public class LeftButton extends EventButton { private OvalController controller; public LeftButton(int x, int y, int w, int h, OvalController c) { super(“Move Left”); setBounds(x, y, w, h); controller = c; } public void actionPerformed(ActionEvent e) { controller.moveLeft(); } import java.awt.event.*; public class RightButton extends EventButton { // other code omitted public void actionPerformed(ActionEvent e) { controller.moveRight(); } import java.awt.event.*; public class RightButton extends EventButton { // other code omitted public void actionPerformed(ActionEvent e) { controller.moveRight(); }

11 Text Fields 11  A text field is a way for users to type in textual information.  An EventTextField is  A Component  A JTextField EventTextField «constructor» + EventTextField( String txt) «query» + int getX() + int getY() + int getWidth() + int getHeight() + Color getParent() + String getText()... «update» + void repaint() + void setBounds( int, int, int, int ) + void setSize( int, int ) + void setLocation( int, int ) + void setText( String )... «event handler» + void actionPerformed( actionEvent ) EventTextField «constructor» + EventTextField( String txt) «query» + int getX() + int getY() + int getWidth() + int getHeight() + Color getParent() + String getText()... «update» + void repaint() + void setBounds( int, int, int, int ) + void setSize( int, int ) + void setLocation( int, int ) + void setText( String )... «event handler» + void actionPerformed( actionEvent )

12 Example 12 import java.awt.event.*; public class OvalSizerField extends EventTextField { private Oval oval; public OvalSizerField(int x, int y, int w, int h, Oval c) { super(c.getWidth() + “”); setBounds(x, y, w, h); oval = c; } public void actionPerformed(ActionEvent e) { String fieldText = getText(); int numericValue = new Scanner(fieldText).nextInt(); oval.setSize(numericValue, numericValue); } import java.awt.event.*; public class OvalSizerField extends EventTextField { private Oval oval; public OvalSizerField(int x, int y, int w, int h, Oval c) { super(c.getWidth() + “”); setBounds(x, y, w, h); oval = c; } public void actionPerformed(ActionEvent e) { String fieldText = getText(); int numericValue = new Scanner(fieldText).nextInt(); oval.setSize(numericValue, numericValue); }

13 The Client 13 import javax.swing.*; public class OvalController { public OvalController() { JFrame win = new JFrame(“Demo”); win.setSize(300, 300); win.setLayout(null); Oval oval = new Oval(50,50,50,50); win.add(oval); OvalSizerField f = new OvalSizerField(5, 5, 100, 20, oval); win.add(f); win.repaint(); win.setVisible(true); } import javax.swing.*; public class OvalController { public OvalController() { JFrame win = new JFrame(“Demo”); win.setSize(300, 300); win.setLayout(null); Oval oval = new Oval(50,50,50,50); win.add(oval); OvalSizerField f = new OvalSizerField(5, 5, 100, 20, oval); win.add(f); win.repaint(); win.setVisible(true); } Image courtesy http://www.flickr.com/photos/tmab2003/3464087290/sizes/z/in/photostream/

14 Sliders 14  A slider represents a single integer value.  The value is within a range of integers [min, max]  The value is controlled by the position of the slider To the left: value is min To the right: value is max Elsewhere: a value in [min, max]

15 15 EventSlider Crash Course EventSlider + static final int HORIZONTAL + static final int VERTICAL «constructor» + EventSlider( int, int, int, int ) «query» + int getX() + int getY() + int getWidth() + int getHeight() + int getMaximum() + int getMinimum() + Container getParent() + int getValue()... «update» + void repaint() + void setBounds( int, int, int, int ) + void setSize( int, int ) + void setLocation( int, int ) + void setMinimum( int ) + void setMaximum( int ) + void setValue( int )... «event handler» + void stateChanged( javax.swing.event.ChangeEvent ) knob getMinimum()getMaximum() getValue() called when user adjusts the knob or arrow buttons assigns new values to HORIZONTAL or VERTICAL getMinimum(), getMaximum() & getValue()

16 Example 16  Write a program to use three sliders to change the color of an oval. Each slider controls the amount of red, green or blue in the color of the on-screen oval.

17 Type Conformance 17  For any class C, an object V conforms to C if  V is an object of that exact type  OR V is a subclass of class C  For assignment of the form  VAR = EXPRESSION; The EXPRESSION must conform to the class of VAR  For a method of the form  TYPE NAME(type1 name1, type2 name2, …) {…} All actual parameters must conform to the specified types

18 Conformance 18 Write a method to return true if the height of a Rectangle is 10 pixels or greater and false otherwise. public boolean isTall(Rectangle r) { return r.getHeight() >= 10; } public boolean isTall(Rectangle r) { return r.getHeight() >= 10; } Write a method to return true if the height of an Oval is 10 pixels or greater and false otherwise. public boolean isTall(Oval o) { return o.getHeight() >= 10; } public boolean isTall(Oval o) { return o.getHeight() >= 10; } Write a method to return true if the height of an Oval or Rectangle or Line is 10 pixels or greater and false otherwise. public boolean isTall(Component v) { return v.getHeight() >= 10; } public boolean isTall(Component v) { return v.getHeight() >= 10; } Oval oval = new Oval(); Rectangle rect = new Rectangle(); SelectableRectangle rect2 = new ASelectableRectangle(); …isTall(rect)… …isTall(oval)… …isTall(rect2)…

19 Dynamic Conformance Testing 19  The instanceof operator tests conformance  Syntax:  Expression instanceof ClassName  Semantics:  Binary boolean-valued infix operator  Left-hand side is an object  Right-hand side is the name of a class  returns true if the LHS conforms to the RHS and false otherwise

20 Conformance 20 Write a method that accepts a Component object. If the object conforms to Rectangle then move it right by 5 pixels. If the object conforms to Oval then move it left by 5 pixels. All other Components should move down by 5 pixels. public void move(Component v) { int dx = 0, dy = 0; if(v instanceof Rectangle) { dx = 5; } else if(v instanceof Oval) { dx = -5; } else { dy = 5; } v.setLocation(v.getX() + dx, v.getY() + dy); } public void move(Component v) { int dx = 0, dy = 0; if(v instanceof Rectangle) { dx = 5; } else if(v instanceof Oval) { dx = -5; } else { dy = 5; } v.setLocation(v.getX() + dx, v.getY() + dy); }


Download ppt "EVENTS Wiring together objects, code, and actions."

Similar presentations


Ads by Google