0

How are images drawn in java, im at a loss. I've managed to get a BufferedImage from a file and then use getGraphics() and g.drawImage() but this doesnt work. I've seen suggestions for a class called paintComponent() then super(g).paintComponent but this doesnt work.

I have a JPanel called panel and a JFrame called frame. I was originally trying to add an image to the panel but i thought just Graphics would work. I want an image to draw across my entire gui, ive got two ints for width and height so not a problem.

Ive looked at java docs and other posts without finding an answer, this is my last resort :/

Thanks in advance

user2687097
  • 17
  • 1
  • 6

1 Answers1

0

There are several ways. The simplest would be to wrap the BufferedImage in an ImageIcon and apply it to a JLabel...

BufferedImage img = ...
JLabel label = new JLabel(new ImageIcon(img));
// add the label to something ...

Take a look at How to use labels for more details

If you need something more complicated, you should use look at Performing Custom Painting and Drawing an Image for mor details

If you need to resize the image, take a look at Java: maintaining aspect ratio of JPanel background image

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • This is very helpful, didnt think of using JLabels, might be my answer – user2687097 Dec 07 '13 at 20:47
  • Remember, JLabel is just container, so if you need to, you can simply set a layout manager on it and add other components to it...makes for a nice background component... – MadProgrammer Dec 07 '13 at 20:51