By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,984 Members | 997 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,984 IT Pros & Developers. It's quick & easy.

Weird Problem in using MouseListener

P: n/a
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.

--
Imanpreet Singh Arora

Even if you are on the right track you are going to get run over, if
you just keep sitting there.
Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
mi************@yahoo.com (Minti) wrote in news:e87fc4b0.0402021802.7f5fcc26
@posting.google.com:
Hi there all, I am having a weird problem in the following Applet
code,

<begin code> <snip> <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!
--
Ian Shef 805/F6 * These are my personal opinions
Raytheon Company * and not those of my employer.
PO Box 11337 *
Tucson, AZ 85734-1337 *
Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.