1

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);
}
Amir Pashazadeh
  • 7,170
  • 3
  • 39
  • 69

4 Answers4

1

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.

camickr
  • 321,443
  • 19
  • 166
  • 288
0

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(); 
xgMz
  • 3,334
  • 2
  • 30
  • 23
0

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

}
Suroot
  • 4,315
  • 1
  • 22
  • 28
0

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

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045