【问题标题】:JOGL transparent backgroundJOGL透明背景
【发布时间】:2014-03-07 09:34:16
【问题描述】:

我需要让我的 GLCanvas 具有透明背景。我想要收到的效果是 JFrame 背景中不会有黑色背景而是灰色。我用 能力.setBackgroundOpaque(假); 和 gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 但它不起作用。我知道有同样的问题 (Jogl: How to make a transparent background on com.jogamp.opengl.swt.GLCanvas?),但我不明白 HUD 是如何工作的。此外,我只是不明白为什么metod capabilities.setBackgroundOpaque(false);不工作。

这是我的代码

public class JOGL implements GLEventListener {

private static final int FPS = 30;

@Override
public void display(GLAutoDrawable gLDrawable) {

    GL2 gl = gLDrawable.getGL().getGL2();

    // Clear the drawing area
    gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
    // Reset the current matrix to the "identity"
    gl.glLoadIdentity();

    // Move the "drawing cursor" around
    gl.glTranslatef(-1.5f, 0.0f, -6.0f);

    // Drawing Using Triangles
    gl.glBegin(GL.GL_TRIANGLES);
    gl.glColor3f(1.0f, 0.0f, 0.0f);    // Set the current drawing color to red
    gl.glVertex3f(0.0f, 1.0f, 0.0f);   // Top
    gl.glColor3f(0.0f, 1.0f, 0.0f);    // Set the current drawing color to green
    gl.glVertex3f(-1.0f, -1.0f, 0.0f); // Bottom Left
    gl.glColor3f(0.0f, 0.0f, 1.0f);    // Set the current drawing color to blue
    gl.glVertex3f(1.0f, -1.0f, 0.0f);  // Bottom Right
    // Finished Drawing The Triangle
    gl.glEnd();

    gl.glFlush();

}

@Override
public void init(GLAutoDrawable glDrawable) {
    GL2 gl = glDrawable.getGL().getGL2();
    gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
}

@Override
public void reshape(GLAutoDrawable gLDrawable, int x, int y, int width, int height) {

}

@Override
public void dispose(GLAutoDrawable gLDrawable) {
}

public static void main(String[] args) {

    GLCapabilities capabilities = new GLCapabilities(null);
    capabilities.setBackgroundOpaque(false);
    final GLCanvas canvas = new GLCanvas(capabilities);
    final JFrame frame = new JFrame("JOGL - test obj loading");
    frame.setLayout(null);
    final FPSAnimator animator = new FPSAnimator(canvas, FPS);
    canvas.addGLEventListener(new JOGL());

    JPanel p = new JPanel();

    frame.add(canvas);
    frame.add(p);
    p.setBackground(Color.red);
    canvas.setBounds(0, 0, 1024, 768);
    p.setBounds(0, 0, 100, 500);
    frame.setSize(1040, 900);
    frame.addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent e) {
            animator.stop();
            frame.dispose();
            System.exit(0);
        }
    });
    frame.setVisible(true);
    animator.start();
    canvas.requestFocus();

}

}

【问题讨论】:

    标签: java background transparency jogl


    【解决方案1】:

    宁可使用 GLJPanel,将其放入 JInternalFrame 中,同时在两者上调用 setOpaque(false)。我是在一个基于 JOGL 的 Eclipse RCP 应用程序中完成的,目的是创建一个透明的“窗口”,我可以将它移到 GUI 的其余部分之上。

    P.S:JOGL 本身已经有 2 个 WaveFront OBJ 加载器,我的(最强大的)在 JogAmp's Ardor3D Continuation

    【讨论】:

      【解决方案2】:

      我是 jogl 的新手,在使用 jogl 时遇到了类似的问题,但我终于找到了一个解决方案。 使用以下代码创建一个具有透明背景的 GLJPanel 对象。

      //Indeed, use GLJPanel instead of GLCanvas
      GLProfile glp = GLProfile.getDefault();
      GLCapabilities glcap = new GLCapabilities(glp);
      glcap.setAlphaBits(8);
      GLJPanel pane = new GLJpanel(glcap);
      pane.setOpaque(false);
      

      【讨论】:

      • 这篇文章被自动标记为低质量,因为它太短/只有代码。您介意通过添加一些文本来解释它如何解决问题来扩展它吗?
      • 其实几乎没有什么新鲜事,你只是听从了我的建议:stackoverflow.com/a/22280648/458157
      猜你喜欢
      • 1970-01-01
      • 2011-03-08
      • 2021-09-20
      • 2019-02-07
      • 2020-10-15
      • 2017-06-22
      • 2010-10-20
      • 2011-02-01
      • 2013-12-10
      相关资源
      最近更新 更多