User Tools

Site Tools


lang:judo:english:tutorial-en-015

[ Intro | Begin | if/else | Loops | Arrays | Graphics | Animation | Mouse | Game | Real | Methods | Class | Class 2 | Applet | MouseClick | Thread | Button ]


Java Mouse Click Method

In this tute I will attempt to do a rough explanation of the MouseListener Interface. Interfaces are very hard to explain: they are like a plugin or an adapter to allow completely different object to communicate.

Java MouseListener Method

These are its methods:

    public void mousePressed(MouseEvent e) {}
    public void mouseClicked(MouseEvent e) {}
    public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}
    public void mouseReleased(MouseEvent e) {}

Interface uses the keyword “implements” in the class declaration when it is being used. You must implemnet all methods of an interface. You can leave empty any unneeded methods.

E.g.

    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
 
    public class MouseTest extends Applet implements MouseListener {
        int x, y;
        String tracer;
 
        public void init() {
	    addMouseListener(this);
	    setBackground(Color.red);
        }
 
        public void mousePressed(MouseEvent e) {
            x = e.getX() - 10;
            y = e.getY() - 10;
            tracer = " x = " + x + " y = " + y;
 
            // repaint the applet
            repaint();
        }
 
	// dont use these so leave them empty
	public void mouseClicked(MouseEvent e) {}
	public void mouseEntered(MouseEvent e) {}
	public void mouseExited(MouseEvent e) {}
	public void mouseReleased(MouseEvent e) {}
 
	// paint method
	public void paint(Graphics g) {
	    g.drawString(tracer, 100, 100);
	    g.drawOval(x, y, 20, 20);
	}
    }
lang/judo/english/tutorial-en-015.txt · Last modified: 2007/02/08 09:52 by 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki