Session 11 Border Layout, using Panels, Introduction to PinBallGame.

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.
Programming in Java; Instructor:John Punin Graphics and Graphical User Interfaces1 Programming in Java Graphics and Graphical User Interfaces.
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.
CS18000: Problem Solving and Object-Oriented Programming.
Graphic User Interfaces Layout Managers Event Handling.
Graphical User Interfaces, 2D Graphics & Game Programming.
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.
Event Handling Events and Listeners Timers and Animation.
Lecture 18 Review the difference between abstract classes and interfaces The Cloneable interface Shallow and deep copies The ActionListener interface,
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.
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.
Session 12 Introduction to PinBallGame (Chaper 7).
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.
Chapter 11 Java AWT Part I: Mouse Events (Optional) Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin,
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
Java GUIs and Graphics CNS Outline  Introduction  Events  Components  Layout managers  Drawing  Introduction  Events  Components  Layout.
3461A Readings from the Swing Tutorial. 3461A Overview  The follow is the Table of Contents from the trail “Creating a GUI with JFC/Swing” in the “The.
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 21 Chapter 10: Mechanisms for Software Reuse.
Java GUI’s are event driven, meaning they generate events when the user interacts with the program. Typical events are moving the mouse, clicking a mouse.
Session 15 Chapter 8: Understanding Inheritance. Lab Exercise Solution Recall that we needed to know if a mousePress occurred on a target. We tried to.
Session 10 CannonGame and Event-driven Programming.
 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.
Chapter 7: Pinball Game Construction Kit. Vectors Example of a “collection class” Must “import java.util.Vector” More flexible than arrays: will grow.
Layout Managers Arranges and lays out the GUI components on a container.
Session 16 Pinball Game Construction Kit:. Pinball Version 1 Replaced the fire button with a mouse event. Multiple balls can be in the air at once. –Uses.
Computer Science [3] Java Programming II - Laboratory Course Lab 4: Common GUI Event Types and Listener Interfaces Layout Mangers Faculty of Engineering.
Object-Oriented Software Engineering
Lec.10 (Chapter 8 & 9) GUI Jiang (Jen) ZHENG June 27 th, 2005.
Session 19 Chapter 10 – Mechanisms for Software Reuse.
AGDER COLLEGEFACULTY OF ENGINEERING & SCIENCE GUI Components ikt403 – Object-Oriented Software Development.
UID – Event Handling and Listeners Boriana Koleva
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.
Session 18 Chapter 8: Understanding Inheritance. Recall Exercise 2 From Tuesday It’s very annoying to move a target from the pallet and drop it in the.
Week 6: Basic GUI Programming Concepts in Java Example: JFrameDemo.java container : a screen window/applet window/panel that groups and arranges components.
Object Oriented Programming.  Interface  Event Handling.
Session 13 Pinball Game Construction Kit (Version 3):
Session 25 Test 2 Review. Test 2 Details Tuesday, November 22 in class –(Anybody planning on taking it on Friday?) Closed book Closed notes, except for.
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.
Session 7 Introduction to Inheritance. Accumulator Example a simple calculator app classes needed: –AdderApp - contains main –AddingFrame - GUI –CloseableFrame.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
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.
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
Chap 7. Building Java Graphical User Interfaces
Chapter 13: Advanced GUIs and Graphics
Graphical User Interfaces -- Introduction
Introduction to Computing Using Java
GUI Programming using Java - Mouse Events
Border Layout, using Panels, Introduction to PinBallGame
Session 29 Introducing CannonWorld (Event-driven Programming)
Graphical User Interface
Presentation transcript:

Session 11 Border Layout, using Panels, Introduction to PinBallGame

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.

An Exercise Place the button on the left-hand side of the window. public CannonWorld() {... Button changeColor = new Button( "change ball color" ); changeColor.addActionListener( new ChangeColorButtonListener() ); add( "West", changeColor ); }

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

Class Interaction Relationships between classes: 1. composition –A Ball contains a Color. –An instance of Color is a part of a Ball. 2. inheritance –A MovableBall is a kind of Ball. –A CannonWorld is a kind of Frame. 3. association –A BoundedBall interacts with the Frame that contains it. –A CannonWorld interacts with a ColorWheel.

Pinball Game Construction Kit Study a cleaned up and refactored version of Budd’s code from Section 7.1 Construct a list of three questions (to turn in) that you have about this code. Your questions can be about how it works, what some piece of code in the program does, or even whey the code is written in a particular way.

Some cool new features Replaced the fire button with a mouse event. Multiple balls can be in the air at once. Control is no longer in the paint method. But before we talk about what Budd did well…

Say It Once and Only Once! In the interest of simplifying the number of files we need to run a program, Budd has repeated code that can be reused by inheritance (figure 7.3, p 102). Gravity affects a PinBall in the same way that it affects a CannonBall. The only differences between a PinBall and a CannonBall are its size and its initial trajectory. So, we can say: public class PinBall extends CannonBall { public PinBall( int sx, int sy ) { super( sx, sy, 10,-2 + Math.random()/4, -15 ); } Inheritance makes our job easier—as well as isolating repeated code.

Looks the same, but it’s not. Replaced the fire button with a mouse event. Multiple balls can be in the air at once. Control is no longer in the paint method.

Handling Mouse Events The CannonWorld handles only events that are generated by the active components — a button and a slider — that we added to it. More generally, though, we will want to trap and respond to any mouse action, anywhere within the frame. Any listener that wishes to monitor mouse activity must implement the MouseListener interface:

public interface MouseListener { public void mouseClicked ( MouseEvent e ); public void mouseEntered ( MouseEvent e ); public void mouseExited ( MouseEvent e ); public void mousePressed ( MouseEvent e ); public void mouseReleased( MouseEvent e ); } The MouseListener interface is part of the package java.awt.event. It specifies what an object must do to be a mouse listener within the Java Event Model.

How the MouseListener works. Think back to how we implemented Button listeners: private class FireButtonListener implements ActionListener { We can do a similar thing for MouseListeners… private class PinballGameListener implements MouseListener{ However, to implement an interface, the Java language insists that the programmer provide a definition for all operations.

MouseAdapter to the rescue To simplify this process, the Java library provides a simple class named Mouse Adapter. The class MouseAdapter implements the MouseListener interface, but uses an empty method for each method. Rather than implement a MouseListener, we may choose to implement a MouseAdapter. (Let’s go back to the code (also p 104))

Mouse Events in the Pin Ball Game In the PinBallGame class, we have the following class relationship: MouseListener ^ | | implements | MouseAdapter ^ | | extends | MouseKeeper

What does a MouseKeeper do?

–If the mouse is pressed in the“shooting area”, then it creates and launches a new pinball.

What does a MouseAdapter do?

–Nothing, in response to any sort of mouse event.

Why do you suppose that Java’s creators call it an “adapter”?

–They use a common design from the world as an analogy.

Why do you suppose Java’s creators bothered to define the MouseListener interface? Why not have everyone extend MouseAdapter?

–Inheritance is not free. Why force programmers who plan to implement most or all of the interface to pay the extra price?