gpt4 book ai didi

java - JOGL:使用 Component.printAll() 在 JFrame 中截取 GLCanvas 的屏幕截图不起作用

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:23:59 24 4
gpt4 key购买 nike

我有一个带有 GLCanvas 的 JFrame。当我调用 JFrame 的 Component.printAll() 方法然后将 Graphics2D 对象打印到 png 文件(使用 BufferedImage 和 ImageIO.write())时,然后printAll() 方法没有捕捉到 GLCanvas。 JFrame 位于 png 图像上,但 GLCanvas 是全灰色的。

我错过了什么?

重现问题的示例代码。查看运行代码后生成的名为“image.png”的png文件。您将看到除 GLCanvas 之外的所有内容。创建图像的代码片段位于构造函数的底部。

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

最佳答案

使用 GLJPanel 代替 GLCanvas:

GLJPanel panel = new GLJPanel();

然后只需将 canvas 变量替换为 panel 即可,它应该可以工作。GLCanvas 在 swing 组件中使用时有时会导致兼容性问题。

来自 GLCanvas api 页面:

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

关于java - JOGL:使用 Component.printAll() 在 JFrame 中截取 GLCanvas 的屏幕截图不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8586798/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com