1/18H212Mouse and Timer Events H212 Introduction to Software Systems Honors Lecture #16: Mouse and Timer Events October 26, 2015.

Slides:



Advertisements
Similar presentations
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.
Advertisements

ITEC220 GUI Lecture – Part 2 References  Java Software Solutions,” by Lewis & Loftus  Chapter 7  Chapter 8  Chapter 9  Java Foundations-Introduction.
Computer Science 209 Graphics and GUIs. Working with Color The class java.awt.Color includes constants for typical color values and also supports the.
Event-Driven Programming Thus far, our programs have been executed one statement after the other However, many programs depend on user actions to dictate.
Event Handling Events and Listeners Timers and Animation.
Chapter Day 10. © 2007 Pearson Addison-Wesley. All rights reserved4-2 Agenda Day 10 Questions from last Class?? Problem set 2 posted  10 programs from.
Intermediate Java1 An example that uses inner classes Week Four Continued.
Graphical User Interfaces
Chapter Day 9. © 2007 Pearson Addison-Wesley. All rights reserved4-2 Agenda Day 8 Questions from last Class?? Problem set 2 posted  10 programs from.
Chapter Day 19. © 2007 Pearson Addison-Wesley. All rights reserved7-2 Agenda Day 19 Problem set 3 Corrected  1 A, 2 B’s and 1 D Problem set 4 Posted.
ELC 312 Day 16. © 2004 Pearson Addison-Wesley. All rights reserved Agenda Questions? Capstone Proposals Due Problem set 3 Corrected  Poor performance.
Animation To animate our programs, that is, to have real time interactions where objects move on the screen, we want to call repaint( ) every few milliseconds.
Lesson 35: Review of the Java GUI. The JFrame, Container and JButton.
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
Mouse Events. Handling Mouse Events Java provides two listener interfaces to handle mouse events: MouseListener;  MouseListener;  MouseMotionListener.
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Event Driven Programming, The.
Chapter 12 Event Handling. Chapter Goals To understand the Java event model To install action and mouse event listeners To accept input from buttons,
CS 11 java track: lecture 4 This week: arrays interfaces listener classes inner classes GUI callbacks.
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.
CS 112 Department of Computer Science George Mason University CS 112 Department of Computer Science George Mason University Writing Graphic Applications.
CSE 501N Fall ‘09 20: Event Handling and Inner Classes 17 November 2009 Nick Leidenfrost.
Previous programs used a JLabel for OUTPUT. Another Swing component that can be used for both user input and output is the JTextfield. Suppose we want.
Lesson 34: Layering Images with Java GUI. The FlowLayout RECAP.
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
Fall 2006Adapded from Java Concepts Companion Slides1 Event Handling Advanced Programming ICOM 4015 Lecture 13 Reading: Java Concepts Chapter 12.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
1 Graphical User Interfaces AWT and Swing packages Frames and Panels Components Nested Panels Images Reading for this Lecture: L&L, 3.9 – 3.11.
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
Copyright © 2002, Systems and Computer Engineering, Carleton University b-Gui2.ppt * Object-Oriented Software Development Part 18-b Building.
COMP 321 Week 2. Outline Event-Driven Programming Events, Event Sources, Event Listeners Button and Timer Events Mouse Events, Adapters.
For (int i = 1; i
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
Creating a GUI with JFC/Swing. What are the JFC and Swing? JFC –Java Foundation Classes –a group of features to help people build graphical user interfaces.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
1 Lecture 25 Listening to buttons and mice Quotes by Tony Hoare There are two ways of constructing a software design: (1) make it so simple that there.
CHAPTER 10 EVENT HANDLING. CHAPTER GOALS To understand the Java event model To install mouse and action listeners To accept mouse and text input To display.
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
Mouse Listeners Moving the mouse will also generate events like the Timer –To have your program respond, you must implement either or both of MouseListener.
Review_6 AWT, Swing, ActionListener, and Graphics.
Graphical User Interfaces A Graphical User Interface (GUI) in Java is created with at least three kinds of objects: –components, events, and listeners.
CS 151: Object-Oriented Design October 1 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Nested Classes and Event Handling Chapter 10. Overview We explain how to write Java code that responds to events. The Timer class can be used to respond.
1 Event Handling – Lecture 4 Prepared by: Ahmad Ramin Rahimee Assistant Professor ICTI.
What Is an Event? Events – Objects that describe what happened Event sources – The generator of an event Event handlers – A method that receives an event.
Polygons and Polylines © 2004 Pearson Addison-Wesley. All rights reserved 7-1 Arrays can be helpful in graphics processing For example, they can be used.
Mouse Events GUI. Types of Events  Below, are some of the many kinds of events, swing components generate. Act causing EventListener Type User clicks.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Lesson 28: More on the GUI button, frame and actions.
Lesson 33: Layout management and drawing – Java GUI.
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.
GUI Tutorial Day 4. More GUI action  adding a Mouse Listener  SimpleDots  Simple mouse listener  Draw an oval where the mouse is clicked  Box example.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
TENTH LECTURE Event and listener. Events and Listeners An event can be defined as a type of signal to the program that something has happened. The event.
1 A Quick Java Swing Tutorial. 2 Introduction Swing – A set of GUI classes –Part of the Java's standard library –Much better than the previous library:
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
GUI Programming in Java Hao Jiang Boston College April, 2009.
Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
Events and Event Handling
Computer Science 209 Graphics and GUIs.
GUI Programming III: Events
Event-driven programming for GUI
Chapter 12 Event Handling
Events, Event Handlers, and Threads
Constructors, GUI’s(Using Swing) and ActionListner
ITEC220 GUI Lecture – Part 2 References
Presentation transcript:

1/18H212Mouse and Timer Events H212 Introduction to Software Systems Honors Lecture #16: Mouse and Timer Events October 26, 2015

2/18H212Mouse and Timer Events GUI: mouse events Java divides events that are generated when using a mouse into two categories: mouse events mouse motion events When you click the mouse button over a Java GUI component, three events are generated: one when the mouse button is pushed down (mouse pressed) and two when it is let up (mouse released and mouse clicked). A mouse click is defined as pressing and releasing the mouse button in the same location. If you press the mouse button down, move the mouse, and then release the mouse button, a mouse clicked event is not generated.

3/18H212Mouse and Timer Events GUI: mouse events Java divides event that are generated when using a mouse into two categories: mouse events mouse motion events mouse pressedthe mouse button is pressed down mouse releasedthe mouse button is released mouse clickedthe mouse button is pressed down and released without moving the mouse in between mouse enteredthe mouse pointer is moved onto (over) a component mouse exitedthe mouse pointer is moved off of a component mouse movedthe mouse is moved mouse draggedthe mouse is moved while the mouse button is pressed down

4/18H212Mouse and Timer Events GUI: mouse events Listeners for mouse events are created using the MouseListener and MouseMotionListener interfaces. A MouseEvent object is passed to the appropriate method when a mouse event occurs. For a given program, we may only care about one or two mouse events. To satisfy the implementation of a listener interface, empty methods must be provided for unused events. Example: The Dots program responds to one mouse event. It draws a green dot at the location of the mouse pointer whenever the mouse button is pressed. Unused mouse methods have to be implemented, but the body could be empty.

5/18H212Mouse and Timer Events //******************************************************************** // DotsPanel.java // // Represents the primary panel for the Dots program. //******************************************************************** import java.util.ArrayList; import javax.swing.JPanel; import java.awt.*; import java.awt.event.*; public class DotsPanel extends JPanel { private final int SIZE = 6; // radius of each dot private ArrayList pointList; Object Component Container JComponent JPanel Point is a class in package java.awt

6/18H212Mouse and Timer Events // // Constructor: Sets up this panel to listen for mouse events. // public DotsPanel() { pointList = new ArrayList (); addMouseListener(new DotsListener()); setBackground(Color.black); setPreferredSize(new Dimension(300, 200)); } // // Draws all of the dots stored in the list. // public void paintComponent(Graphics page) { super.paintComponent(page); page.setColor(Color.green); for (Point spot : pointList) page.fillOval(spot.x-SIZE, spot.y-SIZE, SIZE*2, SIZE*2); page.drawString("Count: " + pointList.size(), 5, 15); } Object Component Container JComponent JPanel addMouseListener setPreferredSize setBackground paintComponent

7/18H212Mouse and Timer Events //***************************************************************** // Listener for mouse events. //***************************************************************** private class DotsListener implements MouseListener { // // Adds the current point to the list of points and redraws // the panel whenever the mouse button is pressed. // public void mousePressed(MouseEvent event) { pointList.add(event.getPoint()); repaint(); } // // Provide empty definitions for unused event methods. // public void mouseClicked(MouseEvent event) {} public void mouseReleased(MouseEvent event) {} public void mouseEntered(MouseEvent event) {} public void mouseExited(MouseEvent event) {} } } Method inherited from Jcomponent. It calls paintComponent.

8/18H212Mouse and Timer Events //******************************************************************** // Dots.java // Demonstrates mouse events. //******************************************************************** import javax.swing.JFrame; public class Dots { // // Creates and displays the application frame. // public static void main(String[] args) { JFrame frame = new JFrame("Dots"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new DotsPanel()); frame.pack(); frame.setVisible(true); } }

9/18H212Mouse and Timer Events The Timer Class The Timer class of the javax.swing package is a GUI component, but it has no visual representation. A Timer object generates an action event at specified time intervals. Timers can be used to manage any events that are based on a timed interval, such as an animation. To create the illusion of movement, we use a timer to change the scene after an appropriate delay. The start and stop methods of the Timer class start and stop the timer The delay can be set using the Timer constructor or using the setDelay method.

10/18H212Mouse and Timer Events Timer Class Methods Timer (int delay, ActionListener listener) Constructor: Creates a timer that generates an action event at regular intervals, specified by the delay. The event will be handled by the specified listener. void addActionListener (ActionListener listener) Adds an action listener to the timer. void setDelay (int delay) Sets the delay of the timer. void start () Starts the timer, causing it to generate action events. void stop () Stops the timer, causing it to stop generating action events.

11/18H212Mouse and Timer Events Use of Timer When you use a timer, you specify the frequency of the events and an object of a class that implements the ActionListener interface. Place whatever action you want to occur inside the actionPerformed method. Finally, start the timer. class MyListener implements ActionListener { public void actionPerformed(ActionEvent event) { // Action that is executed at each timer event } } MyListener listener = new MyListener(); Timer t = new Timer(interval, listener); t.start(); Then the timer calls the actionPerformed method of the listener object every interval milliseconds.

12/18H212Mouse and Timer Events Game of Tetris To play a game such as Tetris, you need to do things in real time, which is adequate for human being. We will start building an application which will eventually end up as a Tetris game. Initially, we will just implement first versions of: World An interface, initially empty, but will be used later. Any game should implement this interface. BigBang A class that implements the actions. Tetris Instantiates BigBang and starts the application.

13/18H212Mouse and Timer Events BigBang import javax.swing.*; import java.awt.event.*; public class BigBang implements ActionListener { private Timer timer; private int steps; public BigBang(int delay, World world) { this.timer = new Timer(delay, this); } public void start() { this.timer.start(); } public void actionPerformed(ActionEvent e) { this.steps += 1; System.out.println("Time goes by: " + this.steps); } } Listener for Timer events Set a Timer Start the Timer

14/18H212Mouse and Timer Events World, Tetris interface World { } public class Tetris implements World { // see Tetris implementing World... public static void main(String[] args) { BigBang b = new BigBang(1000, new Tetris()); b.start(); } } Initially leave the interface empty Generate Timer event every 1 second Start Timer

15/18H212Mouse and Timer Events JComponent We will use JComponent, from the Swing toolkit. JComponent class represents a blank component. Since we don’t want to add a blank component, we have to modify the Jcomponent class and specify how the component should be painted. The solution is to declare a new class that extends the JComponent class. The paintComponent method is called automatically: When the component is shown for the first time, When the window is resized, When it is shown again after it was hidden. Example of using JComponent.

16/18H212Mouse and Timer Events import java.awt.*; import javax.swing.*; class One extends JComponent { int count; public void paintComponent(Graphics g) { System.out.println("Called " + ++count + " times."); g.drawOval(100, 100, 60, 60); } } import javax.swing.*; public class Two extends JFrame { Two() { this.setVisible(true); this.setSize(400, 400); this.add(new One()); } public static void main(String[] args) { Two t = new Two(); } } When the components is shown for the first time, the paintComponent method is called automatically. Increment count each time the paintComponent is called.

17/18H212Mouse and Timer Events New version of BigBang import javax.swing.*; import java.awt.event.*; import java.awt.*; public class BigBang extends JComponent implements ActionListener { private Timer timer; public BigBang(int delay, World world) { this.timer = new Timer(delay, this); } public void start() { this.timer.start(); } int steps; public void actionPerformed(ActionEvent e) { this.steps += 1; // System.out.println("Time goes by: " + this.steps); this.repaint(); } public void paintComponent(Graphics g) { g.drawString("Time goes by: " + this.steps, 100, 200); } } BigBang extends a class and implements an interface! Listener is an object of this class. Method inherited from Jcomponent. It calls paintComponent.

18/18H212Mouse and Timer Events New version of Tetris class import javax.swing.*; public class Tetris implements World { // see Tetris implementing World... public static void main(String[] args) { JFrame f = new JFrame(); f.setVisible(true); f.setSize(400, 400); BigBang b = new BigBang(1000, new Tetris() ); b.start(); f.getContentPane().add( b ); } } Interface World is the same.