Demonstrating the MouseListener and MouseMotionListener : Various Event Listener « Swing JFC « Java






Demonstrating the MouseListener and MouseMotionListener

Demonstrating the MouseListener and MouseMotionListener
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

public class MouseTest extends JFrame {
  int startX, startY, endX, endY;

  Color color = Color.BLACK;

  public MouseTest() {
    super();

    final JPopupMenu pop = new JPopupMenu();
    pop.add(new JMenuItem("Cut"));
    pop.add(new JMenuItem("Copy"));
    pop.add(new JMenuItem("Paste"));
    pop.addSeparator();
    pop.add(new JMenuItem("Select All"));
    pop.setInvoker(this);

    MouseListener popup = new MouseListener() {
      public void mouseClicked(MouseEvent e) {
      }

      public void mouseEntered(MouseEvent e) {
      }

      public void mouseExited(MouseEvent e) {
      }

      public void mousePressed(MouseEvent e) {
        if (e.isPopupTrigger()) {
          showPopup(e);
        }
      }

      public void mouseReleased(MouseEvent e) {
        if (e.isPopupTrigger()) {
          showPopup(e);
        }
      }

      private void showPopup(MouseEvent e) {
        pop.show(e.getComponent(), e.getX(), e.getY());
      }
    };
    addMouseListener(popup);

    MouseListener drawing1 = new MouseListener() {
      public void mouseClicked(MouseEvent e) {
      }

      public void mouseEntered(MouseEvent e) {
      }

      public void mouseExited(MouseEvent e) {
      }

      public void mousePressed(MouseEvent e) {
        color = Color.RED;
        startX = endX = e.getX();
        startY = endY = e.getY();
        repaint();
      }

      public void mouseReleased(MouseEvent e) {
        color = Color.BLACK;
        repaint();
      }
    };
    addMouseListener(drawing1);

    MouseMotionListener drawing2 = new MouseMotionListener() {
      public void mouseDragged(MouseEvent e) {
        endX = e.getX();
        endY = e.getY();
        repaint();
      }

      public void mouseMoved(MouseEvent e) {
      }
    };
    addMouseMotionListener(drawing2);

  }

  public void paint(Graphics g) {
    super.paint(g);
    g.setColor(color);
    g.drawLine(startX, startY, endX, endY);
  }

  public static void main(String args[]) {
    JFrame frame = new MouseTest();
    frame.setSize(300, 300);
    frame.show();
  }
}
           
       








Related examples in the same category

1.Demonstrating the WindowListener with a WindowAdapterDemonstrating the WindowListener with a WindowAdapter
2.Demonstrating the ActionListenerDemonstrating the ActionListener
3.Demonstrating the AdjustmentListenerDemonstrating the AdjustmentListener
4.Demonstrating the AncestorListener
5.Demonstrating the ComponentListenerDemonstrating the ComponentListener
6.Demonstrating the ContainerListenerDemonstrating the ContainerListener
7.Demonstrating the FocusListenerDemonstrating the FocusListener
8.Demonstrating the HyperlinkListenerDemonstrating the HyperlinkListener
9.Demonstrating the InternalFrameListenerDemonstrating the InternalFrameListener
10.Demonstrating the ItemListenerDemonstrating the ItemListener
11.Demonstrating the KeyListenerDemonstrating the KeyListener
12.Demonstrating the MenuListenerDemonstrating the MenuListener
13.Demonstrating the MouseWheelListenerDemonstrating the MouseWheelListener
14.Demonstrating the PopupMenuListenerDemonstrating the PopupMenuListener
15.Demonstrating the WindowListener
16.Responding to KeystrokesResponding to Keystrokes