jQuery in Action, 2nd edition*
The moose likes Swing / AWT / SWT and the fly likes Moving object on keyEvents FAILING ! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Moving object on keyEvents FAILING !" Watch "Moving object on keyEvents FAILING !" New topic
Author

Moving object on keyEvents FAILING !

Jibby Lala
Ranch Hand

Joined: Aug 29, 2010
Posts: 35
Hi champs,
Inspite of my deep efforts, i m not able to move the animation on key presses, i applied the tricks and techniques also on the net about the keylisterner, Adapter, jframe, jpanel but still i m failed can somebody fix the problem and can try if he is able to run this animation on keyevents
here is the code which i tried in various ways:



thanks a lot,
jibby
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Jibby Lala wrote:Inspite of my deep efforts, i m not able to move the animation on key presses, i applied the tricks and techniques also on the net about the keylisterner, Adapter, jframe, jpanel but still i m failed can somebody fix the problem and can try if he is able to run this animation on keyevents
here is the code which i tried in various ways:


Before requesting our help, you should do a little debugging to first find out exactly where the problem is. If this were my project I'd sprinkle my code with multiple println statements to see if the problem is with recognizing the key presses, with the drawing, with both, or with something completely different. Also you should add @Override annotations above methods where you think that your code should be overriding a super method -- but may in fact not be -- hint hint (does it make you wonder that the super method is paintComponents?).

Do this and my may be able to fix this yourself. If not, you'll be able to tell us where to focus our attention.

Luck.
Jibby Lala
Ranch Hand

Joined: Aug 29, 2010
Posts: 35
The problem seems to be with keylistener, i was inheriting first the Jpanel and then i saw some other technique with Jframe so i try to adapt that but the problem was still persistent.
thanks
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Jibby Lala wrote:the problem is with keylistener, i was inheriting first the Jpanel and then i saw some other technique with Jframe so i try to adapt that but the problem was still persistent.


You haven't done the @Override yet. It's not just the key listener.
Jibby Lala
Ranch Hand

Joined: Aug 29, 2010
Posts: 35
override what ?
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
@Override is an annotation you use to have the compiler check that the method that you think you are overriding is actually being overridden. For instance you borrowed code that was in a JPanel where the paintComponent method was being overridden. So if you want to make sure that your paintComponent method is actually overriding the super's method, you add the annotation. For instance, this works fine:



Now try compiling this code where you borrow the above JPanel's method and try to place it in a class that extends JFrame:

Will it compile?

Moral of the story, use this annotation frequently to have the compiler test for you. The other moral is to do your drawing in a JPanel, not a JFrame.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
OK, now I see you've changed your paintComponent method to paint, and I strongly recommend against this as you're still drawing directly in the JFrame, which you shouldn't be doing, and you're still calling super.paintComponents(...) which does something completely different. We seem to be going in a bad direction. Please let me suggest a couple of things:

1) Consider starting over as this code is getting to be a mix of things that you don't want.
2) Get one thing working at a time. I'd start with a simple animation without worrying about key presses to start with.
3) Do all your drawing in the paintComponent method of a JPanel and then add that JPanel to a JFrame.
4) This methods first line should be super.paintComponent(g) (no s at the end).
5) Use a Swing Timer not a thread to do the animation... the Swing tutorials will show you how to use this, and also if you search this forum, you'll see example code that uses a Swing Timer for animation.

Then when this is working, work on adding the key presses.
1) Use key binding rather than a key listener. For one thing this will get you out of having to worry so much about the component being listened to having focus. Again the Swing tutorials will show you how to do this.
2) Get this working first by itself, not in an animation app.

Then once the key binding works,
1) Combine the two codes.
Jibby Lala
Ranch Hand

Joined: Aug 29, 2010
Posts: 35
thanks but i am sorry PAINTCOMPONENT is not working on my machine, secondly i follow almost the same approach as you told but now the code is kind of spaghetti because of applying lot of experiments from different forums and tutorials , but it was n't like this i haven't the problem with drawing but only with keyevents(moving).
thanks
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Jibby Lala wrote:thanks but i am sorry PAINTCOMPONENT is not working on my machine, secondly i follow almost the same approach as you told but now the code is kind of spaghetti because of applying lot of experiments from different forums and tutorials

Then some decent refactoring will help.

, but it was n't like this i haven't the problem with drawing but only with keyevents(moving).

I'm sorry, but your initial code as posted could not have drawn as it had paintComponent within a class that extended JFrame, and then when you changed it to paint, yes it will draw, but not correctly. You need to draw in a paintComponent override in a class that extends JPanel or JComponent. If you doubt this, please have a look at the Swing and Graphics2D tutorials and they will corroborate this.

Regarding the key presses, the tutorial on key binding is here: How to use Key Binding

Good luck!
Jibby Lala
Ranch Hand

Joined: Aug 29, 2010
Posts: 35
the some how running solution is there:

cheers
jibby
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Moving object on keyEvents FAILING !
 
Similar Threads
Drawing Problems with JPanel /Jframe need optimize solution.
setting keyboard navigation (shortcut keys) to the Buttons
Double Buffering help!
KeyEvent problem on a JPanel
how can i stop my car crossing the roadsides