Weird Problem in using MouseListener

M

Minti

Hi there all, I am having a weird problem in the following Applet
code,

<begin code>

public class MouseInputTester extends java.applet.Applet {
private String msg = "";
private int mouseX ;
private int mouseY;
static int i = 0;

public MouseInputTester() {
this.addMouseListener(new MouseInput());
}
private class MouseInput implements java.awt.event.MouseListener{
private void setMousecoords(java.awt.event.MouseEvent me){
mouseX = me.getX();
mouseY = me.getY();
}
public void mousePressed(java.awt.event.MouseEvent me ){
msg += "Mouse Pressed";
setMousecoords(me);
repaint();
}
public void mouseClicked(java.awt.event.MouseEvent me){
// msg="";
msg += "Mouse Clicked ";
setMousecoords(me);
repaint();
}
public void mouseEntered(java.awt.event.MouseEvent me){}
public void mouseExited(java.awt.event.MouseEvent me){}
public void mouseReleased(java.awt.event.MouseEvent me){
msg = "Mouse Released";
setMousecoords(me);
repaint();
System.out.println(msg); // msg should have been set to "" in
paint, but that is justnot happening
}
}
public void paint(java.awt.Graphics g){
g.drawString(msg, mouseX, mouseY);
msg = "";
System.out.println("IN paint" + ++i );
}
}

<end code>

The calls to System.out.println() are for debugging purposes only.

The problem is that when the applet is running, whenever mouse is
clicked the applet shows MouseRelasedMouseClicked, even though I set
the msg to "" whenever paint is called, the line with
System.out.println(msg) in mouseReleased() shows that somehow the call
to repaint() just did not occur, Am I missing any point on how the
paint method works.


I belive it is possibly because of the paint queue, that might be
maintained and from which only the most recent call to paint is
executed.
 
M

Matt Humphrey

Minti said:
Hi there all, I am having a weird problem in the following Applet
code,

<begin code>
The problem is that when the applet is running, whenever mouse is
clicked the applet shows MouseRelasedMouseClicked, even though I set
the msg to "" whenever paint is called, the line with
System.out.println(msg) in mouseReleased() shows that somehow the call
to repaint() just did not occur, Am I missing any point on how the
paint method works.

Yes, you're missing the point. Repaint does not call paint. Repaint informs
the rendering system that a particular region needs to be updated. You can
call repaint many times and the region may be marked only once. (In fact, if
the region is obscured it may not even get repainted at all, which is why
paint shouldn't have side-effects.) Moreover, the mouse events are occuring
in the UI thread, which is also used for painting. The painting will occur
at best after the current events are finished--you really wouldn't want to
deal with the complexity of an action thread running in parallel with your
painting thread. Finally, part of the explanation of your result is that
while mousePressed and mouseClicked say msg += " more message", the
mouseReleased handler simply says msg = "mouseReleased", which makes it drop
the mouse pressed message.
I belive it is possibly because of the paint queue, that might be
maintained and from which only the most recent call to paint is
executed.

There is no paint queue. Painting is done from the same UI thread that
handles event dispatching.

Cheers,
Matt Humphrey (e-mail address removed) http://www.iviz.com/
 
I

Ian Shef

(e-mail address removed) (Minti) wrote in @posting.google.com:
Hi there all, I am having a weird problem in the following Applet
code,

<begin code>
<end code>

The calls to System.out.println() are for debugging purposes only.

The problem is that when the applet is running, whenever mouse is
clicked the applet shows MouseRelasedMouseClicked, even though I set
the msg to "" whenever paint is called, the line with
System.out.println(msg) in mouseReleased() shows that somehow the call
to repaint() just did not occur, Am I missing any point on how the
paint method works.


I belive it is possibly because of the paint queue, that might be
maintained and from which only the most recent call to paint is
executed.
Calls to repaint queue up a request for paint to happen.
The paint does not happen immediately, so your listeners cannot depend upon
paint to clear out msg. The paint will occur sometime in the future.
However, multiple requests in the paint queue can be collected together, so
there is not neceesarily a one-to-one correspondence between calls to
repaint and executions of paint.

It is time to come up with a new plan of how to arrange your program.

Good Luck!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top