Hello
I am very new to graphic and I have problem with speed animation.
I have simple „game” with one player, when I press key space player start shooting, unfortunately bullet animation start slow down. All bullets are holding in list and they are creating in separate thread which detecting pressed space. Bullet position also is updating in separated thread.
Animation start slowing even if in bullet list i have two objects.
Whay this happening and how to fix it?
Ok, here it is some part of my code
public class CanvasTest extends Canvas implements KeyListener {
public void init(){
setSize(500, 500);
setBackground(Color.BLACK);
addKeyListener(this);
setVisible(true);
BulletCreator bc = new BulletCreator(this);
Thread b = new Thread(bc);
b.start();
BulletMovement bm = new BulletMovement(this);
Thread tm = new Thread(bm);
tm.start();
}
public void paint(Graphics g){
for(Bullet bullet : getBullets()){
bullet.drow(g);
}
repaint();
}
@Override
/**
* Double buffering
*/
public void update(Graphics g) {
if(image == null) {
image = createImage(500, 500);
doubleGraphic = image.getGraphics();
}
doubleGraphic.setColor(getBackground());
doubleGraphic.fillRect(0, 0, 500, 500);
doubleGraphic.setColor(getForeground());
paint(doubleGraphic);
g.drawImage(image, 0, 0, this);
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
fire = true;
}
e.consume();
}
@Override
public void keyReleased(KeyEvent e) {
fire = false;
e.consume();
}
public boolean isFire() {
return fire;
}
public CopyOnWriteArrayList<Bullet> getBullets() {
return bullets;
}
...
}
public class BulletCreator implements Runnable {
private CanvasTest canvas;
public BulletCreator(CanvasTest canvas) {
this.canvas = canvas;
}
@Override
public void run() {
while (true) {
try {
if (canvas.isFire()) {
Bullet player1Bullet = new Bullet(10,45, canvas.getImage(".\\images\\bullet.gif"));
Thread.sleep(500);
canvas.getBullets().add(player1Bullet);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class BulletMovement implements Runnable {
private CanvasTest canvas;
public BulletMovement(CanvasTest canvas) {
this.canvas = canvas;
}
@Override
public void run() {
while (true) {
try {
if (canvas.getBullets().size() > 0) {
for (Bullet bullet : canvas.getBullets()) {
bullet.update(canvas);
Thread.sleep(10);
}
}
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class Bullet {
protected Image img;
public Rectangle rect;
private int xPos;
private int yPos;
public Bullet (final int xPos, final int yPos, final Image img){
setxPos(xPos);
setyPos(yPos);
this.rect = new Rectangle(getxPos(), getyPos(), 4, 4);
this.img = img;
}
public void drow (Graphics g){
g.drawImage(img, getxPos(), getyPos(), 4, 4, null);
}
public Image getImage(String img) {
return Toolkit.getDefaultToolkit().getImage(img);
}
public void update(CanvasTest canvas) {
if (getxPos() < 5 || getxPos() > Config.WIDTH-5) {
canvas.getBullets().remove(this);
this.rect = new Rectangle(getxPos(), getyPos(), 4,4);
} else {
setxPos(getxPos() + 1);
rect.x += 1;
}
}
public int getxPos() {
return xPos;
}
public void setxPos(int xPos) {
this.xPos = xPos;
}
public int getyPos() {
return yPos;
}
public void setyPos(int yPos) {
this.yPos = yPos;
}
}