18

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!

Miss Chanandler Bong
  • 4,081
  • 10
  • 26
  • 36
Saucymeatman
  • 587
  • 3
  • 6
  • 8
  • 1
    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](http://docs.oracle.com/javase/tutorial/2d/images/index.html), search here in posts tagged by `paintComponent` or `JLabel` with `Icon`/`ImageIcon` – mKorbel Jul 25 '13 at 18:13
  • 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.... – Saucymeatman Jul 25 '13 at 18:19
  • 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) – mKorbel Jul 25 '13 at 18:23

2 Answers2

34

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;
    }


}
Daniel Kaplan
  • 62,768
  • 50
  • 234
  • 356
  • 1
    Thanks alot man! Just what I was looking for. I am very new to using images in java and was completely lost. – Saucymeatman Jul 25 '13 at 18:22
  • 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. – redeagle47 Jan 13 '15 at 14:40
  • 1
    @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. – Daniel Kaplan Jan 13 '15 at 18:36
  • 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 Jan 13 '15 at 19:40
  • it is actually just repaint() by itself – redeagle47 Jan 13 '15 at 20:15
  • @DanielKaplan : Perfect answer. – Jad Chahine Apr 15 '16 at 20:46
  • Answer is nice, but if you have problem with image not displaying at start, move reading the image before buildFrame. – The Raven May 29 '19 at 12:12
4

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 :)

Coderman69
  • 69
  • 7
NiziL
  • 5,068
  • 23
  • 33