【问题标题】:Graphics Issue with JFrame in a Game Engine游戏引擎中 JFrame 的图形问题
【发布时间】:2015-04-14 06:42:30
【问题描述】:

我正在为我的大学班级的积木游戏 Collapse 创建一个“简单”的 2D 游戏引擎。我试图让它在任何时候引入新屏幕时都会调整 JFrame 的大小,并且我已经做了两次尝试,一次正确调整大小,但不显示任何内容,另一次显示 SOMETHING,但没有t 调整大小。

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

首先,由于某种原因,即使执行了 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 只是空白类

【问题讨论】:

    标签: java swing graphics jframe game-engine


    【解决方案1】:

    这个,

    SwingUtilities.invokeLater(gameLoop);
    

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

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

    【讨论】:

    • 所以我重新构建了游戏引擎,以便 GameEngine.java 实现 Runnable。在构造函数中我有它 invokeLater(this) 并且在 run() 我也有它 invokeLater(this) 。这使得它自己初始化并循环自己。它按照我想要的方式运行,但我很好奇它是否真的在调用另一个之前完成了工作。让 run() 调用自身来进行循环有什么问题吗?
    • @FoxnEagle 正如该方法的名称所说,它“稍后”运行。主要问题是它本质上是一个繁忙的循环。 invokeLater() 将可运行对象放入队列中,以便在事件调度线程中运行;然后在run() 中调用invokeLater() 将其立即再次放入要运行的队列中- 没有任何暂停。在 EDT 中休眠将是一个非常糟糕的主意,因为在此期间程序也无法执行任何其他与 UI 相关的任务。对于以恒定频率运行的 GUI 任务,您真的应该使用 swing Timer
    • 是否可以安全地重写 paintComponent(Graphics g) 安装一个摆动计时器以限制刷新,并在结束时调用 repaint() ?我试图根据答案中给出的链接制作循环,但我也试图通过终止 running 循环并让它在 内运行来暂停游戏while(true) 循环。但是它要么根本不调用paint(),要么在我终止running循环时丢失实例化的对象
    • 哦,刚刚记住了另一个调用,invokeAndWait() 并将其设置在 while(true) 循环中。它有效,但我上面评论中的问题仍然存在。
    • @FoxnEagle 从计时器的动作侦听器调用repaint() 是习惯性的(就像执行任何其他任务一样,应该定期完成)。当你想暂停时,你可以stop()定时器,当你想恢复时,再次start()它。可以有自己的循环,但它需要位于 EDT 的不同线程中,并且它在访问任何摆动组件时必须使用invokeLater()。使用 Timer 更简单,所以我建议从它开始。任何事情都需要invokeAndWait() 是非常不寻常的。
    猜你喜欢
    • 2018-01-02
    • 2017-08-01
    • 1970-01-01
    • 2011-04-18
    • 1970-01-01
    • 2012-02-15
    • 1970-01-01
    • 2016-04-17
    • 2015-04-14
    相关资源
    最近更新 更多