Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Image Icon not displaying properly

 
William H Taylor
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rob Spoor
Sheriff
Pie
Posts: 20532
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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().
 
Campbell Ritchie
Sheriff
Pie
Posts: 48976
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving thread to our GUIs forum, where we usually discuss such questions.
 
William H Taylor
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you the validate and repaint worked. I wasn't sure where to place the question, sorry about that.
 
Rob Camick
Ranch Hand
Posts: 2584
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic