/**
   * Paints all the objects.
   *
   * <p>Must be synchronized on the World.lock.
   */
  public void paintObjects(Graphics2D g) {
    Set<Actor> objects = WorldVisitor.getObjectsListInPaintOrder(world);
    int paintSeq = 0;
    for (Iterator<Actor> iter = objects.iterator(); iter.hasNext(); ) {
      Actor thing = iter.next();
      int cellSize = WorldVisitor.getCellSize(world);

      GreenfootImage image = ActorVisitor.getDisplayImage(thing);
      if (image != null) {
        ActorVisitor.setLastPaintSeqNum(thing, paintSeq++);

        double halfWidth = image.getWidth() / 2.;
        double halfHeight = image.getHeight() / 2.;

        AffineTransform oldTx = null;
        try {
          int ax = ActorVisitor.getX(thing);
          int ay = ActorVisitor.getY(thing);
          double xCenter = ax * cellSize + cellSize / 2.;
          int paintX = (int) Math.floor(xCenter - halfWidth);
          double yCenter = ay * cellSize + cellSize / 2.;
          int paintY = (int) Math.floor(yCenter - halfHeight);

          int rotation = ActorVisitor.getRotation(thing);
          if (rotation != 0) {
            // don't bother transforming if it is not rotated at
            // all.
            oldTx = g.getTransform();
            g.rotate(Math.toRadians(rotation), xCenter, yCenter);
          }

          ImageVisitor.drawImage(image, g, paintX, paintY, this, true);
        } catch (IllegalStateException e) {
          // We get this if the object has been removed from the
          // world. That can happen when interactively invoking a
          // method that removes an object from the world, while the
          // scenario is executing.
        }

        // Restore the old state of the graphics
        if (oldTx != null) {
          g.setTransform(oldTx);
        }
      }
    }
  }