bullet java,Synchronization when removing and object outside of JPanel

I'm trying to remove objects that are outside of the JPanel. However, when I do that I get this error

d917ba167597e61fb0dd1ef486e3a2a8.png

and my program crashes. I was told by my lecturer that it's because two Threads are accessing the ArrayList that stores my objects.

I did to synchronize the functions but it didn't work.

Bullet

public void move(){

if(y< -height){

synchronized (this) {

bullet.remove(this);

}

}

y-=5;

}

Relevant Classes:

Application

import javax.swing.*;

public class Application {

public static String path ="C:\Users\jarek\OneDrive\NUIG Private\(2) Semester 2 2019\Next Generation Technologies II CT255\Assignment 3\";

private Application(){

JFrame frame = new JFrame("Ihsan The Defender");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

GamePanel gamePanel= new GamePanel();

frame.add(gamePanel);

frame.pack();

frame.setLocationRelativeTo(null);

frame.setResizable(false);

frame.setVisible(true);

new Thread(gamePanel).start();

}

public static void main (String args[]){

new Application();

}

}

GamePanel

import javax.swing.*;

import java.awt.*;

import java.awt.event.KeyEvent;

import java.awt.event.KeyListener;

import java.security.Key;

import java.util.ArrayList;

public class GamePanel extends JPanel implements Runnable, KeyListener{

private String path = Application.path;

Image gameOverImg = new ImageIcon(path+"//images//gameover1.png").getImage();

private Ihsan ihsan;

private ArrayList david = new ArrayList<>();

private int enemies=5;

private boolean pause=false;

private boolean gameOver=false;

GamePanel(){

ihsan = new Ihsan(this);

for(int i=0; i

david.add(new David(this));

}

setFocusable(true);

requestFocusInWindow();

addKeyListener(this);

}

@Override

public void run() {

while (!pause){

repaint();

for(David david:david){

david.move();

}

for(Bullet bullet:Bullet.bullet){

bullet.move();

}

try{Thread.sleep(30);}

catch (InterruptedException e){}

}

}

public void paint(Graphics g){

Graphics2D g2d = (Graphics2D) g.create();

g2d.setColor(Color.GRAY);

g2d.fillRect(0,0 ,getWidth(), getHeight());

for(David david : david){

g2d.drawImage(david.getImg(), david.getX(), david.getY(), null);

}

g2d.drawImage(ihsan.getImg(), ihsan.getX(), ihsan.getY(), null);

for (Bullet bullet:Bullet.bullet){

g2d.drawImage(bullet.getImg(), bullet.getX(), bullet.getY(), null);

}

if(gameOver){

g2d.drawImage(gameOverImg,0,getHeight()/4,null);

}

}

private static final Dimension DESIRED_SIZE = new Dimension(600,700);

@Override

public Dimension getPreferredSize(){

return DESIRED_SIZE;

}

public void setGameOver(boolean gameOver) {

this.gameOver = gameOver;

}

@Override

public void keyPressed(KeyEvent e) {

int key=e.getKeyCode();

if (key==KeyEvent.VK_D || key==KeyEvent.VK_RIGHT){

ihsan.move(4,0);

System.out.println("Right Key");

}

if (key==KeyEvent.VK_A || key== KeyEvent.VK_LEFT){

ihsan.move(-4,0);

System.out.println("Left Key");

}

if(key==KeyEvent.VK_SPACE){

Bullet.bullet.add(new Bullet(this,ihsan.getX()+(ihsan.getWidth()/2), ihsan.getY()));

}

}

@Override

public void keyTyped(KeyEvent e) { }

@Override

public void keyReleased(KeyEvent e) { }

public boolean getGameOver(){

return gameOver;

}

}

Bullet

import javax.swing.*;

import java.awt.*;

import java.util.ArrayList;

public class Bullet {

//Environment

public static ArrayList bullet = new ArrayList<>();

private String path = Application.path;

private GamePanel gp;

//properties

private int x,y;

private int width,height;

private int yVector;

private Image image;

Bullet(GamePanel gp, int x, int y){

image = new ImageIcon(path+"\images\javaicon.png").getImage();

width=image.getWidth(null);

height=image.getHeight(null);

this.gp=gp;

this.x=x;

this.y=y;

yVector=5;

}

public void move(){

if(y< -height){

bullet.remove(this);

}

y-=5;

}

public Image getImg(){

return image;

}

public int getX(){

return x;

}

public int getY(){

return y;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值