我想帮助找到我在这里犯的错误。我使用Swing相对较新,这是我试图制作的第一款游戏。
问题如下:代码编译正常,但是当我运行此代码时,框架中没有任何内容显示。我注意到如果我在Player类中删除了对grabFrame的调用,并取消注释g.drawRect(...),那么框架中会显示一个矩形,但是当我再次添加了grabFrame调用时,再也没有显示
的GamePanel:
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();
}
}
JavaGame:
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);
}
}
GameEngine:
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
}
}
答案 0 :(得分:1)
关于:
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();
为什么不将您的图像作为资源而不是首先作为文件?
答案 1 :(得分:0)
将add(game)
放在topit也可以帮助最后setVisible(true)
。您还想调用pack
方法
在java游戏类中,不要在java游戏类中声明this.setPreferredSize(screensize)
而是将其放在游戏面板类中,也不要只调用setPreferredSize()
你想要setMaximumSize()
和{ {1}}所以你的游戏小组类看起来应该是这样的。
setMinimumSize()
希望这有帮助。