java - LwjglCanvas 与 JFrame 调整大小

标签 java swing opengl libgdx jframe

我有一个桌面 LibGDX 项目,我的主 (DesktopLauncher) 类扩展了 JFrame(它将是未修饰且不可调整大小的)。我在 LwjglCanvas 中渲染 LibGDX OpenGL Canvas ,该 Canvas 作为组件添加到容器中。现在有一个奇怪的问题:有时我可以看到 Canvas 周围有 3-4-5 像素的边框,这使得某些可疑的大小调整成为可能。

Not cool resizing

这是我的类(class):

    public DesktopLauncher() {

    this.setUndecorated(true);
    this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    this.setTitle("Evil Engine (v. Lambda)");

    Container container = this.getContentPane();
    this.canvas = new LwjglCanvas(new Main(DesktopLauncher.inst));

    // this.canvas.getCanvas().setLocation(200, -200);

    // height of the task bar
    int taskBarSize = Toolkit.getDefaultToolkit().getScreenInsets(this.getGraphicsConfiguration()).bottom;
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

    this.canvas.getCanvas().setSize(screenSize.width, screenSize.height - taskBarSize);

    container.add(this.canvas.getCanvas(), BorderLayout.CENTER);

    this.pack();

    this.setVisible(true);
}

当 setUndecorated 为 true 时就会出现问题。我尝试移动 Canvas (setLocation() 行),发现它周围有一个边框: Border around canvas

作为一个未装饰的窗口: Border around canvas-undecorated

我尝试了很多方法,但没有任何效果:

  • 我更改了布局类型
  • 我创建了一个新的 JPanel,将 Canvas 添加到其中并设置为 setContentPane()
  • 尝试过 setPreferedSize、setMaximumSize、setMinimumSize
  • 搜索 ...setBorder() 或 ..setResizable() 方法,但没有成功

我怎样才能摆脱 Canvas 周围这个可怕的边框?

最佳答案

你的桌面启动器应该是这样的:

public class DesktopLauncher {
    public static void main (String[] arg) {
        LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
        config.height =1024;
        config.width= 768;
        config.resizable = false;
        config.title= "My App Title";
        System.setProperty("org.lwjgl.opengl.Window.undecorated", "true"); 
        new LwjglApplication(new Game(), config);
    }
}

现在你有了一个无边界、不可调整大小的游戏窗口。

对于你的情况,如果你仍然想将它与 swing 一起使用,只需这样做:

public DesktopLauncher() {

    this.setUndecorated(true);
    this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    this.setTitle("Evil Engine (v. Lambda)");

    Container container = this.getContentPane();
    LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
    config.resizable = false;
    System.setProperty("org.lwjgl.opengl.Window.undecorated", "true"); 
    this.canvas = new LwjglCanvas(new Main(DesktopLauncher.inst),config);

    // this.canvas.getCanvas().setLocation(200, -200);

    // height of the task bar
    int taskBarSize = Toolkit.getDefaultToolkit().getScreenInsets(this.getGraphicsConfiguration()).bottom;
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

    this.canvas.getCanvas().setSize(screenSize.width, screenSize.height - taskBarSize);

    container.add(this.canvas.getCanvas(), BorderLayout.CENTER);

    this.pack();

    this.setVisible(true);
}

关于java - LwjglCanvas 与 JFrame 调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35831228/

相关文章:

java - 剥离的 JAR(方法没有字节码)是什么意思?

Java/SQL 查找重复项

java - JTable 删除行

java - 从 JSpinner 获取 TIME 值

c++ - 旋转对象 AABB 更新不正确

java - 如何从javascript调用servlet

java - 如何在 Hibernate 中的同一个表上建模 ManyToOne 和 ManyToMany 关系?

java - 将 JTextArea 的大小限制为其文本的大小

opengl - 为什么 golang gomobile basic example 为 vec4 属性设置 3-float 大小?

c++ - opengl 3d 点云从 x,y,z 二维数组渲染