Wrox Programmer Forums
|
J2EE General J2EE (Java 2 Enterprise Edition) discussions. Questions not specific to EE will be redirected elsewhere.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the J2EE section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 24th, 2005, 12:03 PM
Registered User
 
Join Date: Aug 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default MousePanel

// File: mousedeme/MousePanel.java
// Description: Panel that listens to mouse events and displays info
// about some of them.
// Author: Fred Swartz
// Date: 2005-02-03, 2000-11-29...2002-11-21

package mousedemo;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

// class MousePanel
class MousePanel extends JPanel implements MouseListener, MouseMotionListener {

   // instance variables
    private int m_lastClickedX = 0; // x coord of mouse click
    private int m_lastClickedY = 0; // y coord of mouse click
    private int m_lastMovedX = 0; // x coord of mouse move
    private int m_lastMovedY = 0; // y coord of mouse move


    //constructor
    public MousePanel() {
        this.setBackground(Color.white);
        this.setPreferredSize(new Dimension(200, 200));
        //--- Add the mouse listeners.
        this.addMouseListener(this); // listen to mouse events
        this.addMouseMotionListener(this); // listen to moves and drags
    }


    //method paintComponent
    public void paintComponent(Graphics g) {
        super.paintComponent(g); // paint background and borders
        g.drawString("Last click: x=" + m_lastClickedX
                      + ", y=" + m_lastClickedY , 10, 30);
        g.drawString("x=" + m_lastMovedX + ", y=" + m_lastMovedY
                      , m_lastMovedX, m_lastMovedY);
    }


    // listener mouseClicked
    public void mouseClicked(MouseEvent e) {
        m_lastClickedX = e.getX(); // Save the x coordinate of the click
        m_lastClickedY = e.getY(); // Save the y coordinate of the click
        this.repaint(); // Paint the panel with the new values.
    }


    // listener mouseMoved
    public void mouseMoved(MouseEvent e) {
        m_lastMovedX = e.getX();
        m_lastMovedY = e.getY();
        this.repaint();
    }


    // ignored
    // the other motion events must be here, but do nothing.
    public void mouseDragged (MouseEvent e) {} // ignore
    // these "slow" mouse events are ignored.
    public void mouseEntered (MouseEvent e) {} // ignore
    public void mouseExited (MouseEvent e) {} // ignore
    public void mousePressed (MouseEvent e) {} // ignore
    public void mouseReleased(MouseEvent e) {} // ignore
}







Similar Threads
Thread Thread Starter Forum Replies Last Post
MousePanel k_o_bliss J2EE 0 August 24th, 2005 12:55 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.