aspose file tools*
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 Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Image Icon not displaying properly" Watch "Image Icon not displaying properly" New topic
Author

Image Icon not displaying properly

William H Taylor
Greenhorn

Joined: Apr 22, 2009
Posts: 16
Hello,

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){
super();
file = aFileName;

}
public void paintComponent(Graphics g){
super.paintComponent(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


SCJP 6, OCMJD 6, OCPJWSD 6
I no good English.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

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().


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

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

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: 2200
    
    7
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Image Icon not displaying properly