Search code examples
javaswingjframepaintcomponent

Trying to draw an image on a JFrame


I'm trying to draw an image on a JFrame but it does not seem to work. This is the panel where the image is painted:

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;

public class GraphicsPanel extends JPanel {

public BufferedImage image;

public GraphicsPanel(){

}
  @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(image, 0, 0, null);
        }
}

This is the JFrame:

import java.awt.Dimension;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class Spelplan extends javax.swing.JFrame {

public static BufferedImage image;
public static GraphicsPanel pane;

public Spelplan() {
    try {
        image = ImageIO.read(new File("*IMAGE PATH*"));  
    }
    catch(IOException e) {
        System.out.println("Image not found");
    }

    pane = new GraphicsPanel(); 
    pane.setPreferredSize(new Dimension(image.getWidth(), image.getHeight()));
    pane.setVisible(true);

    initComponents();
    this.setPreferredSize(new Dimension(image.getWidth(), image.getHeight()));
    this.add(pane);
    this.pack();
    pane.repaint();        
}

When the program runs, the JFrame appears in the correct size but without the image drawn. Any idea what I'm doing wrong?


Solution

  • BufferedImage image maybe public but GraphicsPanel never accesses it.
    Anyway, it is better practice to keep image private and use a getter to gain access to it, or pass a reference to the constructor:

    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.net.URL;
    import javax.imageio.ImageIO;
    import javax.swing.JPanel;
    
    public class Spelplan extends javax.swing.JFrame {
    
        public static BufferedImage image;
        private String imagePath = "https://upload.wikimedia.org/wikipedia/commons/3/3f/Crystal_Project_bug.png";
    
        public Spelplan() {
            try {
                URL url = new URL(imagePath);
                image = ImageIO.read(url);
            }
            catch(IOException e) {
                System.out.println("Image not found");
            }
    
            add(new GraphicsPanel(image));
            pack();
            setVisible(true);
        }
    
        public static void main(String[] args){
            new Spelplan();
        }
    }
    
    class GraphicsPanel extends JPanel {
    
        private BufferedImage image;
    
        public GraphicsPanel(BufferedImage image){
            this.image = image;
            setPreferredSize(new Dimension(image.getWidth(), image.getHeight()));
        }
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(image, 0, 0, null);
        }
    }
    

    Always post mcve and use photos url's so others can run your code.