Name: Checked: Learn about listeners, events, and simple animation for interactive graphical user interfaces.

Similar documents
CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

ANSWER KEY Exam 2 Computer Programming 230 Dr. St. John Lehman College City University of New York Thursday, 5 November 2009

Chapter 4 Writing Classes

Graphical Applications

Designing Classes Part 2

AP CS Unit 11: Graphics and Events

MIT AITI Swing Event Model Lecture 17

Adding Buttons to StyleOptions.java

Chapter. We've been using predefined classes. Now we will learn to write our own classes to define objects

DEMYSTIFYING PROGRAMMING: CHAPTER SIX METHODS (TOC DETAILED) CHAPTER SIX: METHODS 1

G51PGP Programming Paradigms. Lecture 008 Inner classes, anonymous classes, Swing worker thread

Command-Line Applications. GUI Libraries GUI-related classes are defined primarily in the java.awt and the javax.swing packages.

Class 16: The Swing Event Model

Systems Programming. Bachelor in Telecommunication Technology Engineering Bachelor in Communication System Engineering Carlos III University of Madrid

Building a GUI in Java with Swing. CITS1001 extension notes Rachel Cardell-Oliver

Designing Classes. Where do objects come from? Where do objects come from? Example: Account datatype. Dr. Papalaskari 1

The JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class

To gain experience using GUI components and listeners.

Frames, GUI and events. Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling

Programming Language Concepts: Lecture 8

University of Cape Town Department of Computer Science. Computer Science CSC117F Solutions

SINGLE EVENT HANDLING

CS Exam 3 - Spring 2010

CSIS 10A Assignment 7 SOLUTIONS

Part I: Learn Common Graphics Components

CS Exam 3 - Spring 2010

CS 134 Programming Exercise 7:

First Name: AITI 2004: Exam 2 July 19, 2004

Introduction. Introduction

RAIK 183H Examination 2 Solution. November 10, 2014

G51PGP Programming Paradigms. Lecture 009 Concurrency, exceptions

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

First Name: AITI 2004: Exam 2 July 19, 2004

Chapter 5: Enhancing Classes

References. Chapter 5: Enhancing Classes. Enhancing Classes. The null Reference. Java Software Solutions for AP* Computer Science A 2nd Edition

RAIK 183H Examination 2 Solution. November 11, 2013

CMSC 150 Lab 8, Part II: Little PhotoShop of Horrors, Part Deux 10 Nov 2015

Starting Out with Java: From Control Structures Through Objects Sixth Edition

G51PRG: Introduction to Programming Second semester Applets and graphics

Introduction This assignment will ask that you write a simple graphical user interface (GUI).

AP Computer Science Unit 13. Still More Graphics and Animation.

COMP-202 Unit 10: Basics of GUI Programming (Non examinable) (Caveat: Dan is not an expert in GUI programming, so don't take this for gospel :) )

IT101. Graphical User Interface

CSI Introduction to Software Design. Prof. Dr.-Ing. Abdulmotaleb El Saddik University of Ottawa (SITE 5-037) (613) x 6277

1.00/1.001 Introduction to Computers and Engineering Problem Solving Fall (total 7 pages)

Swing - JButton. Adding buttons to the main window

ITEC 120 4/14/11. Review. Need. Objectives. GUI basics JFrame JPanel JLabel, JButton, JTextField Layout managers. Lecture 38 GUI Interactivity

Systems Programming Graphical User Interfaces

What Is an Event? Some event handler. ActionEvent. actionperformed(actionevent e) { }

Java Help Files. by Peter Lavin. May 22, 2004

import javax.swing.jframe; import javax.swing.joptionpane;

Final Exam CS 251, Intermediate Programming December 13, 2017

Learning objectives: Enhancing Classes. CSI1102: Introduction to Software Design. More about References. The null Reference. The this reference

class BankFilter implements Filter { public boolean accept(object x) { BankAccount ba = (BankAccount) x; return ba.getbalance() > 1000; } }

CSC207 Week 4. Larry Zhang

Swing/GUI Cheat Sheet

Graphical User Interface (Part-1) Supplementary Material for CPSC 233

AP CS Unit 12: Drawing and Mouse Events

Advanced Java Unit 6: Review of Graphics and Events

DEMYSTIFYING PROGRAMMING: CHAPTER FOUR

GUI Forms and Events, Part II

Graphical User Interfaces

Control Flow: Overview CSE3461. An Example of Sequential Control. Control Flow: Revisited. Control Flow Paradigms: Reacting to the User

Java, Swing, and Eclipse: The Calculator Lab.

An array is a type of variable that is able to hold more than one piece of information under a single variable name.

Swing from A to Z Some Simple Components. Preface

Parts of a Contract. Contract Example. Interface as a Contract. Wednesday, January 30, 13. Postcondition. Preconditions.

&KDSWHU(QKDQFLQJ&ODVVHV

Interfaces & Polymorphism part 2: Collections, Comparators, and More fun with Java graphics

Assignment #3 (Due 4/4/2017)

EXCEPTIONS & GUI. Errors are signals that things are beyond help. Review Session for. -Ankur Agarwal

More About Objects and Methods

Java Swing. based on slides by: Walter Milner. Java Swing Walter Milner 2005: Slide 1

This exam is closed textbook(s) and closed notes. Use of any electronic device (e.g., for computing and/or communicating) is NOT permitted.

CS 3331 Advanced Object-Oriented Programming. Final Exam

Chapter 6: Graphical User Interfaces

CS 2113 Software Engineering

Java - Applets. public class Buttons extends Applet implements ActionListener

Programming Languages and Techniques (CIS120)

CS 251 Intermediate Programming GUIs: Event Listeners

Topic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more

Topic 9: Swing. Why are we studying Swing? GUIs Up to now: line-by-line programs: computer displays text user types text. Outline. 1. Useful & fun!

Queen s University Faculty of Arts and Science School of Computing CISC 124 Final Examination December 2004 Instructor: M. Lamb

Graphics User Defined Forms, Part I

We are on the GUI fast track path

GUI DYNAMICS Lecture July 26 CS2110 Summer 2011

CS180 Spring 2010 Exam 2 Solutions, 29 March, 2010 Prof. Chris Clifton

CSC 160 LAB 8-1 DIGITAL PICTURE FRAME. 1. Introduction

Points Missed on Page page 1 of 8

Queens College, CUNY Department of Computer Science. CS 212 Object-Oriented Programming in Java Practice Exam 2. CS 212 Exam 2 Study Guide

Agenda. Container and Component

Project 1. LibraryTest.java. Yuji Shimojo CMSC 335

CS 11 java track: lecture 3

CS56 final (E03) W15, Phill Conrad, UC Santa Barbara Wednesday, 03/18/2015. Name: Umail umail.ucsb.edu. Circle one: 4pm 5pm 6pm

CS180 Recitation. More about Objects and Methods

Java & Graphical User Interface II. Wang Yang wyang AT njnet.edu.cn

Block I Unit 2. Basic Constructs in Java. AOU Beirut Computer Science M301 Block I, unit 2 1

2IS45 Programming

H212 Introduction to Software Systems Honors

Transcription:

Lab 15 Name: Checked: Objectives: Learn about listeners, events, and simple animation for interactive graphical user interfaces. Files: http://www.csc.villanova.edu/~map/1051/chap04/smilingface.java http://www.csc.villanova.edu/~map/1051/chap04/smilingfacepanel.java http://www.csc.villanova.edu/~map/1051/chap04/pushcounter.java http://www.csc.villanova.edu/~map/1051/chap04/pushcounterpanel.java http://www.csc.villanova.edu/~map/1051/chap09/rebound.java http://www.csc.villanova.edu/~map/1051/chap09/reboundpanel.java http://www.csc.villanova.edu/~map/1051/chap09/happyface.gif A. Adding a button to SmilingFacePanel.java: Before you begin, download and compile SmilingFace.java and SmilingFacePanel.java; run SmilingFace.java and observe the panel it creates. 1. Import the class JButton from the package javax.swing, i.e., add the import statement: import javax.swing.jbutton; 2. Add an instance variable for the button let s call it clicker private JButton clicker; 3. In the SmilingFacePanel constructor, instantiate a JButton object and assign it to clicker; then add the button to the panel. clicker = new JButton("click here"); add (clicker); clicker represents a button that may be associated with an action. For the moment it does nothing when clicked, but compile your program and run it anyway. You should see the same picture, but with a button on it. When you click on the button, nothing happens. Next, we will fix this - make something happen...

B. Experiment with PushCounterPanel: Download and compile PushCounter.java and PushCounterPanel.java; run PushCounter and observe the behavior of the button. The PushCounterPanel.java uses a JButton object. Just like our program, above, it is instantiated and added to the panel in the constructor. //******************************************************************** // PushCounterPanel.java Authors: Lewis/Loftus // Demonstrates a graphical user interface and an event listener. //******************************************************************** import java.awt.*; import java.awt.event.*; import javax.swing.*; public class PushCounterPanel extends JPanel private int count; private JButton push; private JLabel label; //----------------------------------------------------------------- // Constructor: Sets up the GUI. //----------------------------------------------------------------- public PushCounterPanel () count = 0; push = new JButton ("Push Me!"); push.addactionlistener (new ButtonListener()); label = new JLabel ("Pushes: " + count); add (push); add (label); setpreferredsize (new Dimension(300, 40)); setbackground (Color.cyan); //***************************************************************** // Represents a listener for button push (action) events. //***************************************************************** private class ButtonListener implements ActionListener //-------------------------------------------------------------- // Updates the counter and label when the button is pushed. //-------------------------------------------------------------- public void actionperformed (ActionEvent event) count++; label.settext("pushes: " + count);

Associating an action with a button The action(s) associated with clicking on a button are handled through something called an ActionListener. Observe that the PushCounterPanel class contains, inside it, the definition of another class: the ButtonListener class (the class that handles the event that a button gets pushed in the PushCounterPanel class). Because it is defined inside thepushcounterpanel.java class, ButtonListener class is called an inner class. The ButtonListener class only has one method, actionperformed() that specifies what has to happen (in this case a counter is incremented and the text in a label is replaced with the new value of the counter). Before proceeding, answer/do the following: 1. What is the name of the variable representing the button in PushCounterPanel class? 2. What is the name of the variable representing the button in the SmilingFacePanel class (as modified in part A above)? 3. Circle the line of code that associates the action with the button in PushCounterPanel class. Copy it out here: 4. Modify actionperformed() (method in the inner class) so that when the button is clicked, the background color changes to a different color. C. Implement an action for the button in SmilingFacePanel 1. Import the package java.awt.event.* 2. We will create a listener for clicker named ClickerListener as an inner class in SmilingFacePanel. The action will be to cause the background to change to black when clicker is pressed. It should not do anything else. We will do this as follows: Add instance variables for red, green, and blue to be used in the background color. These are integers: private int red, green, blue; Add the following code to your SmilingFacePanel class.

private class ClickerListener implements ActionListener public void actionperformed (ActionEvent event) red = 0; green = 0; blue = 0; setbackground (new Color(red, green, blue)); The clicker button should be controlled by the ClickerListener we specify this in the constructor, as follows: clicker.addactionlistener(new ClickerListener()); 3. Now we will add another button that, rather than causing the background to turn black with a single click, it will increase the amount of red in the background, gradually. a. Follow the steps you used to implement the clicker button to add another JButton called redclicker with the text "More red". b. In the constructor, assign the values: red=0; green=0; blue=0; c. Still in the constructor, create a new color for the background, using red, green, blue: setbackground (new Color(red, green, blue)); // this is the color black when red=green=blue=0 d. Add a listener for redclicker named RedListener (copy/paste the code for ClickerListener and adapt it). In the actionperformed() method of this listener: red +=20; setbackground (new Color(red, green, blue)); What happens when you keep clicking the More red button repeatedly? (Fix the actionperformed() method to prevent this from happening.) 4. Once you get the redclicker to work, repeat the above steps to create buttons to increment the amount of blue and green.

E. Make the color change by itself! Using the Timer class, you can cause the color change in the background of SmilingFacePanel to happen automatically, at specified intervals. Follow these steps: 1. Import the class Timer from the package javax.swing, i.e., add the import statement: import javax.swing.timer; 2. Add an instance variable for the Timer let s call it ticker private Timer ticker; 3. Add a constant for the delay: private final int DELAY = 100; // (milliseconds) 4. In the SmilingFacePanel constructor, instantiate the Timer object and start it up: ticker = new Timer(DELAY, new TickerListener()); ticker.start(); 5. Implement TickerListener as an inner class. For starters, have it function exactly like RedListener. Since the delay is set to 100 milliseconds, it will be as if you click the button 10 times every second. Observe it change color! Experiment with different functionality. For example, you might make the image move, by changing BASEX or BASEY in TickerListener.(you will first need to change BASEX to a regular variable by removing the final from its declaration). See ReboundPanel.java for a more interesting example of movement. F. Add a button to make it stop Add another JButton object with label stop to make the ticker (the timer) stop. The Listener for this button should simply invoke the stop() method for the Timer object, i.e., ticker.stop(); G. Wait! Don t stop Experiment with adding more buttons. You can add another button to restart the animation. Or to change an object in some other way There are so many possibilities, enjoy!

Lab 15 Comments Name: Comments on this lab, please: What was the most valuable thing you learned in this lab? What did you like best about this lab? Was there any particular problem? Do you have any suggestions for improving this lab as an effective learning experience?