我写的最简单的jogl代码不显示真实的视图?

发布于 2024-11-18 15:17:32 字数 1656 浏览 2 评论 0原文

我尝试编写一个在蓝色框架中显示矩形的代码:

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦萦几度 2024-11-25 15:17:32

试试这个方法:

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;
import java.awt.BorderLayout;

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(new BorderLayout()); // 1.              <- HERE
        jp.setSize(jframe.getSize());
        jframe.add(jp);
        // jp.setBackground(Color.BLUE); // 2. rather do that in JOGL
        jp.add(convas);
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        animator.start();

    }

    public Jojl() {
        super(new GLCapabilities());
        addGLEventListener(this);  // 2.                              <-- HERE
    }

    @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) {}
}

注意评论。您只错过了两个元素:

  • 将 GLEventListener 添加到 GLCanvas。通常情况下,您不需要扩展 GLCanvas,实现 GLEventlistener 就是获取 GL 所需的全部内容。

  • JPanel 的 LayoutManager 未指定,因此未注意全面布局画布

Try this way:

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;
import java.awt.BorderLayout;

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(new BorderLayout()); // 1.              <- HERE
        jp.setSize(jframe.getSize());
        jframe.add(jp);
        // jp.setBackground(Color.BLUE); // 2. rather do that in JOGL
        jp.add(convas);
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        animator.start();

    }

    public Jojl() {
        super(new GLCapabilities());
        addGLEventListener(this);  // 2.                              <-- HERE
    }

    @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) {}
}

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文