-2

Possible Duplicate:
Using an image for the background of a JPanel and JButton

Similar question to this one HAVE been asked before, but this is slightly different. I want to know if anyone knows how to set the BACKGROUND of a JPanel/JFrame in swing. I need to place buttons and text fields OVER this image eventually. All the solutions on here so far have been adding the image to a JPanel which doesn't work when I use GridLayout etc.

Community
  • 1
  • 1
Matthew Kemnetz
  • 845
  • 1
  • 15
  • 28
  • @Matthew Kemnetz I vote for closing this question as duplicate too – mKorbel Dec 04 '11 at 12:21
  • @AndrewThompson Yes I did. I implemented it just as you linked there. The problem is that it adds adds the image to the frame instead of making it the background upon which I can overlay buttons... – Matthew Kemnetz Dec 04 '11 at 19:04
  • @AndrewThompson I will delete my question but I just don't understand how to adapt [this](http://stackoverflow.com/questions/2960279/add-other-components-to-jframe-with-background) to make it be the background of a frame... – Matthew Kemnetz Dec 04 '11 at 19:13
  • Ok, I figured out how to make it work. This is my first project with swing so its been tough. But now I have to set the background of the top JPanel to transparent so you can see my Background image. When I try to set that background image to transparent using gui.setOpaque(false) it doesn't work – Matthew Kemnetz Dec 04 '11 at 19:40

1 Answers1

-1

You may create a custom JPanel by inheriting it which paint the given image as background. Here is the code. Here is an example:

public class ImagePanel extends JPanel {
  private Image img;

  public ImagePanel(String img) {
    this(new ImageIcon(img).getImage());
  }

  public ImagePanel(Image img) {
    this.img = img;
    Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
    setPreferredSize(size);
    setMinimumSize(size);
    setMaximumSize(size);
    setSize(size);
    setLayout(null);
  }

  public void paintComponent(Graphics g) {
    g.drawImage(img, 0, 0, null);
  }
}
Jomoos
  • 12,823
  • 10
  • 55
  • 92
  • 3
    Using this code, the image would not fill the panel, and the content would not be painted. BTW 1) When an `ImagePanel` requires an `Image`, specify an `Image` as the argument to the constructor and stop p*ssing about with `String` arguments (pet hate of mine). 2) The sizes (preferred/max/min) are merely suggestions that may or may not be enforced by the layout. 3) A `JPanel` implements `ImageObserver` so can be used as the argument to `getWidth(ImageObserver)` or `getHeight(ImageObserver)`. Please consider reading more replies before adding answers on topics on which you are inexperienced. – Andrew Thompson Dec 04 '11 at 12:22
  • @AndrewThompson +1 for being in educative mood :-) – kleopatra Dec 04 '11 at 12:25
  • 2
    @AndrewThompson Thanks for the information. +1 for explaining my mistakes. – Jomoos Dec 04 '11 at 12:37
  • 1
    You are very gracious about the matter. With that kind of common sense, I can imagine myself up-voting some of your answers in the near future, and am looking forward to it. :) – Andrew Thompson Dec 04 '11 at 12:53