Session 29 Introducing CannonWorld (Event-driven Programming)

Slides:



Advertisements
Similar presentations
Mari Göransson - KaU - Datavetenskap - DAVD11 1 Java Event Handling --
Advertisements

Mouse Listeners We continue our examination of GUIs by looking at how to interact with the mouse –Just as Java creates Events when the user interacts with.
Problem Solving 6 GUIs and Event Handling ICS-201 Introduction to Computing II Semester 071.
Deitel Ch 11-part 1 Java GUIs 1 Java GUIs (Deitel, Chap 14-part1) Focus: Attaching GUI components + event handling  input dialog => only one value for.
Event Handling. In this class we will cover: Basics of event handling The AWT event hierarchy Semantic and low-level events in the AWT.
1 lecture 12Lecture 13 Event Handling (cont.) Overview  Handling Window Events.  Event Adapters Revisited.  Introduction to Components and Containers.
Inner Classes. Inner classes All the classes so far have been “top level” It is possible (and useful) to define a class inside another class Inner classes.
More OOP Design Patterns
More on Creating GUIs in Java using Swing David Meredith Aalborg University.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
Session 9 CannonWorld, Event-driven Programming, and Border Layout.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Event Driven Programming, The.
Session 11 Border Layout, using Panels, Introduction to PinBallGame.
Session 10 CannonGame and Event-driven Programming.
Session 27 Swing vs. AWT. AWT (Abstract Window ToolKit) It is a portable GUI library for stand-alone applications and/or applets. The Abstract Window.
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
1 Block1 – unit 2 (The Case study in Budd 5-6).  create a small application that uses the Abstract Windowing Toolkit (AWT)  Swing packages to simulate.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
GUI Clients 1 Enterprise Applications CE00465-M Clients with Graphical User Interfaces.
Chapter 12 1 TOPIC 13B l Buttons and Action Listeners Window Interfaces Using Swing Objects.
CS324e - Elements of Graphics and Visualization Java GUIs - Event Handling.
Interactive Programs Java API. Terminology Event—an action or occurrence, not part of a program, detected by the program. Events can be Event—an action.
CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
2-Dec-15 Inner Classes. 2 Inner classes All the classes so far have been “top level” It is possible (and useful) to define a class inside another class.
Java Inner Classes. Interfaces interface KeyListener { public void keyPressed(KeyEvent e); public void keyReleased(KeyEvent e); public void keyTyped(KeyEvent.
2-Dec-15 Inner Classes By Alguien Soy. 2 Inner classes All the classes so far have been “top level” It is possible (and useful) to define a class inside.
Object Oriented Programming.  Interface  Event Handling.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
1 Event Driven Programs with a Graphical User Interface Rick Mercer.
A cannon game ?. Simple version angle from command line, one shot only Coordinate system is “upside-down”: Use dy(int) method to transform y coordinate:
1 Layout Managers Layout managers –Provided for arranging GUI components –Provide basic layout capabilities –Processes layout details –Programmer can concentrate.
Lecture 261 CS110 Lecture 26 Thursday, May 6, 2004 This lecture not delivered from slides Announcements –final exam Thursday, May 20, 8:00 AM McCormack,
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Event Handling CS 21a: Introduction to Computing I First Semester,
View  view  presents the user with a sensory (visual, audio, haptic) representation of the model state  a user interface element (the user interface.
MIT AITI 2004 Swing Event Model Lecture 17. The Java Event Model In the last lecture, we learned how to construct a GUI to present information to the.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
Event Driven (Asynchronous) Programming. Event handling in Unity Subclass a class that contains event handling methods, and then override those methods.
Inner Classes.
GUIs and Events Rick Mercer.
CSC 205 Programming II Lecture 5 AWT - I.
Topic: Inner Classes Course : JAVA PROGRAMMING Paper Code: ETCS-307 Faculty : Dr. Prabhjot Kaur Reader, Dept. of IT 1.
Chapter 14 Event-Driven Programming
CompSci 230 S Programming Techniques
Inner Classes 27-Dec-17.
Java Events. Java Events Important definitions Overridden method Class vs. abstract class vs. interface Event Handling Definition Main components Windows.
A First Look at GUI Applications
Java Programming: From Problem Analysis to Program Design,
Ellen Walker Hiram College
Event-driven programming for GUI
Event Driven Programming
Inner Classes 11/14/ Dec-04 inner_classes.ppt.
Inheritance, Polymorphism, and Interfaces
Inner Classes 29-Nov-18.
Java Inner Classes.
Graphical User Interfaces in Java Event-driven programming
Events, Event Handlers, and Threads
Border Layout, using Panels, Introduction to PinBallGame
Inner Classes 17-Apr-19.
Inner Classes 21-Apr-19.
Inner Classes 1-May-19.
Inner Classes 11-May-19.
Inner Classes 18-May-19.
Inner Classes.
Inner Classes 25-Oct-19.
Presentation transcript:

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

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.

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.

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.

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.”

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.

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(); }

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(); }

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 ); }

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

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(); }

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

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.

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.

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!

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?

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.