Java Swing: how to add an image to a JPanel ?

Posted by Leonel on Stack Overflow See other posts from Stack Overflow or by Leonel
Published on 2008-11-18T17:36:05Z Indexed on 2010/04/25 0:53 UTC
Read the original article Hit count: 371

Filed under:
|
|
|

I have a JPanel to which I'd like to add JPEG and PNG images that I generate on the fly.

All the examples I've seen so far in the Swing Tutorials, specially in the Swing examples use ImageIcons.

I'm generating these images as byte arrays, and they are usually larger than the common icon they use in the examples, at 640x480.

  1. Is there any (performance or other) problem in using the ImageIcon class to display an image that size in a JPanel ?
  2. What's the usual way of doing it ?
  3. How to add an image to a JPanel without using the ImageIcon class ?

Edit: A more careful examination of the tutorials and the API shows that you cannot add an ImageIcon directly to a JPanel. Instead, they achieve the same effect by setting the image as an icon of a JLabel. This just doesn't fill right...

© Stack Overflow or respective owner

Related posts about java

Related posts about swing