![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: May 2005
Location: St. Andrews, Manitoba, Canada
Posts: 9
Rep Power: 0
![]() |
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? |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 214
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#3 |
|
Expert Programmer
|
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. |
|
|
|
|
|
#4 |
|
Professional Programmer
|
I don't see him calling addMouseListener(this) anywhere.
|
|
|
|
|
|
#5 |
|
Newbie
Join Date: May 2005
Location: St. Andrews, Manitoba, Canada
Posts: 9
Rep Power: 0
![]() |
ya, thats what it is, I didnt add it in the init class, thx for the tips
|
|
|
|
|
|
#6 |
|
Sexy Programmer
|
the init() method not class.
__________________
I would love to change the world, but they won't give me the source code! |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |