Anthony Lagarrigue
Anthony Lagarrigue

Reputation: 31

How to put an image in the background of a window

I'm almost done with my hangman java code. I want to add a picture in the background though.(nightsky.png) How do I do this in the paint graphics method? I created a imageicon in the beginning.

public HangmanRevised() {
    setSize(600,400);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setLayout(new FlowLayout());

    ImageIcon background = new ImageIcon("nightsky.png");
    Letter = new TextField();
    JLabel label = new JLabel("pick a Letter");
    button = new Button("Enter");
    add(label);
    add(button);
    add(Letter);

    button.addActionListener(this);

    createGame(); 
} 

public void paint(Graphics g) {
    super.paint(g);
    g.drawImage(background, 0, 156, Color.green, button);
}

Upvotes: 1

Views: 12319

Answers (4)

trashgod
trashgod

Reputation: 205885

You can also construct any image you like on-the-fly using 2D Graphics, as suggested in RotatableImage.

Upvotes: 0

camickr
camickr

Reputation: 324207

If you are painting the image at its actual size, there is no need to do any custom painting.

As has already been suggested you just add the Icon to a JLabel and add the label to your frame (or panel). Then if you want the image to appear at a certion position within the label, then you simply add an EmptyBorder to the label.

Upvotes: 1

xgMz
xgMz

Reputation: 3354

You need to put the background somewhere, i.e.:

//add the following in the HangmanRevised() constructor (?)

button.addActionListener(this);

//To add
ImagePanel panel = new ImagePanel(background.getImage());
JFrame frame = new JFrame();
frame.getContentPane().add(panel);
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
add(frame); 
//end...

createGame(); 

Upvotes: 0

Suroot
Suroot

Reputation: 4423

By overriding a JPanel, you can redo paintComponent() to paint the image and the JPanel itself should have a paint function for its children (although I haven't tested this functionality).

http://www.java2s.com/Code/Java/Swing-JFC/Panelwithbackgroundimage.htm

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ImageTest {

  public static void main(String[] args) {
    ImagePanel panel = new ImagePanel(new ImageIcon("images/background.png").getImage());

    JFrame frame = new JFrame();
    frame.getContentPane().add(panel);
    frame.pack();
    frame.setVisible(true);
  }
}

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

}

Upvotes: 0

Related Questions