在我的应用程序中,我使用 JOGL lib 来处理 OpenGL。
我需要序列化我的 javax.media.opengl.awt.GLCanvas 实现。
import javax.media.opengl.*;
import javax.media.opengl.awt.GLCanvas;
import javax.media.opengl.glu.GLU;
import java.io.Serializable;
public class Canvas extends GLCanvas implements GLEventListener, Serializable {
private GLU glu;
private static final int FPS = 60;
private FPSAnimator animator;
public Canvas() {
addGLEventListener(this);
}
@Override
public void init(GLAutoDrawable drawable) {
drawable.setGL(new DebugGL2(drawable.getGL().getGL2()));
glu = new GLU();
animator = new FPSAnimator(this, FPS);
animator.start();
}
@Override
public void display(GLAutoDrawable drawable) {
if (!animator.isAnimating()) return;
final GL2 gl = drawable.getGL().getGL2();
gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
setCamera(gl, glu, camera.getHeight(), camera.getOrigin());
}
@Override
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
final GL2 gl = drawable.getGL().getGL2();
gl.glViewport(0, 0, width, height);
}
@Override
public void dispose(GLAutoDrawable drawable) {
}
private void setCamera(GL2 gl, GLU glu, float distance, Float origin) {
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
float widthHeightRatio = (float) getWidth() / (float) getHeight();
glu.gluPerspective(45, widthHeightRatio, 1, 1000);
glu.gluLookAt(origin.x, origin.y, distance, origin.x, origin.y, 0, 0, 1, 0);
gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glLoadIdentity();
}
}
我尝试写信反对我的画布
fout = new FileOutputStream("F:\\canvas.ser");
oos = new ObjectOutputStream(fout);
oos.writeObject(mCanvas);
但我有错误:
java.io.NotSerializableException: com.jogamp.nativewindow.awt.AWTGraphicsConfiguration
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1509)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
at com.pms.efficiency.VideoEffectiveApplication.saveFileAs(VideoEffectiveApplication.java:328)
at com.pms.efficiency.VideoEffectiveApplication.lambda$initMenu$1(VideoEffectiveApplication.java:215)
at java.awt.MenuItem.processActionEvent(MenuItem.java:669)
at java.awt.MenuItem.processEvent(MenuItem.java:628)
at java.awt.MenuComponent.dispatchEventImpl(MenuComponent.java:351)
at java.awt.MenuComponent.dispatchEvent(MenuComponent.java:339)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:761)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
因此,GLCanvas 中的字段不可序列化。我能做些什么?