Detailed design of mouse event in Java graphical programming _java

Source: Internet
Author: User
Tags gety pack

The event source for a mouse event is often associated with a container, and mouse events occur when the mouse enters the container, leaves the container, or clicks the mouse in the container and drags the mouse. The Java language provides two interfaces for handling mouse events: the Mouselistener,mousemotionlistener interface.
MouseListener Interface

MouseListener interface can handle 5 kinds of mouse events: Press the mouse, release the mouse, click Mouse, mouse to enter, mouse exit. The corresponding methods are:
(1) GetX (): The x-coordinate of the mouse
(2) GetY (): The y-coordinate of the mouse
(3) getmodifiers (): Get the left or right mouse button.
(4) GetClickCount (): The number of times the mouse is clicked.
(5) GetSource (): Gets the event source where the mouse occurs.
(6) Addmouselistener (monitor): Add Monitor.
(7) Removemouselistener (monitor): Remove the monitor.

The methods to implement the MouseListener interface are:
(1) mousepressed (MouseEvent e);
(2) mousereleased (MouseEvent e);
(3) mouseentered (MouseEvent e);
(4) mouseexited (MouseEvent e);
(5) mouseclicked (MouseEvent e);

The example applet sets a text area for recording a series of mouse events. When the mouse enters the small application window, the text area displays "mouse in"; When the mouse leaves the window, the text area displays "mouse away"; When the mouse is pressed, the text area displays "Mouse down", when the mouse is double-clicked, the text area displays "mouse double-click", and displays the mouse coordinates. The program also shows a red circle, and when you click the mouse, the radius of the circle will continue to grow larger.

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{text;
  Mypanel Panel;
  int x,y,r = 10;
  int mouseflg=0;
  Static String mousestates[]={"mouse button Down", "Mouse release", "Mouse in", "Mouse Away", "Double-click the Mouse"};
    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]+ ", bitis: "+x+", "+y+");
  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 ();
      The 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 event signaled
  Procedure ");
 }
}

Mouse events can occur on any component: The mouse enters, the mouse exits, presses the mouse and so on. For example, to add a button to the above program and add a mouse monitor to the button object, modify the Init () method in the above program to the form that all mouse events on the button are indicated.

JButton button;
public void init () {button
  = new JButton ("mouse event can also occur");
  R = Ten;
  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 pressed or clicked, the left or right mouse button can be judged by the constant button1_mask and Button3_mask in the InputEvent class. For example, the following expression determines whether the right mouse button is pressed or clicked:

  E.getmodifiers () ==inputevent. Button3_mask


Mousemotionlistener Interface

The Mousemotionlistener interface handles two events, dragging the mouse and moving the mouse.

The method for registering a monitor is:
Addmousemotionlistener (monitor)
There are two interface methods to implement:
(1) mousedragged (MouseEvent e)
(2) mousemoved (MouseEvent e)

Example an application in which the scroll bar changes with the display window. The window has a box, with the mouse to drag the box, or with the mouse to click on the window, the box changes the display position, the corresponding horizontal and vertical scroll bar slider will also change their position in the scroll bar. Conversely, if you move the slider bar, the display position of the square in the window will 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); The public void mouseentered (MouseEvent e) {} is public void mouseexited (MouseEvent e) {} public void mouseclicked (Mouseeve NT 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) (xscroll
    Bar.getvalue ()/100.0) *getsize (). width);
    else if (E.getsource () ==yscrollbar) y = (int) (float) (Yscrollbar.getvalue ()/100.0) *getsize (). height);
  Repaint ();
  The public class example6_9{public static void main () {Mywindow Mywindow = new Mywindow ("scroll bar schematic");
 }
}

In the example above, if you only want to change the display position of the content by sliding the slider, you can simply use the scrolling panel JScrollPane. If so, the creation and control of the scroll bars can be dispensed with directly by the JScrollPane internal implementations. See the following modified Mywindow definition:

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 controlled by the SetCursor () method to set the mouse pointer shape. For example, code setcursor (Cursor.getpredefinedcursor Cursor. Wait_cursor)).

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.