Curious why my image isn't showing up?

Don't draw directly in a JFrame. Instead draw in a JPanel's paintComponent method and then add that JPanel onto your JFrame's contentPane. Better still, if you're not going to be using the image-component as a container (to hold other components), just create an ImageIcon with the Image, place the icon into a JLabel via its constructor or its setIcon method, and simply display the JLabel.No muss, no fuss, no trouble.

Also, likely there's no need to call clearRect if you call super's paintComponent method as the first call in the JPanel's paintComponent override method.

Don't draw directly in a JFrame. Instead draw in a JPanel's paintComponent method and then add that JPanel onto your JFrame's contentPane. Better still, if you're not going to be using the image-component as a container (to hold other components), just create an ImageIcon with the Image, place the icon into a JLabel via its constructor or its setIcon method, and simply display the JLabel.No muss, no fuss, no trouble.

Also, likely there's no need to call clearRect if you call super's paintComponent method as the first call in the JPanel's paintComponent override method. For instance, if going the more complex route of drawing directly in a JPanel, you'd do something like so: import java.awt. *; import java.awt.image.

*; import java.io. IOException; import URL3. MalformedURLException; import java.net. URL; import javax.imageio.

ImageIO; import javax.swing. *; public class Lil extends JPanel { private static final String URL_PATH = "http://duke.kenai.com/Oracle/" + "OracleStratSmall. Png"; BufferedImage image = null; public Lil() { setBackground(Color.

White); try { image = ImageIO. Read(new URL(URL_PATH)); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } @Override public Dimension getPreferredSize() { if (image! = null) { return new Dimension(image.getWidth(), image.getHeight()); } return super.

GetPreferredSize(); // default } @Override protected void paintComponent(Graphics g) { super. PaintComponent(g); if (image! = null) { g.

DrawImage(image, 0, 0, null); } } public static void main(String args) { SwingUtilities. InvokeLater(new Runnable() { public void run() { Lil lil = new Lil(); JFrame frame = new JFrame(); frame.getContentPane(). Add(lil); frame.

SetTitle("flame"); frame. SetDefaultCloseOperation(JFrame. EXIT_ON_CLOSE); frame.

SetResizable(false); frame.pack(); frame. SetLocationRelativeTo(null); frame. SetVisible(true); } }); } } Again, only do this if you're going to be placing components on the image such as text fields, buttons, and so forth.

If not, then use the simpler ImageIcon/JLabel idea.

Hmm ... as I read the OP the intention is to have a background image on the frame. If so, you have to set the panel as contentPane instead of adding it. Or what am I missing?

– kleopatra Sep 6 at 9:45 3 +1 for hot-linking to an image. BTW - in all the excitement, I think everybody forgot to answer the OP's exact question. So for the record, the problem before was using the non-blocking Toolkit method to load images.

The code posted above uses ImageIO, which reads the entire image before proceeding to the next code line. – Andrew Thompson Sep 6 at 11:23 @kleopatra: I agree, if the image is the background, then the component showing it should become the contentPane. – Hovercraft Full Of Eels Sep 6 at 12:28 @Andrew: I didn't provide the correct answer because I didn't remember that fact about Toolkit.

I now have a hazy recollection of reading about it in the past -- thanks for re-animating that memory! – Hovercraft Full Of Eels Sep 6 at 12:30 'Twas an honor & a privilege to be able to add that small detail. :-) – Andrew Thompson Sep 6 at 12:36.

Hovercraft has already added some suggestions so not repeating those. Your code is not working because you have not called the super classes paint method when you have overridden paint(). Just add one single line at the binging of your paint() method.

Like this: public void paint(Graphics g) { super. Paint(g); g. ClearRect(0, 0, 400, 400); g.

DrawImage(image, 60, 25, null); // repaint(); }.

1 but again, don't override paint, but rather paintComponent, and as noted in my answer, call super. PaintComponent in this method. – Hovercraft Full Of Eels Sep 6 at 10:54 @Hovercraft: Oh, thanks.

I wanted to add that part at the end of my answer. I just forgot :) Good you mentioned. – Mohaimin Sep 7 at 3:02.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions