Advertisement
vvccs

[X]E10_Mouse_event

Oct 26th, 2023 (edited)
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.70 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4.  
  5. public class mouse_event extends JPanel implements MouseMotionListener, MouseListener {
  6.     private String msg;
  7.     private int x, y;
  8.  
  9.     public mouse_event() {
  10.         addMouseMotionListener(this);
  11.         addMouseListener(this);
  12.     }
  13.  
  14.     public void mouseClicked(MouseEvent m) {
  15.         x = 0;
  16.         y = 40;
  17.         msg = "Mouse Clicked";
  18.         repaint();
  19.     }
  20.  
  21.     public void mouseEntered(MouseEvent m) {
  22.         x = 0;
  23.         y = 40;
  24.         msg = "Mouse Entered";
  25.         repaint();
  26.     }
  27.  
  28.     public void mouseDragged(MouseEvent m) {
  29.         x = m.getX();
  30.         y = m.getY();
  31.         msg = "#";
  32.         repaint();
  33.     }
  34.  
  35.     public void mouseExited(MouseEvent m) {
  36.         x = 0;
  37.         y = 20;
  38.         msg = "Mouse Exited";
  39.         repaint();
  40.     }
  41.  
  42.     public void mouseReleased(MouseEvent m) {
  43.         x = 0;
  44.         y = 20;
  45.         msg = "Mouse Released";
  46.         repaint();
  47.     }
  48.  
  49.     public void mousePressed(MouseEvent m) {
  50.         x = 0;
  51.         y = 40;
  52.         msg = "Mouse Pressed";
  53.         repaint();
  54.     }
  55.  
  56.     public void mouseMoved(MouseEvent m) {
  57.         x = 0;
  58.         y = 40;
  59.         msg = "Mouse Moved";
  60.         repaint();
  61.     }
  62.  
  63.     protected void paintComponent(Graphics g) {
  64.         super.paintComponent(g);
  65.         g.drawString(msg, x, y);
  66.     }
  67.  
  68.     public static void main(String[] args) {
  69.         JFrame frame = new JFrame("Mouse Event Example");
  70.         mouse_event panel = new mouse_event();
  71.         frame.add(panel);
  72.         frame.setSize(400, 400);
  73.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  74.         frame.setVisible(true);
  75.     }
  76. }
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement