【发布时间】:2017-08-07 01:51:49
【问题描述】:
我希望在 3D OpenGL 视图上绘制各种 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.
}
}
我的画布;
public class MyCanvas extends GLCanvas{
private static final long serialVersionUID = 1L;
public MyCanvas(){
super(new GLCapabilities(GLProfile.getDefault()));
}
}
绘画发生了,但没有显示在视图中。我尝试过覆盖 repaint()、paint(Graphics)、paintComponent(Graphics) 和 update()。有人说在“重量级”组件上绘画很复杂,我应该直接在组件中绘画或使用其他类型。我显然需要 GLCanvas 来显示 3D 渲染,同时它似乎没有提供绘制叠加层的工具。有人告诉我只需在 JFrame 的 glassPane 中进行绘图,但这似乎有点过头了,而且我被告知永远不要在 glassPane 周围玩耍,所以我不打算这样做。
我已经看到很多关于绘画调用顺序的主题,但我无法确定在覆盖这样或那样的方法时哪个是正确的,我什至不知道我是否应该或者应该覆盖哪个方法。是否有一种明显的方式让我错过了在其 GLCanvas 组件上显示我的简单 JPanel 绘画?
【问题讨论】:
标签: java swing opengl jpanel awt