# Low FPS on Java drawing in BufferStrategy

I ran into a very strange problem while using BufferStrategy, I've tried searching for an answer but all I have found are non-relevant threads and discussions. So here is my query.

for (int i = 0; i < 10; i++){
for (int x = 0; x < 10; x++){
g.drawImage(enlImg(getImg(terrain, x), 4), i*40, x*40, null);
}
}


(The parameter 'terrain' is a BufferedImage pre-loaded, which will be used to get a sub image from it.)

I'm trying to draw images to a Canvas, where 'g' is the buffer strategies, 'getDrawGraphics()' because that's how I am drawing to the BufferStrategy. Anyway, straight to the point. I'm only getting ~3fps.

I know this is only drawing 100 images, but other games draw hundreds of thousands with no lag... Any ideas?

Is it maybe because of trying to enlarge, draw and get a sub image all at once, or is it because of my computer?

Extra

enlImg() code

public BufferedImage enlImg(BufferedImage image, int x){
BufferedImage enlargedImage = new BufferedImage(image.getWidth() * x, image.getHeight() * x, image.getType());
for (int a = 0; a < enlargedImage.getHeight(); a++) {
for (int b = 0; b < enlargedImage.getWidth(); b++) {
enlargedImage.setRGB(a, b, image.getRGB(a / x, b / x));
}
}
return enlargedImage;
}


getImg() code

public BufferedImage getImg(BufferedImage img, int x){
int y = 0;
if (x >= 10){
x=x-10;
y++;
}
return img.getSubimage(x*10, y*10, 10, 10);
}


Computer Specifications (Windows):

AMD Athlon II X2 250 - 3.00GHz
OS: 32bit
4GB RAM,
256GB HHD

In enlImg, you're creating a new image every frame and copy 100*(width*x * height*x) pixels. Instead, you should generate the scaled images only once and reuse them every frame.

Basically, you'd make a list or array (or whatever data structure fits your needs) of BufferedImages:

LinkedList<BufferedImage> enlargedImages;


This should be a member function of the class you're using for drawing.

Then, you generate the enlarged images once in a function which is not the rendering function:

for (int i = 0; i < 10; i++){
for (int x = 0; x < 10; x++){
}
}


Then, terrain can be rendered in your paint function without regenerating the images:

for (int i = 0; i < 10; i++){
for (int x = 0; x < 10; x++){
g.drawImage(enlargedImages.get(x), i*40, x*40, null);
}
}

• So create a BufferedImage array and store loaded images there? – KeirDavis Apr 14 '13 at 16:35
• @KeirDavis What you would need to do is swap the inner loop with the outer loop, and then only use enlImage 10 times. Either that, or enlarge the entire of terrain once and get sub images from there. – Polar Apr 14 '13 at 18:29
• Could you give me an example. That makes no sense to me. (AKA, which loops are you on about and use EnlargeImage 10 times?) – KeirDavis Apr 14 '13 at 20:52
• I expanded the answer to clarify. – Exilyth Apr 14 '13 at 21:10
• Thankyou :D, I would upvote, but I don't have a high enough reputation... – KeirDavis Apr 15 '13 at 10:22