将LibGDX嵌入到Swing中,没有绘制任何内容

时间:2016-01-17 05:58:33

标签: java swing libgdx game-engine

我试图通过LWJGLCanvas将LibGDX游戏嵌入到JFrame中。我试图像许多人那样做。稍后使用SwingUtils.invoke。游戏似乎在后台完美运行(游戏日志输出到终端),但没有任何东西被绘制到我的JFrame或画布上。不确定。我想要实现的是让游戏在jframe的左侧运行,而在右侧,我将为我的关卡编辑器提供所需的所有功能。右边的面板运行得很好。但左边的比赛没有。

如何解决此问题?

以下是代码:

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.badlogic.gdx.backends.lwjgl.LwjglCanvas;
import com.badlogic.gdx.math.Vector2;
import com.boomer.alphaassault.AlphaAssault;
import core.enjineutils.EditPanel;
import core.enjineutils.LevelPanel;
import core.enjineutils.NewLevelPanel;


import javax.swing.*;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;



public class MapEditorLauncher extends JFrame implements         ActionListener,WindowListener{
private static final String TITLE = "EnJine2D Level Editor";

private static final int APP_SIZE_WIDTH = 1224;
private static final int APP_SIZE_HEIGHT = 828;

private LevelPanel levelPanel;
private EditPanel editPanel;
private NewLevelPanel newLevelPanel;

JMenuItem newLevel;
JMenuItem loadLevel;
JMenuItem saveLevel;
JMenuItem exit;

JMenuItem undo;
JMenuItem redo;

JMenuItem showTileGrids;

private LwjglCanvas levelCanvas;





public static void main(String[] args) {
    MapEditorLauncher app = new MapEditorLauncher();
    app.setVisible(true);
}

public MapEditorLauncher() {
    super(TITLE);
    //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setResizable(true);
    addWindowListener(this);





    setLayout(null);
    setSize(APP_SIZE_WIDTH,APP_SIZE_HEIGHT);

    JMenuBar menuBar = new JMenuBar();
    JMenu file = new JMenu("File");
    JMenu edit = new JMenu("Edit");
    JMenu map  = new JMenu("Map");

    newLevel  = new JMenuItem("New Level");
    loadLevel = new JMenuItem("Load Level");
    saveLevel = new JMenuItem("Save Level");
    exit     = new JMenuItem("Exit");

    undo = new JMenuItem("Undo");
    redo = new JMenuItem("Redo");

    showTileGrids  = new JMenuItem("Show tile grids");

    newLevel.addActionListener(this);file.add(newLevel);
    loadLevel.addActionListener(this);file.add(loadLevel);
    saveLevel.addActionListener(this);file.add(saveLevel);
    exit.addActionListener(this);file.add(exit);


    undo.addActionListener(this);edit.add(undo);
    redo.addActionListener(this);edit.add(redo);

    showTileGrids.addActionListener(this);map.add(showTileGrids);

    menuBar.add(file);
    menuBar.add(edit);
    menuBar.add(map);

    setJMenuBar(menuBar);


}


@Override
public void actionPerformed(ActionEvent e) {
    JMenuItem selection = (JMenuItem)e.getSource();
    if(selection.equals(newLevel)){
        popNewLevel();
        return;
    }
}

private void popNewLevel(){
    newLevelPanel = new NewLevelPanel();
    newLevelPanel.initialize();
    if(newLevelPanel.getLevel()!=null){
        initialize();
    }
}

private void initialize(){
    editPanel = new EditPanel();
    levelPanel = new LevelPanel(editPanel);
    levelPanel.setLevel(newLevelPanel.getLevel());

    AlphaAssault gameInstance = new AlphaAssault();
    LwjglApplicationConfiguration configuration = new LwjglApplicationConfiguration();
    configuration.width = 824;
    configuration.height = 768;
    final LwjglCanvas lwjglCanvas = new LwjglCanvas(gameInstance,configuration);
    levelCanvas = lwjglCanvas;


    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            add(lwjglCanvas.getCanvas());
        }
    });

    //add(levelPanel);
    add(editPanel);
    repaint();
}

@Override
public void windowOpened(WindowEvent e) {

}

@Override
public void windowClosing(WindowEvent e) {
    levelCanvas.stop();
}

@Override
public void windowClosed(WindowEvent e) {

}

@Override
public void windowIconified(WindowEvent e) {

}

@Override
public void windowDeiconified(WindowEvent e) {

}

@Override
public void windowActivated(WindowEvent e) {

}

@Override
public void windowDeactivated(WindowEvent e) {

}

}

0 个答案:

没有答案