Interactive Object Movement with Event Buttons in Java
This guide illustrates how to create an interactive Java application that utilizes event-driven programming to move a graphical object (an oval/circle) on screen. We will define custom event buttons for left and right movement of the object. Clicking the buttons will invoke their respective action handlers, demonstrating the use of event listeners and the Java Swing framework. The tutorial includes step-by-step code implementations for both the button actions and the object movement logic, ensuring an engaging way to deepen your understanding of GUI programming in Java.
Interactive Object Movement with Event Buttons in Java
E N D
Presentation Transcript
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 «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
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(); } }
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/
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(); } }
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/
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.
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/
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(); } }
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
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); } }
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/
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]
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
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.
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
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)…
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
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); }