Hello,everyone! I meet a problem,could you help me,please.
I want to create a multiple panels application,and I refer to TestAwtPanels and some other code at the forum.Follows are my codes. It report an error. I don’t konw how to resolve it.
严重: Uncaught exception thrown in Thread[jME3 Main,5,main]
java.lang.IllegalArgumentException: Number of remaining buffer elements is 480000, must be at least 1228800. Because at most 1228800 elements can be returned, a buffer with at least 1228800 elements is required, regardless of actual returned element count
at org.lwjgl.BufferChecks.throwBufferSizeException(BufferChecks.java:162)
at org.lwjgl.BufferChecks.checkBufferSize(BufferChecks.java:189)
at org.lwjgl.BufferChecks.checkBuffer(BufferChecks.java:230)
at org.lwjgl.opengl.GL11.glReadPixels(GL11.java:2450)
at com.jme3.renderer.lwjgl.LwjglGL.glReadPixels(LwjglGL.java:308)
at com.jme3.renderer.opengl.GLRenderer.readFrameBufferWithGLFormat(GLRenderer.java:1839)
at com.jme3.renderer.opengl.GLRenderer.readFrameBuffer(GLRenderer.java:1817)
at com.jme3.system.awt.AwtPanel.drawFrameInThread(AwtPanel.java:170)
at com.jme3.system.awt.AwtPanel.onFrameEnd(AwtPanel.java:308)
at com.jme3.system.awt.AwtPanelsContext.updateInThread(AwtPanelsContext.java:203)
at com.jme3.system.awt.AwtPanelsContext.access$100(AwtPanelsContext.java:45)
at com.jme3.system.awt.AwtPanelsContext$AwtPanelsListener.update(AwtPanelsContext.java:69)
at com.jme3.system.lwjgl.LwjglOffscreenBuffer.runLoop(LwjglOffscreenBuffer.java:125)
at com.jme3.system.lwjgl.LwjglOffscreenBuffer.run(LwjglOffscreenBuffer.java:156)
at java.base/java.lang.Thread.run(Thread.java:834)
package appState;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.concurrent.CountDownLatch;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import com.jme3.app.SimpleApplication;
import com.jme3.asset.plugins.FileLocator;
import com.jme3.input.FlyByCamera;
import com.jme3.light.AmbientLight;
import com.jme3.light.DirectionalLight;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Node;
import com.jme3.system.AppSettings;
import com.jme3.system.awt.AwtPanel;
import com.jme3.system.awt.AwtPanelsContext;
import com.jme3.system.awt.PaintMode;
/**
* @author zhangchao
*
*/
public class XplaneAirport2 extends SimpleApplication {
final private static CountDownLatch panelsAreReady = new CountDownLatch(1);
private static XplaneAirport2 app;
private static AwtPanel panel1, panel2;
private static int panelsClosed = 0;
private static AwtPanelsContext ctx;
@Override
public void simpleInitApp() {
//create nodes
Node Scene=new Node("Scene");
Node Terrain=new Node("Terrain");
Node Model=new Node("Model");
Scene.attachChild(Model);
Scene.attachChild(Terrain);
rootNode.attachChild(Scene);
//camera
cam.setLocation(new Vector3f(0f,15f,0f));
flyCam.setEnabled(false);
AmbientLight ambient = new AmbientLight();
ambient.setColor(new ColorRGBA(0.7f, 0.7f, 0.7f, 1f));
DirectionalLight sun = new DirectionalLight();
sun.setDirection(new Vector3f(-1, -2, -3).normalizeLocal());
rootNode.addLight(ambient);
rootNode.addLight(sun);
/*
* Wait until both AWT panels are ready.
*/
try {
panelsAreReady.await();
} catch (InterruptedException exception) {
throw new RuntimeException("Interrupted while waiting for panels", exception);
}
Camera cam1 =cam.clone();
final FlyByCamera flyCam1 = new FlyByCamera(cam1);
flyCam1.setMoveSpeed(10);
flyCam1.setDragToRotate(true);
flyCam1.setEnabled(false);
ViewPort vp1 = renderManager.createMainView("view1", cam1);
vp1.setClearFlags(true, true, true);
vp1.attachScene(rootNode);
Camera cam2 =cam.clone();
final FlyByCamera flyCam2 = new FlyByCamera(cam2);
flyCam2.setMoveSpeed(10);
flyCam2.setDragToRotate(true);
flyCam2.setEnabled(false);
ViewPort vp2 = renderManager.createMainView("view2", cam2);
vp2.setClearFlags(true, true, true);
vp2.attachScene(rootNode);
panel1.attachTo(true, vp1);
panel2.attachTo(false, vp2);
panel1.addFocusListener(new AwtPanelFocus(panel1, flyCam1));
panel2.addFocusListener(new AwtPanelFocus(panel2, flyCam2));
}
public static void main(String[] args) {
app = new XplaneAirport2();
app.setShowSettings(false);
AppSettings settings = new AppSettings(true);
settings.setCustomRenderer(AwtPanelsContext.class);
// settings.setFrameRate(60);
app.setSettings(settings);
app.start();
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run()
{
/*
* Sleep 2 seconds to ensure there's no race condition.
* The sleep is not required for correctness.
*/
try {
Thread.sleep(2000);
} catch (InterruptedException exception) {
return;
}
ctx = (AwtPanelsContext) app.getContext();
panel1 = ctx.createPanel(PaintMode.Accelerated);
panel1.setPreferredSize(new Dimension(400, 300));
panel2 = ctx.createPanel(PaintMode.Accelerated);
panel2.setPreferredSize(new Dimension(400, 300));
ctx.setInputSource(panel1);
createWindowForPanel(panel1, 100);
createWindowForPanel(panel2, 500);
/*
* Both panels are ready.
*/
panelsAreReady.countDown();
}
});
}
private static void createWindowForPanel(final AwtPanel panel, final int location)
{
JFrame frame = new JFrame("Render Display " + location);
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(panel, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosed(WindowEvent e) {
if (++panelsClosed == 2){
app.stop();
}
}
@Override
public void windowActivated(WindowEvent e) {
//设置当前panel为输入源
ctx.setInputSource(panel);
//当前panel请求获取焦点
panel.requestFocusInWindow();
}
});
frame.pack();
//设置窗口位置(左上--x,y)
frame.setLocation(location, 100);
frame.setVisible(true);
}
}
class AwtPanelFocus implements FocusListener
{
AwtPanel panel;
FlyByCamera flyCam;
public AwtPanelFocus(AwtPanel panel, FlyByCamera flyCam)
{
this.panel = panel;
this.flyCam = flyCam;
}
@Override
public void focusGained(FocusEvent e)
{
flyCam.setEnabled(true);
}
@Override
public void focusLost(FocusEvent e)
{
flyCam.setEnabled(false);
}
}