我第一次搞乱java图形,经过大量搜索后,我似乎仍然无法成功添加背景,我将背景图像放在一个单独的文件中,预先绘制,我正在尝试将其导入到我的项目中...... 任何帮助将非常感激!
public static void startMenu(){
ImagePanel panel = new ImagePanel(new ImageIcon("C:/Background.jpg").getImage());
// Loads the background image and stores in img object
Window game = new Window();
JFrame frame = new JFrame();
JButton startButton = new JButton("Start"); //makes a button with the text Start
startButton.setBounds(300, 400, 89, 23);
startButton.setVisible(true);
frame.getContentPane().add(startButton);
frame.getContentPane().add(panel);
frame.add(game);
frame.pack();
frame.setTitle(title);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setSize(WIDTH, HEIGHT);
frame.setLocationRelativeTo(null); // starts it in the center of the
// screen
frame.setVisible(true);
game.start(); // Initialize start method
}
public static void startButton(){
}
}
class ImagePanel extends JPanel {
private Image backgroundImg;
public ImagePanel(String backgroundImg) {
this(new ImageIcon(backgroundImg).getImage());
}
public ImagePanel(Image backgroundImg) {
Dimension size = new Dimension(backgroundImg.getWidth(null), backgroundImg.getHeight(null));
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
setSize(size);
setLayout(null);
}
public void paintComponent(Graphics g) {
g.drawImage(backgroundImg, 0, 0, null);
}
}
最佳答案
一些事情
查看您正在调用的构造函数:
new ImagePanel(new ImageIcon("C:/Background.jpg").getImage());
然后看构造函数和
ImagePanel
类class ImagePanel extends JPanel { private Image backgroundImg; public ImagePanel(Image backgroundImg) { Dimension size = new Dimension( backgroundImg.getWidth(null), backgroundImg.getHeight(null)); ... } public void paintComponent(Graphics g) { g.drawImage(backgroundImg, 0, 0, null); } }
因此,除了使用其大小之外,您实际上并未对传递给构造函数的
Image
执行任何操作。您可能应该使用它来初始化backgroundImg
。就像这样:private Image backgroundImg; public ImagePanel(Image backgroundImg) { this.backgroundImg = backgroundImg; }
看看这些行
frame.getContentPane().add(startButton); frame.getContentPane().add(panel); frame.add(game);
仅供引用,
frame.add()
与frame.getContentPane().add()
执行相同的操作。在您的情况下,由于您没有更改框架/内容 Pane 的布局,因此您将保留默认的BorderLayout
。这对您来说意味着,如果您在添加组件时未指定位置(例如frame.add(component, BorderLayout.PAGE_END)
),那么您将隐式尝试添加到中心(例如frame.add(component, BorderLayout.CENTER)
)。问题是每个BorderLayout
位置只能容纳一个组件 - 最后添加的组件获胜。因此,在您的情况下,游戏
获胜。参见Laying out Components Withing a Container了解布局管理的其他一些想法。另外更直接的参见How to use
BorderLayout
Window
是一个 AWT 组件。您不应该尝试将 AWT 组件添加到 Swing 组件中。尽管不太确定窗口的用途,但使用JPanel
或JComponent
可能会更好g.drawImage(backgroundImg, 0, 0, null);
→null
应该改为this
。删除
ImagePanel
构造函数中的所有get[Xxx]Size()
。您不需要它们,最好重写getPreferredSize()
。class ImagePanel extends JPanel { private Image backgroundImg; public ImagePanel(Image backgroundImg) { this.backgroundImg = backgroundImg; } @Override public Dimension getPreferredSize() { return new Dimension(backgroundImage.getWidth(this), backgroundImage.getHeight(this)); } }
另请查看Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?
关于Java-图形-导入背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26068941/