• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

Moving object on keyEvents FAILING !

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
override what ?
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the some how running solution is there:

cheers
jibby
 
This one time, at bandcamp, I had relations with a tiny ad.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic