Java - AWT MouseMotionListener Interface


Java MouseMotionListener je obaviješten kad god pomjerite ili prevučete miša. Prijavljen je protiv MouseEventa. Interfejs MouseMotionListener se nalazi u paketu java.awt.event. Ima dvije metode. Potpis 2 metode za MouseMotionListener interfejsu je prikazan u nastavku:

public abstract void mouseDragged(MouseEvent e);  
public abstract void mouseMoved(MouseEvent e);  


Primjer Java MouseMotionListener-a

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

public class Main extends Frame implements MouseMotionListener{  
    Main(){  
        addMouseMotionListener(this);  
          
        setSize(300,300);  
        setLayout(null);  
        setVisible(true);  
    }  
public void mouseDragged(MouseEvent e) {  
    Graphics g=getGraphics();  
    g.setColor(Color.BLUE);  
    g.fillOval(e.getX(),e.getY(),20,20);  
}  
public void mouseMoved(MouseEvent e) {}  
  
    public static void main(String[] args) {  
        new Main();  
    }  
}

Prikaz koda iz gornjeg primjera izgledaće ovako:



Primjer 2 Java MouseMotionListener-a

import java.awt.*;  
import java.awt.event.MouseEvent;  
import java.awt.event.MouseMotionListener;  

public class Main extends Frame implements MouseMotionListener{  
    Label l;  
    Color c=Color.BLUE;  
    
    Main(){  
    l=new Label();  
    l.setBounds(20,40,100,20);  
    add(l);  
      
    addMouseMotionListener(this);  
      
    setSize(400,400);  
    setLayout(null);  
    setVisible(true);  
}  
public void mouseDragged(MouseEvent e) {  
    l.setText("X="+e.getX()+", Y="+e.getY());  
    Graphics g=getGraphics();  
    g.setColor(Color.RED);  
    g.fillOval(e.getX(),e.getY(),20,20);  
}  
public void mouseMoved(MouseEvent e) {  
    l.setText("X="+e.getX()+", Y="+e.getY());  
}  
    public static void main(String[] args) {  
        new Main();  
    }  
}

Prikaz koda iz gornjeg primjera izgledaće ovako: