Question

Just using the paint method and my image won't show up initially. Once I minimize the java window and resize it, the image shows up. Is there any code I'm missing?

public class Lil extends JFrame {

Image image = Toolkit.getDefaultToolkit().getImage("images/Untitled.png");

public Lil(){

    setTitle("flame");
    setBackground(Color.WHITE);
    setSize(400, 400);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setResizable(false);
    setVisible(true);
}

public void paint(Graphics g){

    g.clearRect(0, 0, 400, 400);        
    g.drawImage(image, 60, 25, null);
    //repaint();
}   

public static void main(String [] args){    
        new lil();  
    }
}
Was it helpful?

Solution

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 java.net.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.

OTHER TIPS

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();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top