How do I draw an image to a JPanel or JFrame?

108,902

Solution 1

Try this:

package com.sandbox;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class SwingSandbox {

    public static void main(String[] args) throws IOException {
        JFrame frame = buildFrame();

        final BufferedImage image = ImageIO.read(new File("C:\\Projects\\MavenSandbox\\src\\main\\resources\\img.jpg"));

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


        frame.add(pane);
    }


    private static JFrame buildFrame() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setSize(200, 200);
        frame.setVisible(true);
        return frame;
    }


}

Solution 2

There are a lot of methods, but I always override the paint(Graphics g) of a JComponent and use g.drawImage(...)

edit: I was making a sample, but Daniel Kaplan did it perfectly, look at his answer :)

Share:
108,902

Related videos on Youtube

Saucymeatman
Author by

Saucymeatman

Updated on February 13, 2021

Comments

  • Saucymeatman
    Saucymeatman about 3 years

    How do I draw an Image to a JPanel or JFrame, I have already read oracle's tutorial on this but I can't seem to get it right. I need the image "BeachRoad.png" to be displayed on a specific set of coordinates. Here is what I have so far.

    public class Level1  extends JFrame implements ActionListener {
    
    static JLayeredPane EverythingButPlayer;
    static Level1 l1;
    
    public Level1() {
        EverythingButPlayer = new JLayeredPane();
    
        BufferedImage img = null;
        try {
            img = ImageIO.read(new File("BeachRoad.png"));
        } catch (IOException e) {
        }
        Graphics g = img.getGraphics();
        g.drawImage(img,0, 0, EverythingButPlayer);
    
    
        this.add(EverythingButPlayer);
    }
    

    And in the Main(),

    l1 = new Level1();
    l1.setTitle("poop");
    l1.setSize(1920, 1080);
    l1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    l1.setVisible(true);
    

    Thanks in advance!

    • mKorbel
      mKorbel almost 11 years
      your code is direct way to the troubles or how to hate programming languages (whatever.getGraphics()) 1. please to read Oracle tutorial Working with Images, search here in posts tagged by paintComponent or JLabel with Icon/ImageIcon
    • Saucymeatman
      Saucymeatman almost 11 years
      I already stated in the first part of the question that I read Oracles Tutorial on Working with images. And I cant really understand the first thing you said....
    • mKorbel
      mKorbel almost 11 years
      see answer by @tieTYT, to start with paintComponent (forgot about paint()) and JPanel, is required ot override getPreferredSize (forgot about JFrame.setSize()) for JPanel and then call JFrame.pack() (search here, half of questions about paiting Image, Shapes, Line)
  • Saucymeatman
    Saucymeatman almost 11 years
    Thanks alot man! Just what I was looking for. I am very new to using images in java and was completely lost.
  • redeagle47
    redeagle47 over 9 years
    Could you show an example of how that paintComponent method would be called? It looks like it would be pane.paintComponent(), but I don't understand what argument would be passed to it for g.
  • Rana Ghosh
    Rana Ghosh over 9 years
    @redeagle47 that's a good question but unfortunately I don't know the answer (or I forgot). This is simply a contract you're promising Swing you will implement. The details of how you get that g comes from Swing internals, not from any code you write. Swing renders a JPanel and calls paintComponent when it does. If you override that method, it'll draw it your way instead of the default way.
  • redeagle47
    redeagle47 over 9 years
    oh, actually I was reading through the docs and I think I figured it out. I think pane.repaint() would be the correct method; it should invoke paintComponent() through the higher level container. I'm not 100% that this is the correct syntax, but it's something like that.
  • redeagle47
    redeagle47 over 9 years
    it is actually just repaint() by itself
  • Jad Chahine
    Jad Chahine about 8 years
    @DanielKaplan : Perfect answer.
  • The Raven
    The Raven almost 5 years
    Answer is nice, but if you have problem with image not displaying at start, move reading the image before buildFrame.