• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Image Icon not displaying properly

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
}
}
 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22682
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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().
 
Marshal
Posts: 76406
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you the validate and repaint worked. I wasn't sure where to place the question, sorry about that.
 
Rancher
Posts: 3279
30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Anderson gave himself the promotion. So I gave myself this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic