2

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

assylias
  • 321,522
  • 82
  • 660
  • 783
user1320716
  • 170
  • 6
  • What's the delay on your timer? Why are you catching `ConcurrentModificationException`s and ignoring them?! If you want to remove an object from a `Collection` while you are iterating over it, you should manually use its `Iterator` in a while loop and use `Iterator#remove`. – Jeffrey Apr 08 '12 at 20:04
  • I have it set to 10, would that be too fast? – user1320716 Apr 08 '12 at 20:06
  • 2
    A 10 ms delay is 100 frames per second. That is almost certainly too fast. – Jeffrey Apr 08 '12 at 20:07
  • gotcha, thanks! Looks like I've got a bit of cleanup to do – user1320716 Apr 08 '12 at 20:12
  • Also if possible try to repaint() http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/Component.html#repaint(int, int, int, int) a specific area rather than the complete panel. – Nitin Chhajer Apr 08 '12 at 20:12
  • 1) `public void paint(Graphics g){` Wrong for a `JPanel`. It should be `public void paintComponent(Graphics g){` 2) For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Apr 08 '12 at 20:12
  • @user1320716 I changed that comment into an answer so you can accept it. – Jeffrey Apr 08 '12 at 20:13
  • 3) The next line after the overloaded method declaration also needs to be changed. 4) Don't dispose of a `Graphics` object passed to `paint()`/`paintComponent()` – Andrew Thompson Apr 08 '12 at 20:17

1 Answers1

1

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();
    }
}

ConcurrentModificationExceptions lead to non-deterministic behavior. You need to avoid them, not ignore them.

Jeffrey
  • 44,417
  • 8
  • 90
  • 141