• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Please help with this program

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Im just trying to create a frame with a canvas on which I want to draw an image. And then I want to use the arrow keys to move the image up, down, left and right. But as of right now, when I run the main program, I just get the frame, and thats it. There is no image display, and I dont know whats up. what am i doing wrong?

and
>
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't use getGraphics(). You should do all painting inside the paintComponent method:
I'll move this thread to our GUI forum.>
 
Shashank Gokhale
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I made the change, but it still doesnt work. Is there something else I did wrong
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Show us your changed code, in the form of an SSCCE <- link
 
Rob Spoor
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I already see (part of) the problem. I misread your code and thought that animationObject extends JPanel (or another component type). It doesn't.

Let animationObject (please rename that to AnimationObject) extends JPanel and replace its canvas field with it. Here's a new SSCCE based on your code, with a few more fixes in it:
It still doesn't work with the key listener, but I think that's because a JPanel simply doesn't handle key events well. I think that key bindings are better.
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By default, a JPanel isn't focusable, and key events only go to the focused component.

Try calling setFocusable(true) and requestFocusInWIndow().
 
Shashank Gokhale
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is SSCCE

Anyway, the code is changed now to what Rob Spoor and Darryl Burke suggested. It does show the image and with those two lines DB said, the keylistener works. If, according to Darryl, the JPanel doesnt handle key events, then should I maybe use a JFrame or a JWindow? And what is the difference in these anyway, JPanel vs. JFrame vs JWindow? Also, Darryl, when you said

By default, a JPanel isn't focusable, and key events only go to the focused component.
Try calling setFocusable(true) and requestFocusInWIndow().


adding those lines to the constructor does make the key events work. But would using JWindow or JFrame work better than JPanel, assuming they handles KeyEvents better and automatically get focus?


 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

would using JWindow or JFrame work better than JPanel


Please go through the APIs for JFrame, RootPaneContainer and JRootPane where you will learn the structure of a top level window.

That will answer your question.
 
Shashank Gokhale
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
okay where do i find the APIs
 
Rob Spoor
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By clicking on the links automatically created in Darryl's post. A similar link is also created for SSCCE so if you want to know what it means, simply click the link.
 
Shashank Gokhale
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the revised code according to your suggestions. There are two things I want to do: one is to make the frame window fullscreen that means without decorations, no status bar , nothing. However, when I type fame.setUndecorated(true) in the main method, it doesnt seem to do anything. And the JPanel dosnt have such a method. If I change the whole thing to JFrame, JFrame doesnt have a paintComponent method. So how do I make it a full window?

Second, I want to program it so that if I press space bar it shoots, but right now, the bullet just stays where it is unless the object is moving. I know I have something wrong, but what?


 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shashank Gokhale wrote:... to make the frame window fullscreen that means without decorations, no status bar , nothing. However, when I type fame.setUndecorated(true) in the main method, it doesnt seem to do anything.


Well, fame and decoration commonly go together

Did you read the API forthe method? It's only two sentences, and the second is important for your situation.


Shashank Gokhale wrote:I want to program it so that if I press space bar it shoots, but right now, the bullet just stays where it is unless the object is moving. I know I have something wrong, but what?


I'm sure you're capable of sorting out the logic. And while you're at it, learn to format your code like the vast majority of Java coders. This line is particularly ugly:
edit: Also note that it is never needed to compare a boolean with true(nor false).[code]// if (shot==true && ...
if (shot && ...
 
Rob Spoor
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out GraphicsDevice and its setFullScreenWindow method. A JFrame IS-A Window. You can get a GraphicsDevice instance through GraphicsConfiguration.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic