Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1/18H212Mouse and Timer Events H212 Introduction to Software Systems Honors Lecture #16: Mouse and Timer Events October 26, 2015."— Presentation transcript:

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

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


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

Similar presentations


Ads by Google