我需要一些帮助来定位我在这里犯的错误。我对使用 Swing 还比较陌生,这是我尝试制作的第一个游戏。
问题如下:代码编译正常,但是当我运行此代码时,框架中没有任何显示。我注意到,如果我删除 Player 类中对grabFrame的调用,并取消注释 g.drawRect(...),则框架中将显示一个矩形,但是当我重新添加grabFrame调用时,再次没有任何显示。
游戏面板:
import java.awt.Dimension;
import javax.swing.JFrame;
public class GamePanel extends JFrame {
final static int GWIDTH = 512;
final static int GHEIGHT = 512;
static final Dimension screenSize = new Dimension(GWIDTH, GHEIGHT);
JavaGame game;
public GamePanel () {
this.setTitle("Game");
this.setSize(screenSize);
this.setResizable(false);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
game = new JavaGame(screenSize);
add(game);
}
public static void main (String[] args) {
GamePanel gp = new GamePanel();
}
}
Java游戏:
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JPanel;
class JavaGame extends JPanel {
// Game variables
private GameEngine gameEngine;
public JavaGame(Dimension screenSize) {
this.setPreferredSize(screenSize);
gameEngine = new GameEngine();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
gameEngine.draw(g);
}
}
游戏引擎:
import java.awt.Graphics;
public class GameEngine {
Player p1;
public GameEngine () {
p1 = new Player();
}
public void draw(Graphics g) {
drawCharacters(g);
}
private void drawCharacters(Graphics g) {
p1.draw(g);
}
}
玩家:
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.imageio.ImageIO;
public class Player {
private BufferedImage frame;
private URL frameURL;
public Player() {
try {
frameURL = new File("C:/Users/admin/workspace/JavaGame/src/civ_walk_south.png").toURI().toURL();
} catch (MalformedURLException e) {
e.printStackTrace();
}
grabFrame();
}
private void grabFrame() {
try {
frame = ImageIO.read(frameURL);
} catch (IOException e) {
e.printStackTrace();
}
}
public void draw(Graphics g) {
g.drawImage(frame, 50, 50, null);
//g.drawRect(50, 50, 50, 50); // put this here for testing, when uncommented it only works if call to grabFrame() is removed from constructor
}
}
最佳答案
关于:
public GamePanel () {
this.setTitle("Game");
this.setSize(screenSize);
this.setResizable(false);
this.setVisible(true); // *****
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
game = new Game(screenSize);
add(game);
}
添加所有组件之后在 JFrame 上调用 setVisible(true)
,而不是之前。
所以,....
public GamePanel () {
this.setTitle("Game");
// this.setSize(screenSize); // **** don't set sizes like this
this.setResizable(false);
// this.setVisible(true); // *****
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
game = new Game(screenSize);
add(game);
pack(); // **** this should size the GUI to the preferred sizes
setVisible(true); // **** call this here ****
}
<小时/>
另外,你在绘制之前测试过获取到的图像是否为空吗?
这对我来说很有趣,因为您的图像路径似乎位于将包含在 jar 文件内的位置:
frameURL = new File("C:/Users/admin/workspace/JavaGame/src/civ_walk_south.png")
.toURI().toURL();
为什么不将图像作为资源而不是首先作为文件获取?
关于java - 编写 java 游戏 - JFrame 未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20591286/