{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

Info icon This preview shows pages 1–3. 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 definition: public class AJPanel extends JPanel implements MouseListener { 4 Handling MouseEvents Any class which implements MouseListener must include all five 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). returns the x,y position of the event relative to the source component (i.e. the JPanel). More about Point objects later.
Image of page 1

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

View Full Document Right Arrow Icon
CompSci101 Lecture 26 2 7 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Example 2 - Handling MouseEvents import java.awt.*;import java.swing.*; public class L25JPanel02 extends JPanel private String message; public L25JPanel02() { message = "No mouse event has occurred yet"; }
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern