Sign in to follow this  
tscott1213

Java Awt Event's Being sent twice

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this