Presentation is loading. Please wait.

Presentation is loading. Please wait.

Session 29 Introducing CannonWorld (Event-driven Programming)

Similar presentations


Presentation on theme: "Session 29 Introducing CannonWorld (Event-driven Programming)"— Presentation transcript:

1 Session 29 Introducing CannonWorld (Event-driven Programming)
Action Listeners, Interfaces

2 The CannonWorld We have added two new objects to our cannon game:
a button at the top of the window. When the user presses the button, the cannon fires a cannonball. a slider at the right. When the user adjusts the position of the slider, the cannon changes the angle at which it fires.

3 Event-Driven Programming
Object-oriented programming shifts program control from a top-level “main” program into a set of collaborating objects. This new emphasis also makes it easier for us to write programs that shift control from the program to the user.

4 Event-Driven Programming
This style of programming is called event-driven because it builds program control around one or more events that the user causes. The program retains low-level control of how to respond to each kind of event that the user can initiate. Event-driven programming can also be done in environments with little or no user interaction. In such environments, the events are generated by other programs: objects that generate events to request services.

5 An Example: The CannonWorld
The user can cause these events at any time. The user controls the flow of the program, not the game world. Control of the game changes from “Do this , then that, then this other thing.” to “Respond to the user’s action.”

6 Event-Driven Programming
In Java, the programmer defines objects called listeners that wait for and respond to user-initiated events. A listener can be attached to any object capable of generating an event caused by the user: the mouse moving the mouse pressing and releasing mouse buttons the keyboard pressing any key active components such as buttons and sliders We can also attach listeners to non-UI components, but we won’t write that sort of program until much later in the semester.

7 Listeners in the CannonWorld
Each GUI component needs a listener to wait for the user to manipulate and then relay the event to the CannonWorld. private JScrollBar slider; private class ScrollBarListener implements AdjustmentListener { public void adjustmentValueChanged( AdjustmentEvent e ){ angle = slider.getValue(); message = "Angle: " + angle; repaint(); }

8 Listeners in the CannonWorld
private class FireButtonListener implements ActionListener { public void actionPerformed( ActionEvent e ) { double radianAngle = angle * Math.PI / 180.0; double sinAngle = Math.sin(radianAngle); double cosAngle = Math.cos(radianAngle); cannonBall = new CannonBall( 20 + (int) (30 * cosAngle), dy(5+(int) (30 * sinAngle)), 5, 12 * cosAngle, -12 * sinAngle ); repaint(); }

9 Listeners in the CannonWorld
public CannonWorld() { setSize ( FrameWidth, FrameHeight ); setTitle( "Cannon Game" ); slider = new JScrollBar( Scrollbar.VERTICAL, angle, 5, 0, 90 ); slider.addAdjustmentListener( new ScrollBarListener() ); getContentPane(). add( "East", slider ); JButton fire = new JButton( "fire" ); fire.addActionListener( new FireButtonListener() ); add( "North", fire ); }

10 Temporary Sidebar… Why do we use inheritance?
The subclass inherits the implementation of the super class This allows us to avoid code duplication The subclass inherits the “interface” of the super class This allows us to provide substitutability through polymorphism So what do we do when we want to inherit the interface WITHOUT any implementation? Interfaces

11 Interfaces How do we create one?
Just like a class, without the method bodies: public interface Shape { public void paint(Graphics g); public double getArea(); }

12 Listeners Implement Interfaces
How do we create one? Just like a class, without the method bodies: public interface AdjustmentListener { public void adjustmentValueChanged( AdjustmentEvent e ); }

13 Listeners Implement Interfaces
Examples ScrollBarListener implements AdjustmentListener FireButtonListener implements ActionListener What is an interface? A list of responsibilities. A set of messages to which an object promises to respond. Sometimes called a protocol. Like a class with no behavior.

14 Why Use Interfaces Why do we use interfaces in this program?
• When the user presses a button, the Java run-time system sends an actionPerformed() message to any object that is listening for the button’s events. If we want our listener to listen, it must listen for that particular message. • When the user adjusts a slider, the Java run-time system sends an adjustmentValueChanged() message to any object that is listening for the slider’s events. If we want our listener to listen, it must listen for that particular message.

15 Why Use Interfaces More generally, why do we use interfaces?
• To allow our objects to work inside an existing framework. • To allow programmers to create objects that fulfill responsibilities — without committing to how their objects do so!

16 Listeners as Inner Classes
What is an inner class? Any class defined within another class definition: public class CannonWorld extends CloseableFrame { ... private class FireButtonListener ... { } How do we create one? “Just do it.” Why do you suppose FireButtonListener is private?

17 Listeners as Inner Classes
Why do we create them? • For convenient access to the private members of the outer class. • For simple collaboration between pure masters and slaves. • Almost only for listeners. Notice that one .java file can produce many .class files.


Download ppt "Session 29 Introducing CannonWorld (Event-driven Programming)"

Similar presentations


Ads by Google