Session 10 CannonGame and Event-driven Programming.

Slides:



Advertisements
Similar presentations
1 Graphical User Interface (GUI) Applications Abstract Windowing Toolkit (AWT) Events Handling Applets.
Advertisements

Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
CS18000: Problem Solving and Object-Oriented Programming.
Graphic User Interfaces Layout Managers Event Handling.
Session 9 MultiballWorld, Refactoring Ball using Inheritence, and Intro. to CannonWorld.
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
Event Driven Programming and GUIs Part 3 CS221 – 4/15/09.
June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 1 Lecture 9 Object Oriented Programming in Java Advanced Topics Abstract Windowing.
Event Handling. In this class we will cover: Basics of event handling The AWT event hierarchy Semantic and low-level events in the AWT.
GUI and Event-Driven Programming Recitation – 3/6/2009 CS 180 Department of Computer Science, Purdue University.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Scott Grissom, copyright 2006Ch 11: GUI Slide 1 Graphical User Interfaces (Ch 11) Careful design of a graphical user interface is key to a viable software.
1 Object Oriented Design & Patterns Part 1. 2 Design Patterns Derived from architectural patterns: –rules for design of buildings –describe common problems,
Java Programming Chapter 10 Graphical User Interfaces.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Welcome to CIS 083 ! Events CIS 068.
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
Session 9 CannonWorld, Event-driven Programming, and Border Layout.
Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
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,
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
C13a, AWT Create, display, facilitate user interaction with window objects software framework: a way of structuring generic solutions to common problems.
Session 11 Border Layout, using Panels, Introduction to PinBallGame.
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.
 2002 Prentice Hall, Inc. All rights reserved Introduction Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
1 Outline 1 Introduction 2 Overview of Swing Components 3 JLabel 4 Event Handling 5 TextFields 6 How Event Handling Works 7 JButton 8 JCheckBox and JRadioButton.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
Timer class and inner classes. Processing timer events Timer is part of javax.swing helps manage activity over time Use it to set up a timer to generate.
Swing Differences between Swing and AWT Naming Conventions All Swing components begin with a capital J -- JPanel, JButton, JScrollBar, JApplet, etc..
Layout Managers Arranges and lays out the GUI components on a container.
Computer Science [3] Java Programming II - Laboratory Course Lab 4: Common GUI Event Types and Listener Interfaces Layout Mangers Faculty of Engineering.
CS324e - Elements of Graphics and Visualization Java GUIs - Event Handling.
Lec.10 (Chapter 8 & 9) GUI Jiang (Jen) ZHENG June 27 th, 2005.
Session 19 Chapter 10 – Mechanisms for Software Reuse.
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.
The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include: a rich set of user interface components; a.
Object Oriented Programming.  Interface  Event Handling.
SEEM3460 Tutorial GUI in Java. Some Basic GUI Terms Component (Control in some languages) the basic GUI unit something visible something that user can.
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
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.
CSI 3125, Preliminaries, page 1 Layout Managers. CSI 3125, Preliminaries, page 2 Layout Managers The LayoutManagers are used to arrange components in.
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.
Graphical User Interface (GUI)
User Interface Components. Layout Placement of UI components in a window – Size & Position Each component occupies a rectangular region in the window.
CSC 205 Programming II Lecture 7 AWT – Event Handling & Layout.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
AWT Vs SWING. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many applications Uses.
Session 8 Lab 4 comments, MultiballWorld, Refactoring Ball using Inheritence, and Intro. to CannonWorld.
GUIs and Events Rick Mercer.
CSC 205 Programming II Lecture 5 AWT - I.
Advanced GUIs II CS Lecture
Chap 7. Building Java Graphical User Interfaces
Graphical User Interfaces -- Introduction
Timer class and inner classes
GUI building with the AWT
Border Layout, using Panels, Introduction to PinBallGame
GUI building with the AWT
Advanced GUIs II CS Lecture
Session 29 Introducing CannonWorld (Event-driven Programming)
Graphical User Interface
Presentation transcript:

Session 10 CannonGame and Event-driven Programming

The CannonGame Application What color is the cannon ball? What is the role of dy()? How does the CannonGame draw the cannon? What do lv, lh, sv, and sh mean? What about sx and sy? How does the cannon ball follow the prescribed angle, when we don’t pass the angle to the cannon ball? How does the cannon ball reverse direction? How does the game know that the ball has hit something (either the target or the floor)? How does the program terminate?

The CannonGame Application Let’s go to the tape, Jim…

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.

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 this semester.

An Example: The CannonWorld Budd has 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.

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

Listeners in the CannonWorld Each component needs a listener to wait for the user to manipulate and then relay the event to the CannonWorld. private Scrollbar 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" ); Button fire = new Button( "fire" ); fire.addActionListener( new FireButtonListener() ); add( "North", fire ); slider = new Scrollbar( Scrollbar.VERTICAL, angle, 5, 0, 90 ); slider.addAdjustmentListener( new ScrollBarListener() ); add( "East", slider ); }

Listeners as Inner Classes What is an inner class? Any class defined within another class definition: public class CannonWorld extends Frame {... 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.

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.

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

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 Frame {... 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.

An Exercise Add to our CannonWorld a button that allows the user to change the color of the cannonball from blue to red to yellow to blue.... Place the button on the lefthand side of the window.

Steps to the solution: 1. Add a button to the CannonWorld. 2. Add a listener class to CannonWorld. The listener’s actionPerformed method needs to tell the ball to take the next color. I could have written an if expression that toggles the colors in sequence, but... Shouldn’t the ball be controlling its own color, rather than the application using the ball? So I decided to have the CannonBall respond to a nextColor() message and toggle its own color.

More on a Solution to the Exercise The next question is, do I add a nextColor() method directly to the CannonBall class, or do I create a subclass? In general, if we need a new kind of object, then we should create a new kinds of object. (If a particular method turns out to be more generally useful than we anticipate, then we can always refactor the solution so that the method lives higher in the class hierarchy.) So: 3. Implement a ChangingCannonBall class. 4. Use instances of ChangingCannonBall in the CannonWorld.

Layout Managers Frame is a subclass of the Container class –so it can hold objects such as buttons, scroll bars, etc. –layout manager assigns locations to these objects within the container –the default LayoutManager is BorderLayout North South CenterWestEast

Layout Managers Five standard types of layout managers: –BorderLayout –GridLayout - creates an rectangular array of components –FlowLayout - places components in rows left to right, top to bottom –CardLayout - stacks components vertically with only one visible at any one time –GridBagLayout - most general, but most complex. It allows for a nonuniform grid of squares with components placed in various positions within each square.

Using Panels with BorderLayout BorderLayout seems very restrictive, but you can use a panel to hold several components in each “direction”.