java - 如何在 GLCanvas 上绘图?

标签 java swing opengl jpanel awt

我希望在 3D OpenGL View 上绘制某种 HUD,但似乎在我的面板中完成的任何绘制都会被忽略,尽管它已经完成了。

这是一些准系统代码。

我的框架;

public class MyFrame extends JFrame{
    private static final long serialVersionUID = 1L;
    public MyFrame(Labyrinth l){
        super();
        this.setTitle("My Frame");
        this.setSize(512, 384);
        this.setContentPane(new MyPanel());
        //this.setVisible(true);//If needed here.
    }
}

我的面板;

public class MyPanel extends JPanel {
    private static final long serialVersionUID = 1L;
    public MyPanel(){
        super();
        this.setLayout(new BorderLayout());
        MyCanvas mc=new MyCanvas(l);
        mc.setFocusable(false);
        this.add(this.mc, BorderLayout.CENTER);
        //this.revalidate();//Doesn't seem needed in the instanciation.
    }
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        this.mc.repaint();
        g.setColor(new Color(128,128,128));
        g.fillRect(0, 0, this.getWidth()/2,this.getHeight()/2); 
        //top-left quarter should be greyed away.
    }
}

我的 Canvas ;

public class MyCanvas extends GLCanvas{
    private static final long serialVersionUID = 1L;
    public MyCanvas(){
        super(new GLCapabilities(GLProfile.getDefault()));
    }
}

绘画发生,但未显示在 View 中。我尝试过重写 repaint()、paint(Graphics)、paintComponent(Graphics) 和 update()。有人说在“重量级”组件上绘制很复杂,我应该直接在组件中绘制或使用其他类型。我显然需要 GLCanvas 来显示 3D 渲染,同时它似乎没有提供绘制叠加层的工具。有人告诉我只需在 JFrame 的 glassPane 中进行绘图,但这似乎有点过分了,而且我被告知永远不要在 glassPane 周围玩耍,所以我不打算这样做。

我见过很多关于绘画调用顺序的主题,但我无法确定在重写这样或那样的方法时哪个是正确的,而且我什至不知道是否应该重写或应该重写哪个方法。有没有一种明显的方法我会错过让我的简单 JPanel 绘画显示在其 GLCanvas 组件上?

最佳答案

首先,我真的不建议通过这些方式获得HUD。因为我只能想象这种伤害性的表演。当然,我从未尝试过像这样混合 Java、OpenGL 和 AWT 的图形。

现在,不要使用用胶带将这些类固定在一起,而是考虑使用 JLayeredPane

JLayeredPane layeredPane = new JLayeredPane();

layeredPane.add(new MyCanvas());
layeredPane.add(new MyPanel());

frame.add(layeredPane);

现在重要的部分是您必须手动设置两个组件的边界:

canvas.setBounds(x, y, width, height);
panel.setBounds(x, y, width, height);

如果不这样做,您最终会遇到与以前相同的问题:

The painting takes place, but isn't shown in the view

为了演示它的工作原理,我创建了这个类似于 MyPanel 的小 TestPanel 类。

public static class TestPanel extends JPanel {
    private Color color;

    public TestPanel(Color color) {
        this.color = color;
    }

    @Override
    public void paintComponent(Graphics g) {
        g.setColor(color);
        g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
    }
}

然后创建两个实例,如下所示:

JPanel panel1 = new TestPanel(new Color(255, 0, 0));
panel1.setBounds(25, 25, 100, 100);

JPanel panel2 = new TestPanel(new Color(0, 0, 255));
panel2.setBounds(75, 75, 100, 100);

然后将它们添加到 JLayeredPane 并将其添加到 JFrame 中,我们会看到:

关于java - 如何在 GLCanvas 上绘图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42824765/

相关文章:

java - sql查询平均,按毫秒的日期部分分组后

java - Android studio 预期的类或包

java - 保存选定的 JTable 行并在另一个 JTable 中显示它们(在另一个 JFrame 中)

java - 鼠标移动-十字准线光标

c++ - FBO 在 OSX 中无法正常工作

c++ - 更改子窗口大小 opengl

java - 时间问题..以及如何在java中操作它

java - 如何从文件中的原始 xml 负载中解码 SoapFault?

java - 是否可以在 JFrame 中包含 "movable"/"draggable"组件,如 JButtons、JTextFields?

performance - 如何暂时禁用 OpenGL 命令队列以获得更准确的分析结果?