Jump to content
  • Advertisement
Sign in to follow this  
AlysiumX

Displaying images in java

This topic is 2530 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Alright so I started attempting to write a simple 2D game. I was looking for some good tutorials on how to get images on the screen but everyone that I seen was just overwriting the onpaint method which kind of makes me cringe. I would like to control how much my screen is drawing so I tried to use GetGraphics(); on a JPanel and use it to put and image on the screen but i can't seem to get it to work. Should this method work?

Sorry no example code on here, ill be able to post it later.

Can someone maybe type me up a small example so I can get a better idea of how to paint a JPanel control without using the OnPaint method.

Thanks

Share this post


Link to post
Share on other sites
Advertisement
TheJavaHub.

This site is the forums for a Youtube series about java game programming.

You will find that getGraphics only works when you have a BufferStrategy.

For example,you have a method called render().

Within render call:

BufferStrategy bs = getBufferStrategy();
If(bs == null){
createBufferStrategy(2); //for 2d
requestFocus();
return;
}

Graphics g = bs.getDrawGraphics();
g.draw(image, x, y, width, height, null);
g.dispose();
bs.show();

Share this post


Link to post
Share on other sites

Alright so I started attempting to write a simple 2D game. I was looking for some good tutorials on how to get images on the screen but everyone that I seen was just overwriting the onpaint method which kind of makes me cringe. I would like to control how much my screen is drawing so I tried to use GetGraphics(); on a JPanel and use it to put and image on the screen but i can't seem to get it to work. Should this method work?

Sorry no example code on here, ill be able to post it later.

Can someone maybe type me up a small example so I can get a better idea of how to paint a JPanel control without using the OnPaint method.

Thanks


This should help: http://pastie.org/3177881

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!