Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java
Expertise: Advanced
Nov 26, 2007

Implement mouseDragged and mouseMove Events

This tip shows you how to implement mouseDragged and mouseMove events for moving components into a frame:

import java.awt.*;
import java.awt.event.*;

class f extends Frame implements MouseMotionListener,MouseListener{

Checkbox CB=new Checkbox("Label",true);
Button BP=new Button("BUTTON_1");
Button B=new Button("BUTTON");
TextField TF=new TextField(10);
Panel P=new Panel();
List TA=new List();

boolean b1=false;
int c1=0;int c2=0;

public f(String titlu)
       {
       super(titlu);
       }

void init()
      {
      setLayout(null);       

      TA.addMouseMotionListener(this);
      TA.addMouseListener(this);
      B.addMouseMotionListener(this);
      B.addMouseListener(this);
      P.addMouseMotionListener(this);
      P.addMouseListener(this);
      CB.addMouseMotionListener(this);
      CB.addMouseListener(this);

      TA.setLocation(50,50);
      TA.setSize(100,100);TA.add("List");
      B.setLocation(100,150);B.setSize(80,40);
      CB.setLocation(120,370);CB.setSize(100,20);
      P.setLayout(new BorderLayout());
      P.setLocation(100,200);P.setSize(150,150);
      P.setBackground(Color.yellow);
      P.add(BP,BorderLayout.NORTH);
      P.add(TF,BorderLayout.SOUTH);
      add(TA);add(B);add(P);add(CB);

     setLocation(0,0);	
     setSize(400,400);

     setVisible(true);
     //show();
     }
 
//mouseDragged
public void mouseDragged(MouseEvent e)
             {            
             Component C=e.getComponent();

             if(b1==false){b1=true;Point p=new  
                            Point(e.getPoint());c1=p.x;c2=p.y;}
        
             Point z=new Point(e.getPoint());
             Point q=new Point(C.getLocation());  
             
             C.setBounds(q.x+(z.x-c1),q.y+(z.y-c2),C.getSize().  
                                     width,C.getSize().height);  
             } 

//mouseReleased
public void mouseReleased(MouseEvent e)
              {
              b1=false;
              }          

//mouseEntered
public void mouseEntered(MouseEvent e)
              {
              Cursor c1=new Cursor(Cursor.HAND_CURSOR);                                    
              setCursor(c1);
              }
 
//mouseExited
public void mouseExited(MouseEvent e)
              {
              Cursor c2=new Cursor(Cursor.DEFAULT_CURSOR);                                    
              setCursor(c2);
              }               

//mouseClicked
public void mouseClicked(MouseEvent e)
              {
              Component C=e.getComponent();
              System.out.println(C);
              }                         
 
//mouseMoved
 public void mouseMoved(MouseEvent e)
              {
              Component C=((Component)e.getSource());
              java.util.Random r=new java.util.Random();
              java.util.Random g=new java.util.Random();
              java.util.Random b=new java.util.Random();
              int cr=r.nextInt(255);int cg=g.nextInt(255);
              int cb=b.nextInt(255);
              Color col=new Color(cr,cg,cb);
              C.setBackground(col);
              }
 
//mousePressed
public void mousePressed(MouseEvent e){}                         
     }

public class mouse_events{
       public static void main(String[] args)
             {
             f t=new f("MOUSE EVENT");
             t.init();
             }
       }
Leonard Anghel
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap