Transcript Document
Inheritance for Event Handling
Some classes include their own event handler method(s).
Such classes...
1) inherit the class with the event handler
2) override the event handler in the subclass
Example
public class Director extends ThreeButtons {
...
public void leftAction( ) {
...
}
public void midAction( ) {
...
}
public void rightAction( ) {
...
}
...
}
© 2006 Pearson Addison-Wesley. All rights reserved
8.4.1
EventButton
javax.swing.JButton
«constructor»
+ Button( String )
To Handle Events with EventButton:
1) inherit EventButton
2) override ___________________
«query»
+ int getX( )
+ int getY( )
+ int getWidth( )
+ int getHeight( )
+ String getLabel( )
«update»
+ void repaint()
+ void setBounds( int, int , int, int)
+ void setSize( int, int )
+ void setLocation( int, int )
+ void setLabel( String )
+ void setBackground( Color )
+ void setForeground( Color )
EventButton
«constructor»
+ EventButton(String)
«event handler»
+ void actionPerformed( java.awt.event.ActionEvent )
© 2006 Pearson Addison-Wesley. All rights reserved
8.4.2
Using EventButton to move a circle.
This problem is to use a button to move a circle to its right by 5 pixels.
Solution 1: The button contains a dot.
Driver
dotBtn :
OvalButton
dot : Oval
© 2006 Pearson Addison-Wesley. All rights reserved
8.4.3
Using EventButton to move a circle.
import javax.swing.JFrame;
public class Driver {
private OvalButton dotBtn;
private JFrame win;
public Driver() {
win = new JFrame();
win.setBounds(0,0,200,200);
win.setLayout(null);
win.setVisible(true);
dotBtn = new OvalButton(“Move”);
win.add(dotBtn, 0);
dotBtn.addDotTo(win);
win.repaint();
}
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
public class OvalButton
extends EventButton {
private Oval dot;
public OvalButton(String s) {
super(s);
setBounds(10, 100, 100, 25);
dot = new Oval(0, 0, 30, 30);
}
public void addDotTo(JFrame jf) {
jf.add(dot, 0);
}
}
public void actionPerformed
(ActionEvent e) {
dot.setLocation(dot.getX()+5, 0);
dot.repaint();
}
}
© 2006 Pearson Addison-Wesley. All rights reserved
8.4.4
Using two EventButtons to move a dot
Including the dot as an instance variable of the button, doesn’t always work.
Consider the problem of two buttons - one to move left and one right.
Solution: The buttons could share a dot from the Driver.
Driver
btnL :
LeftButton
dot : Oval
btnR :
RightButton
This is a typical sharing pattern in which the shared object is passed
as a parameter to the other objects that need shared access.
© 2006 Pearson Addison-Wesley. All rights reserved
8.4.5
import javax.swing.JFrame;
How does the RightButton class
differ from LeftButton?
public class Driver {
private LeftButton btnL;
private RightButton btnR;
private Oval dot;
private JFrame win;
public Driver() {
win = new JFrame();
win.setBounds(0,0,200,200);
win.setLayout(null);
win.setVisible(true);
dot = new Oval(0, 0, 30, 30);
win.add(dot, 0);
btnL = new LeftButton(dot);
win.add(btnL, 0);
btnR = new RightButton(dot);
win.add(btnR, 0);
win.repaint();
}
import java.awt.event.ActionEvent;
public class LeftButton
extends EventButton {
private Oval dot;
public LeftButton(Oval d) {
super(“<-”);
dot = d;
setBounds(10, 100, 50, 25);
}
public void actionPerformed
(ActionEvent e) {
dot.setLocation(dot.getX()-5, 0);
dot.repaint();
}
}
}
© 2006 Pearson Addison-Wesley. All rights reserved
8.4.6
Another way to access a common object
As an alternative to passing the identity of a shared object, why not provide
the appropriate methods to update the object?
Solution: The Driver contains the object, and buttons
must know the identity of the Driver.
Driver
btnL :
LeftButton
dot : Oval
btnR :
RightButton
This is called a callback pattern because the buttons can only
alter circle by way of calling back to the Driver that creates them.
© 2006 Pearson Addison-Wesley. All rights reserved
8.4.7
import javax.swing.JFrame;
public class Driver {
private LeftButton btnL;
private RightButton btnR;
private Oval dot;
private JFrame win;
public Driver() {
win = new JFrame();
win.setBounds(0,0,200,200);
win.setLayout(null);
win.setVisible(true);
dot = new Oval(0, 0, 30, 30);
win.add(dot, 0);
btnL = new LeftButton(this);
win.add(btnL, 0);
btnR = new RightButton(this);
win.add(btnR, 0);
win.repaint();
}
public void moveDotLeft() {
dot.setLocation(dot.getX()-5, 0);
}
public void moveDotRight() {
dot.setLocation(dot.getX()+5, 0);
}
import java.awt.event.ActionEvent;
public class LeftButton
extends EventButton {
private Driver theDriver;
public LeftButton(Driver d) {
super(“<-”);
setBounds(10, 100, 50, 25);
theDriver = d;
}
public void actionPerformed
(ActionEvent e) {
theDriver.moveDotLeft();
}
}
}
© 2006 Pearson Addison-Wesley. All rights reserved
8.4.8
The Identity of the Event Generating Object?
Recall that it is the Java Virtual Machine that activates an event handler.
The JVM also passes a parameter that can be used to obtain info about the event.
EventButton
«constructor»
+ EventButton(String)
«event handler»
+ void actionPerformed( java.awt.event.ActionEvent )
To see what information is available, we need to examine ActionEvent.
java.awt.event.ActionEvent
«query»
+ Object getSource()
+ String toString()
© 2006 Pearson Addison-Wesley. All rights reserved
8.4.9
import javax.swing.JFrame;
import java.awt.event.ActionEvent;
public class Driver {
private OneButton btnL, btnR;
private Oval dot;
private JFrame win;
public Driver() {
win = new JFrame();
win.setBounds(0,0,200,200);
win.setLayout(null);
win.setVisible(true);
dot = new Oval(0, 0, 30, 30);
win.add(dot, 0);
btnL = new OneButton(this, “<-”, 10);
win.add(btnL, 0);
btnR = new OneButton(this, “->”, 100);
win.add(btnR, 0);
win.repaint();
}
public void moveDot(ActionEvent e) {
if (e.getSource() == btnL)
dot.setLocation(dot.getX()-5, 0);
else // e.getSource()==btnR
dot.setLocation(dot.getX()+5, 0);
}
import java.awt.event.ActionEvent;
public class OneButton
extends EventButton {
private Driver theDriver;
public OneButton
(Driver d, String s, int x) {
super(s);
setBounds(x, 100, 50, 25);
theDriver = d;
}
public void actionPerformed
(ActionEvent e) {
theDriver.moveDot(e);
}
}
}
© 2006 Pearson Addison-Wesley. All rights reserved
8.4.10