View Single Post
Old Mar 9th, 2007, 12:53 AM   #3
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 843
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
You don't need to use threads (explicitly, anyway) to listen for mouse clicks. Notice that the applet will not respond to mouse events unless you call addMouseListener with the applet itself (this). In order to update the display, you need to call repaint() in the mousePressed method, which will call the paint method. (Note that mouseClicked, rather than mousePressed, is probably what you want; it waits until the user has released the mouse button to fire the event.)

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

public class MouseTest extends Applet implements MouseListener
{
    boolean hello;

    public void init() {
        hello = false;
        addMouseListener(this);
    }
    
    public void paint(Graphics g)
    {
        if(hello)
            g.drawString("Hello", 2, 10);
    }

    public void mouseClicked(MouseEvent evt)
    {
        hello = !hello;
        repaint();
    }
   
    public void mousePressed(MouseEvent evt) { }
    public void mouseEntered(MouseEvent evt) { }
    public void mouseExited(MouseEvent evt) { }
    public void mouseReleased(MouseEvent evt) { }

}

Last edited by titaniumdecoy; Mar 9th, 2007 at 1:05 AM.
titaniumdecoy is offline   Reply With Quote