Java Game - ClassCastException

Deadly 提交于 2019-12-11 08:22:57

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!