Graphics2D clipping error

well first off, I don’t think Image’s are accelerated period. If you’re asking about the .get() and the type cast, I don’t think any optimization happens there. Even Generics which make it look better don’t even optimize (my biggest peeve) it. What I do with my stuff is something like this:


private static final int
      Actor_001_d_1 = 0,
      Actor_002_d_1 = 1,
      Actor_003_d_1 = 2,
      Actor_004_d_1 = 3;
public void loadImages() {
      images = new BufferedImage[4];
      for (int i = 0; i < images.length; i++)
            images[i] = getResource("Actor_00" + (i+1) + "_d_1.png");
}
public void render(Graphics2D g2d) {
      g2d.drawImage(images[index], x, y, null);
}

kinda like that :slight_smile:
so that I can load them by ints from an array, but still have a uniqueness about them that’s easy for me to read.

EDIT: forgot a curly brace :wink: Coding in here is tough!

i would perhaps try clipping the sprite graphic when you draw it rather than clipping the terrain. then you could use just one large buffer for the entire background / foreground. the tricky part might be making the formula that generates a polygon to use as the clip bounds.

i’m not sure how this would affect performance but i think calculating a polygon that represents the area that is in front of the player sprite and clipping just the sprite(s) when you draw it (or them) should be faster as its a much smaller image you’re clipping, and the anti-aliasing shouldn’t be as much of a problem, and in fact might look nice. as they say, logic is 1000 times cheaper than rendering in games, so adding complexity to the logic in order to reduce the amount of rendering done almost always pays off in performance.

i’m just throwing ideas around now, and i’ll probably even test a couple when i get your source, but one way to do it would be to calculate a polygon for each layer and clip the sprite based on a combination of all layers in front of it when you draw it.

Just to clarify, I meant BufferedImage not Image. :wink:

So I went through and changed it over to using a BufferedImage[], and there wasn’t any change between the two as far as the performance… I was rather hoping that this would speed everthing up… Thanks though!

oh yeah the difference definitely isn’t going to be significant. Accessor methods from your list was never your bottleneck obviously, it’s the rendering that’s your bottleneck.

Its kind of funny, I used something like this:


HashMap array_keys = new HashMap()
BufferedImage[] images = new BufferedImage[1000];
int current_index = 0;

public void addImage(String filename, String sck) {
   array_keys.put(filename, new String(""+current_index));
   images[current_index] = //IMAGE FROM RESOURCES
   current_index++;
}

public BufferedImage getImage(String sck) {
   return images[Integer.parseInt(array_keys.get(sck))];
}

This way I would have the speed of taking it from a BufferedImage[], and also the ability to have better naming…

But my hope of putting it into a BufferedImage[] was that it wasn’t getting accelerated in the HashMap…

Objects that have references to BufferedImages don’t affect how it gets accelerated. Having it stored in an array or Hashtable don’t make a bit of difference in that department. Am I understanding you correctly? I don’t think I know what it is you’re asking now.

Yeah, thats what I figured out. I was hoping that storing the reference in a hashtable was causing the BufferedImages to not be accelerated…

But any way I think I can say that you for all your guys’s input! This thread keeps going on and on, and I don’t see any point to continue it. If I have more questions I’ll post in a new thread.

Thanks!