Java Awt Event's Being sent twice

Started by
4 comments, last by seanw 19 years, 7 months ago
Hey all, I am working on a Java Applet for a graphics class I am taking and Java isn't my primary language. The problem I am having is that Mouse Events and Key Events are being sent twice. So, if I hit the 'n' key the KeyTyped() function is called twice. Same thing for mousePressed(), etc. I'm sure I'm doing something silly, but I have no idea what. Thanks for the help. Todd
Advertisement
Could you post a minimal example of your code which exhibits the problem? I can't think of anything of the top of my head that would cause your bug so code would be helpful.
Thanks for taking a look at this...

The code is as follows:

import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class TheGame extends BufferedApplet implements MouseListener, MouseMotionListener, KeyListener
{

...

public void keyTyped(KeyEvent e) {
if(e.getKeyChar()=='n' && bNotDouble == true){
if (nObjActive < KFObjects[nKFActive].size()) {
++nObjActive;
} else {
nObjActive =0;
}
}
}


public void mouseDragged(MouseEvent e) {
Point pt = e.getPoint();
if (bDrawing) {
dostuff()
}
}

}


BufferedApplet is a custom class that extends Applet and implements Runnable. The class just implements double buffering so my graphics don't flicker.

(This is for a graphics class so we are doing everything from the ground up.)


Thanks again.
Todd
What is bNotDouble?
gib.son
Ahh...I meant to pull all of that stuff out of there...

That is a the kluge that I am using so that the program works while I sort this out. So bNotDouble is initialized to true; When a key is pressed it is set to false the first time I enter this function so that the second time it enters the function nothing happens.

I pulled out the rest of the functionality (since it is just a kluge) and forgot to get rid of the rest.

This is a wierd error I am having with the events.
Quote:Original post by tscott1213
Thanks for taking a look at this...

The code is as follows:
<snip>


What I meant by minimal example is for you to strip your code of all the non-essential features and only show the smallest amount of code you have that still shows the bug happening. When you're debugging code, you have to eliminate as many factors as possible and isolate the bug to a small part of your program as it is much easier to find. If you've got a big program with many components, it could be an infinite about of things causing the bug. I think your problem is probably to do with how you add your listeners to your component, but you didn't include the code for that in you post so I can't see.

This topic is closed to new replies.

Advertisement