L25 - Computer Science 101 SS C Lecture 25 Contents! 2!...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
CompSci101 Lecture 26 1 Computer Science 101 SS C Lecture 25 Contents Handling MouseEvents Handling KeyEvents Handling more than one type of event Coursebook: pages §16 2 MouseEvents A mouse event is generated when the mouse is pressed, released, clicked (pressed and released). A mouse event is also generated when the mouse cursor enters or leaves a component area. The statement: must be at the top of any class which uses MouseEvents. import java.awt.event.*; 3 Handling MouseEvents 1. Indicate that the JPanel is capable of handling MouseEvents : addMouseListener(this); public void mousePressed(MouseEvent e) {} public void mouseClicked(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} 2. In the JPanel constructor, register that the JPanel will respond to MouseEvents which occur inside the whole JPanel area: 3. Add ALL FIVE of the MouseListener methods to the JPanel de±nition: public class AJPanel extends JPanel implements MouseListener { 4 Handling MouseEvents Any class which implements MouseListener must include all ±ve MouseListener methods even if they are not used (i.e. even if these methods contain no code). Often we will want our JPanel to do something whenever the user presses the mouse. So we write the code which we want executed when the user presses the mouse inside the mousePressed() method. Notice that the parameter for the FIVE MouseListener methods is a MouseEvent object. When a mouse event occurs, the relevant method in the JPanel (the MouseListener) is invoked, and the MouseEvent object is passed to this method. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 5 Example 1 - Handling MouseEvents Whenever the user presses the mouse a new square is drawn. import java.awt.*;import java.swing.*; public class L25JPanel01 extends JPanel { public L25JPanel01() { } public void paintComponent(Graphics g) { super.paintComponent(g); g.fillRect( pos.x,pos.y, 20, 20); import java.awt.event .*; implements MouseListener public void mouseClicked(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} addMouseListener(this); public void mousePressed(MouseEvent e) { pos.move((int)(Math.random()*480), (int)(Math.random()*480)); } repaint(); } } private Point pos; 6 The MouseEvent object Three instance methods which can be used with a MouseEvent instance are: public int getX() public int getY() public Point getPoint() returns the horizontal x position of the event relative to the source component (i.e. the JPanel). returns the vertical y position of the event relative to the source component (i.e. the JPanel).
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 5

L25 - Computer Science 101 SS C Lecture 25 Contents! 2!...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online