gpt4 book ai didi

java - 游戏引擎中 JFrame 的图形问题

转载 作者:行者123 更新时间:2023-12-02 04:46:26 25 4
gpt4 key购买 nike

我正在为我的大学类(class)的 block 游戏 Collapse 创建一个“简单”2D 游戏引擎。我试图使其在引入新屏幕时调整 JFrame 的大小,并且我已经进行了两次尝试,一种可以正确调整大小,但不显示任何内容,另一种可以显示某些内容,但不显示任何内容调整大小。

在这篇文章中,我将展示我创建的第二个游戏引擎的代码,因为我的第一次尝试是一个整体版本,但我最终无法跟随自己。

首先,由于某种原因,即使启用了 setDefaultCLoseOperation(JFrame.EXIT_ON_CLOSE),窗口也不会通过 X 按钮关闭。

第二次(第二次尝试)JFrame 将不会调整大小,但会显示测试 StartMenu 的一小部分

(抱歉链接,编辑说我必须有 10 次代表才能发布图片......明智之举,但仍然很烦人) http://i1148.photobucket.com/albums/o577/FoxnEagle/GraphicsWindow1_zpscqdgsjqh.png

但在调整大小时;

http://i1148.photobucket.com/albums/o577/FoxnEagle/GraphicsWindow2_zpsckr4eohs.png

我想这篇文章的问题是我做错了什么,让窗口变得没有响应,并能够用它来制作现代艺术?

崩溃.java

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);
}

}

:||打包游戏引擎||:

游戏.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 扩展并覆盖所有功能)

package gameEngine;

import java.awt.Graphics;
import java.awt.Graphics2D;

public abstract class Screen{
private final ScreenManager scrMan;

public Screen(ScreenManager scrMan){this.scrMan = scrMan;}

public abstract void onCreate();
public abstract void onUpdate();
public abstract void onDraw(Graphics2D g2d);
}

InputManager.java(尚未实现任何内容,只是一个占位符)

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){
}
}

EntityManager.java 和 Entity.java 只是空白类

最佳答案

这个,

SwingUtilities.invokeLater(gameLoop);

在事件调度线程中运行游戏循环,并阻止它。因此重绘管理器永远不会更改绘制内容。你应该 create事件调度线程中的 GUI,因为从其他线程修改或创建 swing 组件是不安全的。

游戏循环的计时需要以不阻塞 EDT 的方式完成。最简单的是使用 swing Timer ;如果后来证明这还不够,您可以切换到另一个解决方案,但是您需要密切注意线程安全。 Swing 计时器在 EDT 中运行操作监听器代码,因此很容易实现线程安全。

关于java - 游戏引擎中 JFrame 的图形问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29620780/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com