Download presentation
Presentation is loading. Please wait.
1
CS12420 - Lecture 04 Mice Lynda Thomas ltt@aber.ac.uk
2
In This Lecture Mouse interaction and its Listeners Mouse use and motion AWT :MouseListener comp.addMouseListener(MouseListener ml); AWT :MouseMotionListener comp.addMouseMotionListener(MouseMotionListener mml);
3
MouseListener void mouseClicked(MouseEvent e) void mouseEntered(MouseEvent e) void mouseExited(MouseEvent e) void mousePressed(MouseEvent e) void mouseReleased(MouseEvent e) Called by runtime system
4
MouseMotionListener void mouseMoved(MouseEvent e) void mouseDragged(MouseEvent e) Called by runtime system
5
MouseEvent int getX() int getY() int getClickCount() boolean SwingUtilities.isLeftMouseButton(MouseEvent e) boolean SwingUtilities.isMiddleMouseButton(MouseEvent e) boolean SwingUtilities.isRightMouseButton(MouseEvent e) Also MouseEvent.getButton()
6
Mouse Project Specification Title position: x = 137 y = 53 number of clicks: 27 mouse is in component: yes Panel 300x300 Mouse pointer
7
So first the easy stuff The driver: Mouse-example/MouseEventDriver.java Brings up the Frame: Mouse-example/MouseEventFrame.java This contains two panels – the event panel and the status panel See handout 7
8
public class MouseEventFrame extends SimpleFrame { private MouseEventPanel mePanel; private StatusPanel stPanel; public MouseEventFrame() { this.setTitle("Mouse Click App"); mePanel = new MouseEventPanel(); stPanel = new StatusPanel(); add(mePanel,BorderLayout.CENTER); add(stPanel,BorderLayout.SOUTH); pack(); } private MyMousePositionListener mPL; private MyMouseListener mL; mPL = new MyMousePositionListener(stPanel); mePanel.addMouseMotionListener(mPL); mL = new MyMouseListener(stPanel); mePanel.addMouseListener(mL); Notice the pattern of create, add, make something listen just like with buttons
10
StatusPanel and MouseEventPanel are the two panels that are in the Frame
11
MyMouseListener and MyMousePositionListener are the two listeners (note you could have one listener that implemented both interfaces)
12
import java.awt.event.*; public class MyMousePositionListener implements MouseMotionListener { private StatusPanel statusPane;//link to status panel public MyMousePositionListener(StatusPanel sp) { statusPane = sp;//set up the link } public void mouseMoved(MouseEvent e) {//update the status panel statusPane.setCoordinates(e.getX(),e.getY()); } …. Notice how the Listener needs a link back to the status panel so it can update that panel – don’t be afraid to do that. For instance, suppose you wanted to draw on the event panel what would you do???
13
General Info on Listeners addXXXXListener(XXXXListener listener) Implementing listeners – several ways As a class on their own As internal classes As part of the application class As anonymous listeners
14
Own Class See for instance CounterListener.java in Lecture 01 (handout 3)
15
Internal Class The CounterPanel class could have an internal Listener class: public class CounterPanel extends JPanel { //... Including the call to new InternalCounterListener private class InternalCounterListener implements ActionListener { public void actionPerformed(ActionEvent e) { //code for this method taken from CounterListener.java }
16
Or Listening could be something another Application Class does public class CounterPanel extends JPanel implements ActionListener { //no need to have an instance of a Listener as the class is a Listener public CounterPanel(){ // code here... upButton.addActionListener(this); downButton.addActionListener(this); } public void actionPerformed(ActionEvent e) { //code for this method taken from CounterListener }
17
Anonymous (on-the-fly) Listeners comp.addXXXXListener( //whole def of XXXXListener here ); upButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { increment(); } ); //similar code for downButton
18
Comparing Implementations As part of the application view class Easy : less interface experience required Confusing, less structured, limits listener’s range As internal classes When manipulating objects from one view class As a class on their own Elegant and efficient (But visibility of things to act on..?) When manipulating objects from many view classes As anonymous listeners No need to check which component triggered listener Can be difficult to understand
19
In This Lecture Mouse interaction Listeners
20
PS. What would it take to turn this into a drawing program JUST DO IT – SEE NEXT TOPIC See the play-drawing folder This represents a naïve view, but notice problems: –If I call super.paintComponent() then the screen gets cleaned –If I don’t: ‘artifacts’ Note what happens when you cover the window and uncover it Next we will solve these problems
21
In The Next Lecture We solve the problems in play-drawing and produce some Interactive graphics
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.