Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 8th, 2007, 9:52 PM   #1
glopal
Newbie
 
Join Date: May 2005
Location: St. Andrews, Manitoba, Canada
Posts: 9
Rep Power: 0 glopal is on a distinguished road
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?
glopal is offline   Reply With Quote
Old Mar 9th, 2007, 12:33 AM   #2
Eric the Red
Hobbyist Programmer
 
Eric the Red's Avatar
 
Join Date: Feb 2006
Posts: 214
Rep Power: 0 Eric the Red is an unknown quantity at this point
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;
		
	}
__________________
Death smiles at us all. All a man can do is smile back.
Eric the Red is offline   Reply With Quote
Old Mar 9th, 2007, 12:53 AM   #3
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 836
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
Old Mar 9th, 2007, 1:05 AM   #4
andro
Professional Programmer
 
Join Date: Oct 2005
Location: California
Posts: 288
Rep Power: 3 andro is on a distinguished road
Send a message via AIM to andro
I don't see him calling addMouseListener(this) anywhere.
andro is offline   Reply With Quote
Old Mar 9th, 2007, 12:05 PM   #5
glopal
Newbie
 
Join Date: May 2005
Location: St. Andrews, Manitoba, Canada
Posts: 9
Rep Power: 0 glopal is on a distinguished road
ya, thats what it is, I didnt add it in the init class, thx for the tips
glopal is offline   Reply With Quote
Old Mar 9th, 2007, 3:59 PM   #6
ReggaetonKing
Sexy Programmer
 
ReggaetonKing's Avatar
 
Join Date: Nov 2005
Location: New Jersey
Posts: 891
Rep Power: 3 ReggaetonKing is on a distinguished road
Send a message via AIM to ReggaetonKing
Quote:
Originally Posted by glopal View Post
ya, thats what it is, I didnt add it in the init class, thx for the tips
the init() method not class.
__________________
I would love to change the world, but they won't give me the source code!
ReggaetonKing is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Programming with Java: Tutorial ReggaetonKing Java 7 May 20th, 2008 10:58 AM
Java Applet Duck Java 4 Jun 26th, 2006 1:46 PM
Please consider this java applet WCentauri Java 4 Jun 26th, 2005 10:01 PM
problem with JDBC for talking to Microsoft Access database using a Java Applet captainK Java 4 Mar 20th, 2005 11:01 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:13 AM.

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