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

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

Graphical Applications

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

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

H212 Introduction to Software Systems Honors

AP CS Unit 11: Graphics and Events

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

Designing Classes Part 2

Java Foundations John Lewis Peter DePasquale Joe Chase Third Edition

Lab 4. D0010E Object-Oriented Programming and Design. Today s lecture. GUI programming in

Chapter 4 Writing Classes

Programming Languages and Techniques (CIS120)

Graphical User Interfaces

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

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

User interfaces and Swing

CSC 1051 Data Structures and Algorithms I

CSC 1051 Data Structures and Algorithms I

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

MIT AITI Swing Event Model Lecture 17

GUI (Graphic User Interface) Programming. Part 2 (Chapter 8) Chapter Goals. Events, Event Sources, and Event Listeners. Listeners

CSIS 10A Assignment 7 SOLUTIONS

Graphical User Interfaces 2

Homework 6 part 2: Turtle Etch-A-Sketch (40pts)

Graphical User Interfaces 2

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!

Graphic User Interfaces. - GUI concepts - Swing - AWT

Introduction to Graphical User Interfaces (GUIs) Lecture 10 CS2110 Fall 2008

Graphical User Interfaces

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

FirstSwingFrame.java Page 1 of 1

CSC 1051 Data Structures and Algorithms I

1.00/1.001 Introduction to Computers and Engineering Problem Solving Spring Quiz 2

APPENDIX. public void cekroot() { System.out.println("nilai root : "+root.data); }

OOP Assignment V. For example, the scrolling text (moving banner) problem without a thread looks like:

CSC 1051 Villanova University. CSC 1051 Data Structures and Algorithms I. Course website:

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

H212 Introduction to Software Systems Honors

COMP Assignment #10 (Due: Monday, March 11:30pm)

Programming Language Concepts: Lecture 8

CSE 143. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

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

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

G51PRG: Introduction to Programming Second semester Applets and graphics

SampleApp.java. Page 1

Arrays, Part 3: Multidimensional arrays

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

Chapter 9 Inheritance

Introduction to Arrays

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

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

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

Graphical User Interfaces 2

Class 16: The Swing Event Model

Window Interfaces Using Swing Objects

Client-side GUI. A simple Swing-gui for searching for proudcts

Adding Buttons to StyleOptions.java

Graphical User Interfaces. Comp 152

JRadioButton account_type_radio_button2 = new JRadioButton("Current"); ButtonGroup account_type_button_group = new ButtonGroup();

Graphical User Interface

Graphical User Interface (GUI)

Chapter 11: Exceptions Lab Exercises

Midterm Test II Object Oriented Programming in Java Computer Science, University of Windsor Fall 2014 Time 2 hours. Answer all questions

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

Chapter 6: Graphical User Interfaces

GUI Forms and Events, Part II

Chapter 13 Lab Advanced GUI Applications

SINGLE EVENT HANDLING

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

Lecture 5: Java Graphics

Advanced Java Unit 6: Review of Graphics and Events

Part I: Learn Common Graphics Components

Introduction to the JAVA UI classes Advanced HCI IAT351

Calculator Class. /** * Create a new calculator and show it. */ public Calculator() { engine = new CalcEngine(); gui = new UserInterface(engine); }

Lecture 3: Java Graphics & Events

We are on the GUI fast track path

Chapter 5: Enhancing Classes

JAVA NOTES GRAPHICAL USER INTERFACES

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

CSE 331. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

Lecture 9. Lecture

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

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

Dr. Hikmat A. M. AbdelJaber

Using Several Components

CMP 326 Midterm Fall 2015

Chapter 6 Using Objects

Heavyweight with platform-specific widgets. AWT applications were limited to commonfunctionality that existed on all platforms.

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

TTTK Program Design and Problem Solving Tutorial 3 (GUI & Event Handlings)

Programmierpraktikum

CS108, Stanford Handout #22. Thread 3 GUI

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 :) )

RAIK 183H Examination 2 Solution. November 10, 2014

2IS45 Programming

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

COSC 123 Computer Creativity. Graphics and Events. Dr. Ramon Lawrence University of British Columbia Okanagan

Multiple Choice Questions: Identify the choice that best completes the statement or answers the question. (15 marks)

Window Interfaces Using Swing Objects

Transcription:

Events and Listeners CSC 1051 Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/ Some slides in this presentation are adapted from the slides accompanying Java Software Solutions by Lewis & Loftus

Events and Listeners Event Component Listener A component object generates an event A corresponding listener object is designed to respond to the event When the event occurs, the component calls the appropriate method of the listener, passing an object that describes the event

Buttons A push button is defined by the JButton class It generates an action event The PushCounter example displays a push button that increments a counter each time it is pushed See PushCounter.java See PushCounterPanel.java

//******************************************************************** // PushCounter.java Authors: Lewis/Loftus // // Demonstrates a graphical user interface and an event listener. //******************************************************************** import javax.swing.jframe; public class PushCounter // Creates the main program frame. public static void main(string[] args) JFrame frame = new JFrame("Push Counter"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.getcontentpane().add(new PushCounterPanel()); frame.pack(); frame.setvisible(true);

//******************************************************************** // 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; continue push = new JButton("Push Me!"); push.addactionlistener(new ButtonListener());

continue label = new JLabel("Pushes: " + count); add(push); add(label); setpreferredsize(new Dimension(300, 40)); setbackground(color.cyan); inner class //***************************************************************** // 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);

The Timer Class Another kind of event: the ticking of a clock Timer classs: part of javax.swing package Timer objects generate action events using a specified delay Use to create animations!

The Timer methods Constructor: Timer timer = new Timer(delay, listener ); start() stop() setdelay() Example: Rebound.java - ReboundPanel.java

//******************************************************************** // Rebound.java Author: Lewis/Loftus // // Demonstrates an animation and the use of the Timer class. //******************************************************************** import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Rebound // Displays the main frame of the program. public static void main(string[] args) JFrame frame = new JFrame("Rebound"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.getcontentpane().add(new ReboundPanel()); frame.pack(); frame.setvisible(true);

//******************************************************************** // ReboundPanel.java Author: Lewis/Loftus // // Represents the primary panel for the Rebound program. //******************************************************************** import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ReboundPanel extends JPanel private final int WIDTH = 300, HEIGHT = 100; private final int DELAY = 20, IMAGE_SIZE = 35; private ImageIcon image; private Timer timer; private int x, y, movex, movey; continue

continue // Sets up the panel, including the timer for the animation. public ReboundPanel() timer = new Timer(DELAY, new ReboundListener()); image = new ImageIcon("happyFace.gif"); x = 0; y = 40; movex = movey = 3; setpreferredsize(new Dimension(WIDTH, HEIGHT)); setbackground(color.black); timer.start(); // Draws the image in the current location. public void paintcomponent(graphics page) super.paintcomponent(page); image.painticon(this, page, x, y); continue

continue inner class //***************************************************************** // Represents the action listener for the timer. //***************************************************************** private class ReboundListener implements ActionListener //-------------------------------------------------------------- // Updates the position of the image and possibly the direction // of movement whenever the timer fires an action event. //-------------------------------------------------------------- public void actionperformed(actionevent event) x += movex; y += movey; if (x <= 0 x >= WIDTH-IMAGE_SIZE) movex = movex * -1; if (y <= 0 y >= HEIGHT-IMAGE_SIZE) movey = movey * -1; repaint();

Text Fields text field object allow user to input text enter key è generate action event gettext() - returns text entered Example: Fahrenheit.java FahrenheitPanel.java

//******************************************************************** // Fahrenheit.java Author: Lewis/Loftus // // Demonstrates the use of text fields. //******************************************************************** import javax.swing.jframe; public class Fahrenheit // Creates and displays the temperature converter GUI. public static void main(string[] args) JFrame frame = new JFrame("Fahrenheit"); frame.setdefaultcloseoperation(jframe.exit_on_close); FahrenheitPanel panel = new FahrenheitPanel(); frame.getcontentpane().add(panel); frame.pack(); frame.setvisible(true);

//******************************************************************** // FahrenheitPanel.java Author: Lewis/Loftus // // Demonstrates the use of text fields. //******************************************************************** import java.awt.*; import java.awt.event.*; import javax.swing.*; public class FahrenheitPanel extends JPanel private JLabel inputlabel, outputlabel, resultlabel; private JTextField fahrenheit; // Constructor: Sets up the main GUI components. public FahrenheitPanel() inputlabel = new JLabel("Enter Fahrenheit temperature:"); outputlabel = new JLabel("Temperature in Celsius: "); resultlabel = new JLabel("---"); continue fahrenheit = new JTextField(5); fahrenheit.addactionlistener(new TempListener());

continue add(inputlabel); add(fahrenheit); add(outputlabel); add(resultlabel); setpreferredsize(new Dimension(300, 75)); setbackground(color.yellow); inner class //***************************************************************** // Represents an action listener for the temperature input field. //***************************************************************** private class TempListener implements ActionListener //-------------------------------------------------------------- // Performs the conversion when the enter key is pressed in // the text field. //-------------------------------------------------------------- public void actionperformed(actionevent event) int fahrenheittemp, celsiustemp; String text = fahrenheit.gettext(); fahrenheittemp = Integer.parseInt(text); celsiustemp = (fahrenheittemp-32) * 5/9; resultlabel.settext(integer.tostring(celsiustemp));