我正在尝试进行碰撞检测,并设法实际做到这一点然而它抛出一个java.lang.OutOfMemoryError:Java堆空间,我相信它是因为不断检查xPosition和yPosition的位置
public void fireBullet(Graphics g, int pow, int angle, int size) {
Rectangle bullet = new Rectangle(xPos - size/2, yPos - size/2, size, size);
g.setColor(new Color(200,255,255));
g.fillOval(bullet.x, bullet.y, bullet.width, bullet.height);
if (LevelType.EASY != null & bullet.intersects(Level1.getBoundsR())){
System.out.println("hi from level 1");
xPosition = 50;
yPosition = 350;
this.setFocusable(false);
new Something().setVisible(true);
}
并且在PaintComponent中的这个类之后我有
fireBullet(g, xPosition, yPosition, 10);
我还将VM参数设置为-Xms512M -Xmx2048M,并在任务管理器上观察我的内存运行速度有多快,我说它在30秒左右就消失了。
关注
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
at java.awt.image.DataBufferByte.<init>(Unknown Source)
at java.awt.image.ComponentSampleModel.createDataBuffer(Unknown Source)
at java.awt.image.Raster.createWritableRaster(Unknown Source)
at javax.imageio.ImageTypeSpecifier.createBufferedImage(Unknown Source)
at javax.imageio.ImageReader.getDestination(Unknown Source)
at com.sun.imageio.plugins.png.PNGImageReader.readImage(Unknown Source)
at com.sun.imageio.plugins.png.PNGImageReader.read(Unknown Source)
at javax.imageio.ImageIO.read(Unknown Source)
at javax.imageio.ImageIO.read(Unknown Source)
at cannonGame.GamePanel.paintComponent(GamePanel.java:99)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent._paintImmediately(Unknown Source)
at javax.swing.JComponent.paintImmediately(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$1100(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
加上我已经注释掉了碰撞检测代码,你们是对的,不是吗。
另一个关注 所以我已经检查了GamePanel.java:99中的内容,发现这个代码我并没有真正使用,它就在那里,我删除了它,现在一切都很顺利。
BufferedImage myPicture = null;
try {
myPicture = ImageIO.read(new File("resources/Level1Bk.jpg"));
} catch (IOException e) {
System.out.println(e);
e.printStackTrace();
}
JLabel picLabel = new JLabel(new ImageIcon(myPicture));
add(picLabel);
因为@Oleg Estekhin提到每张重画都会画一幅新照片,谢谢你