Detail mouse event design in Java graphical programming

  • 2020-04-01 04:15:33
  • OfStack

The event source of mouse events is often container-related, and mouse events occur when the mouse enters or leaves the container, or when the mouse is clicked or dragged in the container. The Java language provides two interfaces for handling mouse events: MouseListener and MouseMotionListener.
The MouseListener interface

The MouseListener interface handles five mouse events: press the mouse down, release the mouse, click the mouse, enter the mouse, and exit the mouse. The corresponding methods are:
(1) getX() : the X coordinate of the mouse
(2) getY() : Y coordinate of the mouse
(3) getModifiers() : gets the left or right mouse button.
(4) getClickCount() : the number of times the mouse is clicked.
(5) getSource() : gets the event source of the mouse.
(6) addMouseListener(monitor) : add a monitor.
(7) removeMouseListener(monitor) : remove the monitor.

The methods for the MouseListener interface to implement are:
(1) mousePressed (MouseEvent e);
(2) the mouseReleased (MouseEvent e);
(3) mouseEntered MouseEvent (e);
(4) mouseExited MouseEvent (e);
(5) mouseClicked MouseEvent (e);

The applet sets up a text field to record a series of mouse events. When the mouse enters the small application window, the text area shows "mouse in"; When the mouse leaves the window, the text area shows "mouse away"; When the mouse is pressed, the text area shows "mouse pressed"; when the mouse is double-clicked, the text area shows "double-click"; And displays the coordinates of the mouse. The program also shows a red circle whose radius keeps growing when the mouse is clicked.


import java.applet.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class MyPanel extends JPanel{
  public void print(int r){
    Graphics g = getGraphics();
    g.clearRect(0,0,this.getWidth(),this.getHeight());
    g.setColor(Color.red);
    g.fillOval(10,10,r,r);
  }
}
class MyWindow extends JFrame implements MouseListener{
  JTextArea text;
  MyPanel panel;
  int x,y,r =10;
  int mouseFlg=0;
  static String mouseStates[]={" Press the mouse key "," Release the mouse "," The mouse come in "," The mouse away "," Mouse click "};
  MyWindow(String s){
    super(s);
    Container con = this.getContentPane();
    con.setLayout(new GridLayout(2,1));
    this.setSize(200,300);
    this.setLocation(100,100);
    panel = new MyPanel();
    con.add(panel);
    text = new JTextArea(10,20);
    text.setBackground(Color.blue);
    con.add(text);
    addMouseListener(this);
    this.setVisible(true);
    this.pack();
  }
  public void paint(Graphics g){
    r = r+4;
    if(r>80){
      r=10;
    }
    text.append(mouseStates[mouseFlg]+" Yes, the location is: " +x+","+y+"n");
    panel.print(r);
  }
  public void mousePressed(MouseEvent e){
    x = e.getX();
    y = e.getY();
    mouseFlg = 0;
    repaint();
  }
  public void mouseRelease(MouseEvent e){
    x = e.getX();
    y = e.getY();
    mouseFlg = 1;
    repaint();
  }
  public void mouseEntered(MouseEvent e){
    x = e.getX();
    y = e.getY();
    mouseFlg = 2;
    repaint();
  }
  public void mouseExited(MouseEvent e){
    x = e.getX();
    y = e.getY();
    mouseFlg = 3;
    repaint();
  }
  public void mouseClicked(MouseEvent e){
    if(e.getClickCount()==2){
      x = e.getX();
      y = e.getY();
      mouseFlg = 4;
      repaint();
    }
    else{}
  }
}
public class Example6_8 extends Applet{
  public void init(){
    MyWindow myWnd = new MyWindow(" Mouse events indicate the program ");
  }
}

Mouse events can occur on any component: mouse entry, mouse exit, mouse down, and so on. For example, add a button to the program and add a mouse monitor to the button object, and modify the init() method in the program to indicate all mouse events on the button.


JButton button;
public void init(){
  button = new JButton( "Buttons can also have mouse events" );
  r = 10;
  text = new JTextArea(15,20);
  add(button);
  add(text);
  button.addMouseListener(this);
}

If the program wants to know further that the left or right mouse button is being pressed or clicked, the left or right mouse button can be determined by the constants BUTTON1_MASK and BUTTON3_MASK in the InputEvent class. For example, the following expression determines whether the right mouse button was pressed or clicked:


  e.getModifiers()==InputEvent. BUTTON3_MASK


MouseMotionListener interface

The MouseMotionListener interface handles both mouse dragging and mouse moving events.

The way to register the monitor is:
      AddMouseMotionListener (monitor)
There are two interface methods to implement:
(1) mouseDragged (MouseEvent e)
(2) mouseMoved (MouseEvent e)

An application in which the scrollbar changes synchronously with the display window. The window has a square, drag the square with the mouse, or click the window with the mouse, the square changes the display position, the corresponding horizontal and vertical scroll bar slider will also change their position in the scroll bar. Conversely, by moving the slider of the scroll bar, the position of the square in the window will also change.


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class MyWindow extends JFrame{
  public MyWindow(String s){
    super(s);
    Container con = this.getContentPane();
    con.setLayout(new BorderLayout());
    this.setLocation(100,100);
    JScrollBar xAxis = new JScrollBar(JScrollBar.HORIZONTAL,50,1,0,100);
    jScrollBar yAxis = new jScrollBar(JScrollBar.VERTICAL,50,1,0,100);
    MyListener listener = new MyListener(xAxis,yAxis,238,118);
    Jpanel scrolledCanvas = new JPanel();
    scrolledCanvas.setLayout(new BorderLayout());
    scrolledCanvas.add(listener,BorderLayout.CENTER);
    scrolledCanvas.add(xAix,BorderLayout.SOUTH);
    scrolledCanvas.add(yAix,BorderLayout.EAST);
    con.add(scrolledCanvas,BorderLayout.NORTH);
    this.setVisible(true);
    this.pack();
  }
  public Dimension getPreferredSize(){
    return new Dimension(500,300);
  }
}
class MyListener extends JComponent implements MouseListener, MouseMotionListener,AdjustmentListener{
  private int x,y;
  private JScrollBar xScrollBar;
  private JScrollBar yScrollBar;
  private void updateScrollBars(int x,int y){
    int d;
    d = (int)(((float)x/(float)getSize().width)*100.0);
    xScrollBar.setValue(d);
    d = (int)(((float)y/(float)getSize().height)*100.0);
    yScrollBar.setValue(d);
  }
  public MyListener(JScrollBar xaxis,JScrollBar yaxis,int x0,int y0){
    xScrollBar =xaxis;
    yScrollBar =yaxis;
    x = x0;
    y=y0;
    xScrollBar.addAdjustmentListener(this);
    yScrollBar.addAdjustmentListener(this);
    this.addMouseListener(this);
    this.addMouseMotionListener(this);
  }
  public void paint(Graphics g){
    g.setColor(getBackground());
    Dimension size = getSize();
    g.fillRect(0,0,size.width,size.height);
    g.setColor(Color.blue);
    g.fillRect(x,y,50,50);
  }
  public void mouseEntered(MouseEvent e){}
  public void mouseExited(MouseEvent e){}
  public void mouseClicked(MouseEvent e){}
  public void mouseRelease(MouseEvent e){}
  public void mouseMoved(MouseEvent e){}
  public void mousePressed(MouseEvent e){
    x = e.getX();
    y = e.getY();
    updateScrollBars(x,y);
    repaint();
  }
  public void mouseDragged(MouseEvent e){
    x = e.getX();
    y = e.getY();
    updateScrollBars(x,y);
    repaint();
  }
  public void adjustmentValueChanged(AdjustmentEvent e){
    if(e.getSource()==xScrollBar)
      x=(int)((float)(xScrollBar.getValue()/100.0)*getSize().width);
    else if(e.getSource()==yScrollBar)
      y = (int)((float)(yScrollBar.getValue()/100.0)*getSize().height);
    repaint();
  }
}
public class Example6_9{
  public static void main(){
    MyWindow myWindow = new MyWindow(" The scroll bar indicates the program ");
  }
}

In the example above, you could simply use the scroll panel JScrollPane if you wanted to change the display position of the content simply by sliding the slider. If so, the creation and control of the scrollbar can be eliminated and implemented directly inside the JScrollPane. See the following modified definition of MyWindow:


class MyWindow extends JFrame{
  public MyWindow(String s){
    super(s);
    Container con = this.getContentPane();
    con.setLayout(new BorderLayout());
    this.setLocaltion(100,100);
    MyListener listener = new MyListener();
    listener.setPreferredSize(new Dimension(700,700));
    JScrollPane scrolledCanvas = new JScrollPane(listener);
    this.add(scrolledCanvas,BorderLayout.CENTER);
    this.setVisible(true);
    this.pack();
  }
  public Dimension getPreferredSize(){
    return new Dimension(400,400);
  }
}

The mouse pointer shape can also be programmatically controlled, and the setCursor() method can set the mouse pointer shape. For example, the code setCursor (Cursor. GetPredefinedCursor (Cursor. WAIT_CURSOR)).


Related articles: