问题
I am developing a simple java game and my scrollbar is raising an exception. The scrollbar is supposed to be a "gameslider".
Exception in thread "Thread-3" java.lang.ClassCastException: sun.java2d.NullSurfaceData cannot be cast to sun.java2d.d3d.D3DSurfaceData at sun.java2d.d3d.D3DRenderer.copyArea(Unknown Source) at sun.java2d.d3d.D3DSurfaceData.copyArea(Unknown Source) at sun.java2d.SunGraphics2D.doCopyArea(Unknown Source) at sun.java2d.SunGraphics2D.copyArea(Unknown Source) at javax.swing.BufferStrategyPaintManager.copyArea(Unknown Source) at javax.swing.RepaintManager.copyArea(Unknown Source) at javax.swing.JViewport.blitDoubleBuffered(Unknown Source) at javax.swing.JViewport.windowBlitPaint(Unknown Source) at javax.swing.JViewport.setViewPosition(Unknown Source) at javax.swing.plaf.basic.BasicScrollPaneUI$Handler.hsbStateChanged(Unknown Source) at javax.swing.plaf.basic.BasicScrollPaneUI$Handler.stateChanged(Unknown Source) at javax.swing.DefaultBoundedRangeModel.fireStateChanged(Unknown Source) at javax.swing.DefaultBoundedRangeModel.setRangeProperties(Unknown Source) at javax.swing.DefaultBoundedRangeModel.setValue(Unknown Source) at javax.swing.JScrollBar.setValue(Unknown Source) at GameEnvironment.GamePanel.gameUpdate(GamePanel.java:171) at GameEnvironment.GamePanel.run(GamePanel.java:73) at java.lang.Thread.run(Unknown Source)
Code Line is following:
if (reachedMiddle)
scrollPane.getHorizontalScrollBar().setValue(player.getX() - (widthHorizontalScrollBar / 2));
The exception will be raised more or less randomly. I didnt figured it out so far.
回答1:
I would make sure this code is run on the GUI Event Thread. If you call the Swing components on other threads you can get strange, randonm errors.
Have a look at using SwingUtilities.invokeLater(Runnable) to perform the tasks where the Swing components are updated.
来源:https://stackoverflow.com/questions/10177943/java-game-classcastexception