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