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 :)
Related videos on Youtube
Author by
Saucymeatman
Updated on February 13, 2021Comments
-
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 almost 11 yearsyour 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
orJLabel
withIcon
/ImageIcon
-
Saucymeatman almost 11 yearsI 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 almost 11 yearssee 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 almost 11 yearsThanks alot man! Just what I was looking for. I am very new to using images in java and was completely lost.
-
redeagle47 over 9 yearsCould 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 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 aJPanel
and callspaintComponent
when it does. If you override that method, it'll draw it your way instead of the default way. -
redeagle47 over 9 yearsoh, 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 over 9 yearsit is actually just repaint() by itself
-
Jad Chahine about 8 years@DanielKaplan : Perfect answer.
-
The Raven almost 5 yearsAnswer is nice, but if you have problem with image not displaying at start, move reading the image before buildFrame.