Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   Java applet mouse event. Some trouble... (http://www.programmingforums.org/showthread.php?t=12750)

glopal Mar 8th, 2007 10:52 PM

Java applet mouse event. Some trouble...
 
Here, it should be a very simple program. When the mouse is clicked, it toggles a boolean value named hello. if hello is true, it draws hello on the screen. But it doesnt work, this mouse stuff is tricky. I click on the screen and nothing happens. Does it has to do with the thread?

:

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

public class MouseTest extends Applet implements Runnable, MouseListener, MouseMotionListener
{
        Thread loop;
        BufferedImage backbuffer;
        Graphics g2d;
        boolean hello;

        public void init()
        {
                backbuffer = new BufferedImage(100,100, BufferedImage.TYPE_INT_RGB);
        g2d = backbuffer.createGraphics();
        }

        public void start()
        {
            loop = new Thread(this);
            loop.start();

            hello = false;
        }

        public void run()
        {
                Thread t = Thread.currentThread();
            while (t == loop)
            {

                    try
                    {
                    Thread.sleep(30);
                }
                catch(InterruptedException e) {
                    e.printStackTrace();
                }
                repaint();
            }
    }
    public void paint(Graphics g)
    {
                g.drawImage(backbuffer,0,0,this);
    }

    public void update(Graphics g)
    {
                if(hello)
                        g2d.drawString("Hello",2,10);
                paint(g);
        }

    public void mousePressed(MouseEvent evt)
    {
                hello = !hello;
    }
    public void mouseReleased(MouseEvent evt) {}
    public void mouseDragged(MouseEvent evt) {}


    public void mouseEntered(MouseEvent evt) { }
    public void mouseExited(MouseEvent evt) { }
    public void mouseClicked(MouseEvent evt) { }
    public void mouseMoved(MouseEvent evt) { }

}


Anyone knows whats wrong?

Eric the Red Mar 9th, 2007 1:33 AM

I'm not too sure. However, its always a good idea to have something like this when you're implementing Runnable.

:

public void destroy()
        {
                hello = false;
                loop = null;
               
        }


titaniumdecoy Mar 9th, 2007 1:53 AM

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) { }

}


andro Mar 9th, 2007 2:05 AM

I don't see him calling addMouseListener(this) anywhere.

glopal Mar 9th, 2007 1:05 PM

ya, thats what it is, I didnt add it in the init class, thx for the tips

ReggaetonKing Mar 9th, 2007 4:59 PM

Quote:

Originally Posted by glopal (Post 125001)
ya, thats what it is, I didnt add it in the init class, thx for the tips

the init() method not class.


All times are GMT -5. The time now is 2:22 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC