How to use java2D within jogl?

hi!
Is it possible to draw some java2D things in jogl offScreen context?
In Java3D API, I can implement postRender method of Canvas3D to paint additional things. Can I do this with jogl?

Thanks~

At the moment there is no compatibility between Java2D and JOGL so you need to think of them as being exclusive. We hope to bridge the gap between the APIs with the new OpenGL pipeline for Java2D. In the meantime you may be able to get some results using the GLJPanel or by using pbuffers and reading back the frame buffer by hand, though performance will probably not be as good as you want.

What we are doing is creating a texture out of a BufferedImage and rendering that in JOGL using a textured Quad. It does not look too bad, but it is still not as good as direct Java2D. I expect that in our case the pixel boundaries are not eactly lined up, so all the fancy anti-alising and hinting for Java2D fonts is not aligned with the actual display. The difference in performance however is huge. Even with the OpenGL pipeline for Java2D there are tradeoffs that Java2D has made for quality and device independence that will be hard to adapt to high performance needs. I look forward to being able to test the OpenGL pipeline when Apple released JDK 5.0.

For those interested in Java2D/JOGL integration,
here’s an update from Chris Campbell who’s
working on the OpenGL pipeline for Java2D:

http://weblogs.java.net/blog/campbell/archive/2005/03/strcrazy_improv_1.html

.rex

I got an idea, and it works on my pc. Maybe it is a stupid idea also…isn’t it? ‘cause it’s flickerring :’(


import net.java.games.jogl.*;

import javax.swing.*;
import java.awt.*;

import static net.java.games.jogl.GL.*;

public class Test extends JFrame implements GLEventListener {

  GLCanvas canvas;
  GL gl;
  GLU glu;

  public Test() throws HeadlessException {
    canvas = GLDrawableFactory.getFactory().createGLCanvas(new GLCapabilities());
    canvas.addGLEventListener(this);
    canvas.setAutoSwapBufferMode(false);
    gl = canvas.getGL();
    glu = canvas.getGLU();
    this.add(canvas);
  }

  public static void main(String[] args) {
    Test t = new Test();
    t.setSize(640, 480);
    t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    t.setLocationRelativeTo(null);
    t.setVisible(true);
  }

  public void init(GLDrawable glDrawable) {
    gl.glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
  }

  public void display(GLDrawable glDrawable) {
    gl.glClear(GL_COLOR_BUFFER_BIT);
    gl.glLoadIdentity();
    gl.glColor3f(1f, 0, 0);
    gl.glBegin(GL_TRIANGLES);
    gl.glVertex2f(0, 0);
    gl.glVertex2f(0.5f, 0);
    gl.glVertex2f(0.5f, 0.5f);
    gl.glEnd();
    canvas.swapBuffers();
    canvas.getGraphics().drawString("Hi jogl!", 100, 100);
  }

  public void reshape(GLDrawable glDrawable, int i, int i1, int w, int h) {
    gl.glViewport(0, 0, w, h);
    gl.glMatrixMode(GL_PROJECTION);
    gl.glLoadIdentity();
    glu.gluOrtho2D(0, 0, 1, 1);
    gl.glMatrixMode(GL_MODELVIEW);
  }

  public void displayChanged(GLDrawable glDrawable, boolean b, boolean b1) {
  }
}

Try removing ‘canvas.swapBuffers();’ in your
display() function. JOGL will swap buffers for
you automatically by default.

yes, by default it is. So I turn off autoSwapBuffer, then I can use java2D in onScreen context :wink: