Java 游戏引擎中JFrame的图形问题
我正在为我的大学班级创建一个“简单”的2D游戏引擎,用于方块游戏崩溃。我试图使JFrame在任何时候引入新屏幕时都能调整大小,我已经做了两次尝试,一次调整大小正确,但不显示任何内容,另一次显示内容,但不调整大小 在这篇文章中,我将展示我创建的第二个游戏引擎的代码,因为我的第一次尝试是一个单一的版本,最终我在跟随自己时遇到了问题 首先,由于某种原因,即使执行了setDefaultCLoseOperation(JFrame.EXIT_ON_close),窗口也不会用X按钮关闭 第二次(对于第二次尝试),JFrame不会调整大小,但会显示测试开始菜单的一小部分 (抱歉链接,编辑说我必须有10个代表才能发布图片…聪明的举动,但仍然很烦人) 但一旦调整尺寸 我想这篇文章的问题是,我有什么错,让窗户变得没有反应,并能用它来创造现代艺术 Collapse.javaJava 游戏引擎中JFrame的图形问题,java,swing,graphics,jframe,game-engine,Java,Swing,Graphics,Jframe,Game Engine,我正在为我的大学班级创建一个“简单”的2D游戏引擎,用于方块游戏崩溃。我试图使JFrame在任何时候引入新屏幕时都能调整大小,我已经做了两次尝试,一次调整大小正确,但不显示任何内容,另一次显示内容,但不调整大小 在这篇文章中,我将展示我创建的第二个游戏引擎的代码,因为我的第一次尝试是一个单一的版本,最终我在跟随自己时遇到了问题 首先,由于某种原因,即使执行了setDefaultCLoseOperation(JFrame.EXIT_ON_close),窗口也不会用X按钮关闭 第二次(对于第二次尝试
import gameEngine.*;
import screens.*;
import javax.swing.*;
public class Collapse{
private Game game;
public Collapse(){
this.game = new Game("Test", null);
game.getScrMan().setScreen(new StartMenu(game.getScrMan()));
}
public static void main(String[] args){
new Collapse();
}
}
:| |包装屏幕| |
StartMenu.java
package screens;
import gameEngine.*;
import java.awt.*;
import javax.swing.*;
public class StartMenu extends Screen{
private final ScreenManager scrMan;
public StartMenu(ScreenManager scrMan){
super(scrMan);
this.scrMan = scrMan;
}
public void onCreate(){
JButton b1 = new JButton();
scrMan.getScrMan().setSize(200, 200);
scrMan.getScrMan().add(b1);
System.out.println("got here");
}
public void onUpdate(){
}
public void onDraw(Graphics2D g2d){
g2d.setColor(Color.BLACK);
g2d.fillOval(10, 10, 10, 10);
}
public void paintComponent(Graphics g){
g.setColor(Color.BLACK);
g.fillOval(10,10,10,10);
}
}
:| |套装游戏引擎| |:
Game.java
package gameEngine;
import javax.swing.*;
import java.awt.*;
public class Game{
private final ImageIcon image;
private final String title;
private final GameLoop gameLoop;
private final ScreenManager scrMan;
private final InputManager inpMan;
private final EntityManager entMan;
private JFrame window;
private boolean running;
//constructor and initializer for gameLoop
public Game(String title, ImageIcon image){
this.title = title;
this.image = image;
window = new JFrame();
scrMan = new ScreenManager(this);
inpMan = new InputManager();
entMan = new EntityManager();
gameLoop = new GameLoop(this);
initialize();
SwingUtilities.invokeLater(gameLoop);
}
private void initialize(){
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //close on exit
window.setTitle(title); //set the title
if(image != null){} //set the image icon NOT IMPLEMENTED YET
//window.setResizable(false); //user cannot resize
window.setFocusable(true); //
window.setLocationRelativeTo(null); //put in center of screen
window.add(scrMan); //add the
window.pack();
window.setVisible(true);
running = true;
}
public JFrame getWindow(){return window;}
public boolean getRunning(){return running;}
public ScreenManager getScrMan(){return scrMan;}
public InputManager getInput(){return inpMan;}
public EntityManager getEntMan(){return entMan;}
public void paused(){running = false;}
public void resume(){running = true;}
}
GameLoop.java
package gameEngine;
import java.lang.Runnable;
public class GameLoop implements Runnable{
private final Game game;
private long time;
private final int fps = 30;
public GameLoop(Game game){this.game = game;}
public void run(){
while(true){
while(game.getRunning()){
//System.out.println("running");//debugging
if(game.getScrMan().getScreen() != null){
game.getScrMan().getScreen().onUpdate();
//System.out.println("screen is updating");//debugging
}
//fps clock, commenting out does not fix problem
time = System.currentTimeMillis();
time = (1000/fps) - (System.currentTimeMillis() - time);
if(time > 0){try{Thread.sleep(time);}catch(Exception e){}}
}
}
}
}
ScreenManager.java
package gameEngine;
import javax.swing.JPanel;
import java.awt.*;
public class ScreenManager extends JPanel{
private final Game game;
private Screen screen;
public ScreenManager(Game game){this.game = game;}
public void setScreen(Screen screen){
this.screen = screen;
this.removeAll();
screen.onCreate();
game.getWindow().revalidate();
//game.getWindow().pack();
}
public Screen getScreen(){return screen;}
public Game getGame(){return game;}
public ScreenManager getScrMan(){return this;}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.BLACK);
g.fillOval(0,0, 10, 10);
}
}
Screen.java(StartMenu扩展并覆盖所有函数)
java(还没有实现,只是一个占位符)
EntityManager.java和Entity.java只是空白类
SwingUtilities.invokeLater(gameLoop);
在事件调度线程中运行游戏循环,阻止它。因此,重新绘制管理器从未更改过绘制内容。您应该在事件分派线程中使用GUI,因为从其他线程修改或创建swing组件是不安全的
游戏循环的计时需要以不妨碍EDT的方式进行。最简单的方法是使用;如果后来证明这是不够的,您可以切换到另一个解决方案,但是您需要密切注意线程安全。Swing timer在EDT中运行操作侦听器代码,这样就很容易实现线程安全。因此我重新构建了游戏引擎,以便GameEngine.java实现Runnable。在构造函数中有invokeLater(this),在run()中也有invokeLater(this)。这使它自身初始化并循环自身。它按我希望的方式运行,但我很好奇它是否真的在调用另一个之前完成了工作。让run()调用自身来进行循环有什么不对吗?@FoxnEagle正如该方法的名称所说,它会“稍后”运行。主要问题是它本质上是一个繁忙的循环
invokeLater()
将runnable放在队列中,以便在事件调度线程中运行;然后在run()
中调用invokeLater()
,将其立即再次放入要运行的队列中,而不需要任何暂停。在EDT中睡觉是一个非常糟糕的主意,因为在此期间,程序也不能执行任何其他与UI相关的任务。对于以恒定频率运行的GUI任务,您应该真正使用swing计时器
。然后安装swing计时器以限制刷新,并在最后调用repaint()是否安全?我试图使循环基于答案中给出的链接,但我也试图通过终止运行循环并使其在while(true)循环中运行来暂停游戏。但它要么根本不调用paint(),要么在我终止运行的loopOh时丢失实例化的对象,只记得另一个调用,invokeAndWait(),并在while(true)循环中设置它。这是可行的,但我上面评论中的问题仍然存在。@FoxnEagle从计时器的动作侦听器调用repaint()
(与执行任何其他任务一样,应该定期执行)。当您想暂停时,您可以stop()
计时器,当您想继续时,您可以start()
再次启动计时器。您可以拥有自己的循环,但它需要位于EDT使用的不同线程中,并且在访问任何swing组件时必须使用invokeLater()
。使用计时器更简单,所以我建议从这个开始。任何事情都需要invokeAndWait()
,这是非常罕见的。
package gameEngine;
import java.awt.event.*;
public class InputManager implements KeyListener, MouseListener{
public void mouseClicked(MouseEvent event){
}
public void mouseEntered(MouseEvent event){
}
public void mouseExited(MouseEvent event){
}
public void mousePressed(MouseEvent event){
}
public void mouseReleased(MouseEvent event){
}
public void keyPressed(KeyEvent event){
}
public void keyReleased(KeyEvent event){
}
public void keyTyped(KeyEvent event){
}
}
SwingUtilities.invokeLater(gameLoop);