File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Please help with this program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Please help with this program" Watch "Please help with this program" New topic
Author

Please help with this program

Shashank Gokhale
Ranch Hand

Joined: Jan 07, 2003
Posts: 92
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
>


May the force of the Java be in all of us !!!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

Don't use getGraphics(). You should do all painting inside the paintComponent method:
I'll move this thread to our GUI forum.>


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Shashank Gokhale
Ranch Hand

Joined: Jan 07, 2003
Posts: 92
I made the change, but it still doesnt work. Is there something else I did wrong
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4531
    
    5

Show us your changed code, in the form of an SSCCE <- link


luck, db
There are no new questions, but there may be new answers.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

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

Joined: May 03, 2008
Posts: 4531
    
    5

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

Joined: Jan 07, 2003
Posts: 92
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

Joined: May 03, 2008
Posts: 4531
    
    5

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

Joined: Jan 07, 2003
Posts: 92
okay where do i find the APIs
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

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

Joined: Jan 07, 2003
Posts: 92
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

Joined: May 03, 2008
Posts: 4531
    
    5

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

Joined: Oct 27, 2005
Posts: 19684
    
  20

Check out GraphicsDevice and its setFullScreenWindow method. A JFrame IS-A Window. You can get a GraphicsDevice instance through GraphicsConfiguration.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Please help with this program