I'm currently making a 2D platformer game and now I want to implement Light using AlphaComposite. For creating the lights I first create a blank BufferedImage with ARGB format. After this, I create darkness by filling a black transparent rectangle over my screen. Now I am using AlphaComposite.DST_OUT and an image with an white cirle. When I render the white circle, the black darkness will be eliminated caused by the AlphaComposite. Lastly, I render this BufferedImage to my main BufferedImage where all the rendered stuff comes together. As you can see on the picture it works fine but when I resize my game it starts to lag extremely. Without using the light system the game actually runs perfect in fullscreen, as well.

enter image description here

Here is an extract of my code where I render my lights:

public void render(Graphics2D g) {
    BufferedImage image = new BufferedImage(GamePanel.width, GamePanel.height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = image.createGraphics();
    g2.setColor(new Color(0,0,0,210));
    g2.fillRect(0, 0, GamePanel.width, GamePanel.height);

    Composite old = g2.getComposite();
    AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.DST_OUT, 1.0f);
    g2.drawImage(Light.LIGHT, 50, 25, null); //here i render the image with the white cirle

    g.drawImage(image, 0, 0, null); //drawing lights to the main bufferedimage

The last line g.drawImage(image, 0, 0, null); is where I render the light to my main BufferedImage where all the rendered stuff is rendered, as well.

This is how I render my main BufferedImage:

public void paintComponent(Graphics g) {

    Graphics2D g2d = image.createGraphics();
    g2d.setBackground(new Color(146, 189, 221));
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);

    g.drawImage(image, 0, 0, width, height, null); //scaling the main bufferedimage

Do you have some Ideas for me?

EDIT The game also starts lagging in fullscreen when I just paint the black rectangle.

  • \$\begingroup\$ Your first problem is that you're using Java and expecting performance. The two are not synonymous. Your next problem is that BufferedImage, Canvas, etc, all use deprecated AWT APIs. You're running everything in software, take a look at your Cpu performance if you don't believe me. Your Cpu should be 90-100% with your rendering loop. Again, this is intrinsically due to the fact that you're using deprecated APIs. So, how do you fix this? Use Lwjgl, and additionally, drop Java altogether and seek QT and C++, or C# and OpenTK. At least C# has structs. \$\endgroup\$
    – Krythic
    Jan 17, 2017 at 21:55

1 Answer 1


I found the answer:

My game is actually very small but I resize it by a scale factor of 4. So I also have to divide the width and height of my bufferedimage by 4 inside the light render method

public void render(Graphics2D g) {
BufferedImage image = new BufferedImage(GamePanel.width / SCALE, GamePanel.height / SCALE, BufferedImage.TYPE_INT_ARGB);
  • \$\begingroup\$ You're creating a new Buffered image every frame? I wish you could read that from my point of view, maybe then you would understand how horrible it is. \$\endgroup\$
    – Krythic
    Jan 17, 2017 at 21:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .