Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6979663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:54:54+00:00 2026-05-27T17:54:54+00:00

I have a JFrame with a GLCanvas on. When I call the JFrame´s Component.printAll()

  • 0

I have a JFrame with a GLCanvas on. When I call the JFrame´s Component.printAll() method and then print the Graphics2D object to a png file (using BufferedImage and ImageIO.write()), then the printAll() method haven´t catched the GLCanvas. The JFrame is there on the png image but the GLCanvas is all gray.

What have I missed?

Sample code to reproduce the problem. Look at the generated png file named “image.png” after running the code. You will see everything except the GLCanvas. The code snippet that creates the image is at the bottom of the constructor.

import com.sun.opengl.util.Animator;
import java.awt.Graphics2D;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.*;
import javax.imageio.ImageIO;
import javax.media.opengl.*;
import javax.media.opengl.glu.GLU;
import javax.swing.JFrame;

public class JOGLTest implements GLEventListener {

    GLU glu = new GLU();
    JFrame frame = new JFrame("JOGL");
    GLCanvas canvas = new GLCanvas();    
    public static void main(String[] args) {
        JOGLTest joglTest = new JOGLTest();
    }

    public JOGLTest() {
        canvas.addGLEventListener(this);
        frame.add(canvas);
        frame.setSize(500, 500);
        final Animator animator = new Animator(canvas);
        frame.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                new Thread(new Runnable() {

                    public void run() {
                        animator.stop();
                        System.exit(0);
                    }
                }).start();
            }
        });

        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        animator.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException ex) {
            Logger.getLogger(JOGLTest.class.getName()).log(Level.SEVERE, null, ex);
        }

        BufferedImage image = new BufferedImage(frame.getWidth(), frame.getHeight(), BufferedImage.TYPE_INT_RGB);         
        Graphics2D g = image.createGraphics();        
        frame.printAll(g);        
        image.flush();

        try {
            ImageIO.write(image, "png", new File("image.png"));
        } catch (IOException ex) {
            Logger.getLogger(JOGLTest.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void init(GLAutoDrawable drawable) {
        GL gl = drawable.getGL();
        gl.setSwapInterval(1);
        gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        gl.glShadeModel(GL.GL_SMOOTH);
    }

    public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
        GL gl = drawable.getGL();
        GLU glu = new GLU();
        if (height <= 0) {        
            height = 1;
        }
        final float h = (float) width / (float) height;
        gl.glViewport(0, 0, width, height);
        gl.glMatrixMode(GL.GL_PROJECTION);
        gl.glLoadIdentity();
        glu.gluPerspective(20.0f, h, 1.0, 550.0);
        gl.glMatrixMode(GL.GL_MODELVIEW);
        gl.glLoadIdentity();
    }

    private int eyeX = 0;
    private int eyeY = 0;
    private int eyeZ = 130;
    private int centerX = 0;    

    public void display(GLAutoDrawable drawable) {
        GL gl = drawable.getGL();
        gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
        gl.glLoadIdentity();
        glu.gluLookAt(eyeX, eyeY, eyeZ, centerX, 0.0, 0.0, 0.0, 1.0, -0.0);
        gl.glBegin(GL.GL_LINES);
        gl.glVertex2d(1, 1);
        gl.glVertex2d(20, 20);
        gl.glEnd();
        gl.glFlush();
    }  
    public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T17:54:55+00:00Added an answer on May 27, 2026 at 5:54 pm

    Use GLJPanel instead of GLCanvas:

    GLJPanel panel = new GLJPanel();
    

    Then just replace your canvas variable with panel and it should work.
    GLCanvas sometimes causes compatibility problems when used inside swing components.

    From GLCanvas api page:

    GLJPanel is provided for compatibility with Swing user interfaces when adding a heavyweight doesn’t work either because of Z-ordering or LayoutManager problems.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JFrame in which I am using Graphics2D to draw a VolatileImage
Following situation: I have a JFrame and call JOptionPane.showInputDialog(test) . The modal dialog will
I am using swing to create my GUI. J have a JFrame containing one
I have a JFrame object, and i need to support two JVM 1.5 on
I have a JFrame inside of which is a jpanel that im using as
I have a JFrame and I have created an object to open it and
I have two JFrame Forms-SelectContactsfrm.java and Taskfrm.java. There is JTable in SelectContactsfrm file to
I have JFrame , and I added my JPanel class with paintComponent() method. For
I have a JFrame . I also have a Box class which extends Component
I have a JFrame with a menu bar and a canvas covering all the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.