wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Image Icon not displaying properly Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Image Icon not displaying properly" Watch "Image Icon not displaying properly" New topic

Image Icon not displaying properly

William H Taylor

Joined: Apr 22, 2009
Posts: 16

Can anyone tell me how to get an image to display without having to resize the java applet first. I wrote a program to display an image but the image wont display unless I resize the web page or the applet viewer. Thank you for your time. I am using java 6 on a vista 64 box. I used the paintComponent method with the imageicon painticon method.

public class PhotoPanel extends JPanel{
ClassLoader cldr = getClass().getClassLoader();
String file = "sausages2.jpg";
URL fileName = cldr.getResource(file);
ImageIcon icon = new ImageIcon(fileName);
public PhotoPanel(String aFileName){
file = aFileName;

public void paintComponent(Graphics g){
icon.paintIcon(this, g, 0, 0);
Martin Vanyavchich
Ranch Hand

Joined: Sep 16, 2008
Posts: 241
I'm not sure, but I think you just need to repaint your PhotoPanel. That happends automaticaly every time you resize.

Next time please use code tags

I no good English.
Rob Spoor

Joined: Oct 27, 2005
Posts: 19655

No need to repaint any component inside its own constructor.

William, did you add an instance of your panel after showing the applet? If so, you must revalidate() and repaint(). If revalidate() is not available (e.g. in a JFrame) then use invalidate() and validate().

How To Ask Questions How To Answer Questions
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 38045
Moving thread to our GUIs forum, where we usually discuss such questions.
William H Taylor

Joined: Apr 22, 2009
Posts: 16
Thank you the validate and repaint worked. I wasn't sure where to place the question, sorry about that.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2155
Why are you extending JPanel to do this?

All you need to do is add the ImageIcon to a JLable and add the label to the content pane of the applet.
I agree. Here's the link: http://aspose.com/file-tools
subject: Image Icon not displaying properly
Similar Threads
small doubt in Swing appln using JPanel
Hello Dear, I want image in JInternalFrame in this code below please help......thanks regards
Can i add an image directly to a JLabel or JWindow?
problem in setting background image of jpanel
getImageIcon() : how does it work?