aspose file tools*
The moose likes Game Development and the fly likes Drawing images in Jpanel, JFrame and game loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Game Development
Bookmark "Drawing images in Jpanel, JFrame and game loop" Watch "Drawing images in Jpanel, JFrame and game loop" New topic
Author

Drawing images in Jpanel, JFrame and game loop

Muteking Tonello
Greenhorn

Joined: Jul 18, 2013
Posts: 8
Hello, I'm new, here!
I'm learning to code my first game.
I need to figure how to paint a simple image in the game loop.
As now, I did this but I cannot see anything.
I have another class wich is a Jframe that sets the content of the following

Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10376
    
    8

Welcome to the Ranch.
Recommended reading: http://docs.oracle.com/javase/tutorial/uiswing/painting/


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Muteking Tonello
Greenhorn

Joined: Jul 18, 2013
Posts: 8
I think the problem was just in the other class. Silly me.
Now I'm moving to the keylistener.
Thanks for the link.
One thing I did was moving the load method in the init clas. I suppose it's better because it only calls images once and the leave the repaint in the run method.
I guess.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10376
    
    8

Muteking Tonello wrote:I think the problem was just in the other class. Silly me.

Not really. Your myPanel extends JPanel.
As per the link I posted, the recommended way to go is override paintComponent
Muteking Tonello
Greenhorn

Joined: Jul 18, 2013
Posts: 8
Yes. It's working like a charm. It doesn't flicker. Does that mean that paintComponent and Jpanel don't need double buffering?
Thanks for the support!
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10376
    
    8

Muteking Tonello wrote:Does that mean that paintComponent and Jpanel don't need double buffering?

Double buffering is turned on by default for JPanel. You don't explicitly need to switch it on.
Christopher McKay
Ranch Hand

Joined: Oct 19, 2013
Posts: 47

In future, using a game library would probably be beneficial as you don't run into low-level problems like this. I am using LibGDX and am finding it much easier to code games in than the built-in java libs.
 
wood burning stoves
 
subject: Drawing images in Jpanel, JFrame and game loop