我正在为我的大学课程中的块游戏Collapse创建一个“简单”的2D游戏引擎。我试图使得JFrame在每次引入新屏幕时都会调整大小,我已经做了两次尝试,一次正确调整大小,但没有显示任何内容,另一次显示SOMETHING,但没有调整大小。
对于这篇文章,我将展示我创建的第二个游戏引擎的代码,因为我的第一个尝试是单片版本,我最终无法跟随自己。
首先,由于某种原因,即使设置了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
我想这篇文章的问题是我的错误是让窗户变得反应迟钝,能够用它制作现代艺术吗?
Collapse.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);
}
}
:|| package gameEngine ||:
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扩展并覆盖所有函数)
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只是空白类
答案 0 :(得分:2)
此,
SwingUtilities.invokeLater(gameLoop);
在事件派发线程中运行游戏循环,阻止它。所以重绘经理从来没有改变来绘制内容。您应该在事件调度线程中create使用GUI,因为从其他线程修改或创建swing组件是不安全的。
游戏循环的计时需要以不阻止EDT的方式完成。最简单的是使用swing Timer;如果后来证明不足,你可以切换到另一个解决方案,但是你需要密切关注线程的安全性。 Swing计时器在EDT中运行动作监听器代码,因此它很容易被线程保护。