Reputation: 170
So I am writing a game in Java, and I started out using the drawRect() method to represent players, enemies, and shots. Everything was great. Then I decided to try to get fancy. I found myself creating .png images of each object and used the Graphics2D drawImage() method. Everything began to slow down. Is there any alternative way to speed up the process?
My animation is based on a Swing Timer
public void paint(Graphics g){
super.paint(g);
Graphics2D g2d = (Graphics2D)g;
player1.paintShips(g);
g2d.drawImage(bGround, 14, 14, this);
try{
for(Shot s: liveRounds){ //liveRounds is an ArrayList of Shots
if(!inBounds.contains(s.getRect()) || villains.collision(s)){
if(villains.collision(s)){
villains.collided(s, this);
}
liveRounds.remove(s);
roundCount--;
}
else{
s.paintShot(g, this);
}
}
}catch(ConcurrentModificationException e){};
villains.paintEnemyGrid(g, this);
g2d.setColor(Color.cyan);
g2d.draw(hitZone1);
g2d.setColor(Color.red);
g.drawString("X: " + player1.getX(1) + " Y: " + player1.getY(1), 370, 150);
g2d.draw(inBounds);
g.drawString(score + "", 440, 40);
g.dispose();
}
Any tips or tutorials on animation? Thanks
Upvotes: 2
Views: 915
Reputation: 44808
A 10 ms delay is 100 frames per second. That is almost certainly too fast.
Also, if you want to remove an object from a Collection
while you are iterating over it, you need to do this:
Iterator<T> itr = collection.iterator();
while(itr.hasNext()) {
T obj = itr.next();
if(removeObj) {
itr.remove();
}
}
ConcurrentModificationException
s lead to non-deterministic behavior. You need to avoid them, not ignore them.
Upvotes: 1