Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Which is better JLabel ImageIcon or PaintComponent?  RSS feed

 
Sumi Kuma
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all, Good Evening,
I have coded a 2d game in java. Works fine.
What I'm doing is painting a screen size BufferedImage and then painting my objects (smaller pictures  on this.
Then every 10 milliseconds this BufferedImage gets updated moving the object a little bit on the screen sized BufferedImage according to the key pressed by the user.
Finally, I'm loading the BufferedImage on a JLabel whose size is same as screen size.
Everything works fine, but I was hoping that there would be a better way than displaying the image in a JLabel.

I searched, and it appears that images can be painted on the PaintComponent, but this requires extending the JPanel class.
I can't do this because I'm extending another class. The class which does all the heavy lifting in the game. All Image manipulation methods are in this class I'm extending.

Should I stick with the JLabel approach, or should I try and Override the PaintComponent method in "my Image" class?
Which way is better? Is there an advantage in using the PaintComponent over JLabel?
Thanks

 
Piet Souris
Rancher
Posts: 1980
67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Sumi,

well, the panel into which you have this JLabel can have its 'paintComponent' overridden.
This has some advantages above using a JLabel. When the window gets resized, you can
simply resize your BufferedImage as well.
The technique to use (in paintComponent) is: g.drawImage(...).

Have a look at the API of the Graphics or Graphics2D class, you will find several
versions of this method.

But there is not a 'better' way. Just use the method that you know or like best.
As you write: your current program works!
 
Sumi Kuma
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
The frame doesn't get resized in the program. The game starts full screen. So I guess I can stay with JLabel.
Thank you

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!