我写的最简单的jogl代码不显示真实的视图?
我尝试编写一个在蓝色框架中显示矩形的代码:
import java.awt.Color;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import com.sun.opengl.util.Animator;
public class Jojl extends GLCanvas implements GLEventListener {
static final long serialVersionUID = 4262202874615600756L;
public static void main(String[] args) {
Jojl convas=new Jojl();
Animator animator=new Animator(convas);
JFrame jframe=new JFrame("hello JOGL");
jframe.setVisible(true);
jframe.setSize(500,500);
jframe.setVisible(true);
jframe.setLayout(null);
JPanel jp=new JPanel();
jp.setSize(jframe.getSize());
jframe.add(jp);
jp.setBackground(Color.BLUE);
jp.add(convas);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
animator.start();
}
public Jojl()
{
super( new GLCapabilities());
}
@Override
public void display(GLAutoDrawable arg0) {
System.out.println("hello");
GL gl=arg0.getGL();
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glRectf(-0.5f,-0.5f,0.5f,0.5f);
}
@Override
public void displayChanged(GLAutoDrawable arg0, boolean arg1, boolean arg2) {
}
@Override
public void init(GLAutoDrawable arg0) {
}
@Override
public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3,
int arg4) {
// TODO Auto-generated method stub
}
}
但这仅显示蓝色框架而不显示矩形。
i try to write a code that show a rect in a blue frame:
import java.awt.Color;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import com.sun.opengl.util.Animator;
public class Jojl extends GLCanvas implements GLEventListener {
static final long serialVersionUID = 4262202874615600756L;
public static void main(String[] args) {
Jojl convas=new Jojl();
Animator animator=new Animator(convas);
JFrame jframe=new JFrame("hello JOGL");
jframe.setVisible(true);
jframe.setSize(500,500);
jframe.setVisible(true);
jframe.setLayout(null);
JPanel jp=new JPanel();
jp.setSize(jframe.getSize());
jframe.add(jp);
jp.setBackground(Color.BLUE);
jp.add(convas);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
animator.start();
}
public Jojl()
{
super( new GLCapabilities());
}
@Override
public void display(GLAutoDrawable arg0) {
System.out.println("hello");
GL gl=arg0.getGL();
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glRectf(-0.5f,-0.5f,0.5f,0.5f);
}
@Override
public void displayChanged(GLAutoDrawable arg0, boolean arg1, boolean arg2) {
}
@Override
public void init(GLAutoDrawable arg0) {
}
@Override
public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3,
int arg4) {
// TODO Auto-generated method stub
}
}
but this show only a blue frame and not show rectangle.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个方法:
注意评论。您只错过了两个元素:
将 GLEventListener 添加到 GLCanvas。通常情况下,您不需要扩展 GLCanvas,实现 GLEventlistener 就是获取 GL 所需的全部内容。
JPanel 的 LayoutManager 未指定,因此未注意全面布局画布
Try this way:
Note the comments. You missed only two elements:
Adding GLEventListener to GLCanvas. Normally You don't extend GLCanvas, implementing GLEventlistener is all that is required to get GL.
JPanel's LayoutManager was unspecified, so no care was taken to layout the canvas at full extent