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?
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.
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?
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.
Joined: Jan 07, 2003
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?
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 && ...