java 如何将图像放在窗口的背景中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5956906/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to put an image in the background of a window
提问by Anthony Lagarrigue
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.
我几乎完成了我的刽子手 Java 代码。我想在背景中添加一张图片。(nightsky.png) 我如何在绘制图形方法中执行此操作?我一开始就创建了一个图像图标。
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);
}
回答by camickr
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.
正如已经建议的那样,您只需将 Icon 添加到 JLabel 并将标签添加到您的框架(或面板)。然后,如果您希望图像出现在标签内的某个位置,则只需向标签添加一个 EmptyBorder。
回答by trashgod
You can also construct any image you like on-the-flyusing 2D Graphics
, as suggested in RotatableImage
.
您还可以按照 中的建议使用即时构建您喜欢的任何图像。2D Graphics
RotatableImage
回答by xgMz
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();
回答by Suroot
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).
通过覆盖 JPanel,您可以重做 PaintComponent() 来绘制图像,并且 JPanel 本身应该为其子项提供绘制功能(尽管我尚未测试此功能)。
http://www.java2s.com/Code/Java/Swing-JFC/Panelwithbackgroundimage.htm
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);
}
}