我正在为Hangman游戏编写程序,我希望背景是JPEG图像,而不仅仅是纯色。但是当我运行我的程序时,背景图像没有显示出来。有什么想法吗?
HangmanFigure.java
import java.awt.*;
import javax.swing.*;
public class HangmanFigure extends JPanel {
private int guesses;
private Image background;
public HangmanFigure() {
super();
guesses = 0;
setPreferredSize(new Dimension(300, 300));
//setting background color
//setBackground(Color.BLACK);
//setting background image to JPEG
background = Toolkit.getDefaultToolkit().createImage("/Desktop/scaryTown.jpg");
//set to true because we want to see the background
setOpaque(true);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(background, 0, 0, null);
//No need to worry about the rest of the elements in this method, I'm just having issues with the background image!
...
}
}
在另一个名为MainWindow.java的文件中,我有main方法。 (这就是我提供其他所有不属于Hangman数字的图形的图形)。提前谢谢!
答案 0 :(得分:0)
如果使用命令行进行编译,请使用以下命令:
javac -d bin -sourcepath YourSourceFolder pathToTheJavaCode/*.java