1 / 20

Events

Events. Wiring together objects, code, and actions. Events. 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’. EventButton

albina
Download Presentation

Events

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Events Wiring together objects, code, and actions

  2. Events • 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 «constructor» + EventButton( String txt) «query» + intgetX( ) + intgetY( ) + intgetWidth( ) + intgetHeight( ) + String getText( ) «update» + repaint() + setSize( int, int ) + setText( String ) «event handler» + actionPerformed( ActionEvent ) EventButton • An EventButton • is a Component that when clicked • invokes the actionPerformed method • Write a program to: • Place a button on the window • Draw a “ball” • When the button is pressed, the circle is moved right by 5 pixels

  4. A Solution 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 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); RightButtonrButton= new RightButton(5, 5, 100, 20, oval); win.add(rButton, 0); win.repaint(); win.setVisible(true); } } Can we make program with two buttons. One moving the circle left and another moving it right? Image courtesy http://www.flickr.com/photos/tmab2003/3464087290/sizes/z/in/photostream/

  6. A Solution import java.awt.event.*; import java.awt.*; public class LeftButtonextends 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 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); RightButtonrButton= new RightButton(5, 5, 100, 20, oval); win.add(rButton, 0); LeftButtonlButton = 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) • 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 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); RightButtonrButton= new RightButton(5, 5, 100, 20, oval); win.add(rButton, 0); LeftButtonlButton = 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(); } } 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. Image courtesy http://www.flickr.com/photos/tmab2003/3464087290/sizes/z/in/photostream/

  10. Another Way import java.awt.event.*; public class LeftButton extends EventButton{ private OvalControllercontroller; 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 RightButtonextends EventButton{ // other code omitted public void actionPerformed(ActionEvent e) { controller.moveRight(); } }

  11. EventTextField «constructor» + EventTextField( String txt) «query» + intgetX() + intgetY() + intgetWidth() + intgetHeight() + 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 ) Text Fields • A text field is a way for users to type in textual information. • An EventTextField is • A Component • A JTextField

  12. Example 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(); intnumericValue = new Scanner(fieldText).nextInt(); oval.setSize(numericValue, numericValue); } }

  13. The Client 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 • 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. EventSlider Crash Course EventSlider + static final intHORIZONTAL + static final intVERTICAL «constructor» + EventSlider( int, int, int, int ) «query» + intgetX() + intgetY() + intgetWidth() + intgetHeight() + intgetMaximum() + intgetMinimum() + Container getParent() + intgetValue() . . . «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 getValue() getMinimum() getMaximum() assigns new values to HORIZONTAL or VERTICAL getMinimum(), getMaximum() & getValue() called when user adjusts the knob or arrow buttons

  16. Example • 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 • 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 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; } 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; } 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; } Oval oval = new Oval(); Rectangle rect = new Rectangle(); SelectableRectangle rect2 = new ASelectableRectangle(); …isTall(rect)… …isTall(oval)… …isTall(rect2)…

  19. Dynamic Conformance Testing • The instanceof operator tests conformance • Syntax: • ExpressioninstanceofClassName • 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 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); }

More Related