fps drops after brightening tile images

Hi when running at no limit i get 100 fps
when i run this code for every tile on my map (30x30) the fps drops to 8.
Is there a better way to brighten/shade Bufferedimages?

    /* Draw the image with changed brightness, by using a RescaleOp.
    Any alpha channel is unaffected. */
  public void drawBrighterImage(Graphics2D g2d, BufferedImage im,  int x, int y, float brightness) {
    if (im == null) {
      System.out.println("drawBrighterImage: input image is null");
      return;
    }

    if (brightness < 0.0f) {
      System.out.println("Brightness must be >= 0.0f; setting to 0.5f");
      brightness = 0.5f;
    }
    // brightness may be less than 1.0 to make the image dimmer

    RescaleOp brigherOp;
    if (hasAlpha(im)) {
      float[] scaleFactors = {brightness, brightness, brightness, 1.0f};
      // don't change alpha
      // without the 1.0f the RescaleOp fails, which is a bug (?)
      float[] offsets = {0.0f, 0.0f, 0.0f, 0.0f};
      brigherOp = new RescaleOp(scaleFactors, offsets, null);
    } else   // not transparent
      brigherOp = new RescaleOp(brightness, 0, null);

    g2d.drawImage(im, brigherOp, x, y);
  }

It seems you are performing the operation everytime you render the image (?)

You should write the result into another image and use that multiple times, that way you save recalculating the images all the time.

Alpha blending in Java2D is done in software, so that could also be the problem. If you use an OpenGL based 2D engine/renderer, that will get you alpha blending in hardware.

oops indeed i created the effect time after time…

  
  // Create additional darker Terrain images, used for Fog
    for (BufferedImage img : terrainImages) {
      BufferedImage darkerCopy;
      darkerCopy = new BufferedImage(img.getWidth(), img.getWidth(), BufferedImage.OPAQUE);
      Graphics2D g = (Graphics2D) darkerCopy.getGraphics();
      imgSFX.drawBrighterImage(g, darkerCopy, 0, 0, 0.5F);
      darkTerrainImages.add(darkerCopy);
    }

I’m trying to make the darker images in the constructor of my map, but it shows a black square when i paint.
is there something wrong with the code above? I don’t see it.

OPAQUE means all pixels should be drawen
It’s not giving a null pointer exception so it’s there already
I used the 0.5 before should make it a bit darker. but not black!

  private void paintTerrain(Graphics2D g, Terrain terrain, boolean fogged) {
    if (terrain != null) {
      BufferedImage buf;
      if (fogged) {
        buf = darkTerrainImages.get(terrain.getType().ordinal());
      } else {
        buf = terrainImages.get(terrain.getType().ordinal());
      }
      g.drawImage(buf, 0, 0, null);
    }
  }

@CaptainJester
I would like to stick to java2D atm

got it! I first need to copy the other image :smiley:

  public BufferedImage grabImage(int width, int height, BufferedImage srcImg, int col, int row) {
    BufferedImage copy;
    Graphics2D graphics;
    int transparency = srcImg.getColorModel().getTransparency();

    // Create new Image
    copy = this.gc.createCompatibleImage(width, height, transparency);

    // create a graphics context for the new Image
    graphics = copy.createGraphics();

    // copy image from srcImg to copy
    graphics.drawImage(srcImg, 0, 0, width, height,       // copy img rectangle
            (col * width), row * height,                  // source img top Left rectangle cord
            ((col + 1) * width), (height * (row + 1)),    // source img lower Right rectangle cord
            null);
    graphics.dispose();
    return copy;
  }

solved, on to the next