This discussion is archived
0 Replies Latest reply: Nov 26, 2012 1:50 PM by 976314 RSS

Window Translucency and BufferStrategy Problem

976314 Newbie
Currently Being Moderated
Using Java 7 we can now have per-pixel translucency for our windows. (see [])

This works fine until you attempt to use active rendering with it. By this I mean using a BufferStrategy created from the translucent window.

In the example app below, the BufferStrategy works when per-pixel translucency is not used. However, if it is used (by uncommenting the first setBackground line and commenting the second), you will see the frame translucent, but nothing from the BufferStrategy is ever rendered.

Am I doing something wrong here? How can I get around this limitation (in other words, how can I do active rendering with a translucent window)?

Setup: Oracle JRE 7u9 on Windows 7 x64
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GraphicsDevice;
import java.awt.GraphicsDevice.WindowTranslucency;
import java.awt.GraphicsEnvironment;
import java.awt.Toolkit;
import java.awt.image.BufferStrategy;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class TestBufferedPerPixelTranslucency extends JFrame {
     public static void main(String[] args) {
          GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
          if (!device.isWindowTranslucencySupported(WindowTranslucency.PERPIXEL_TRANSLUCENT)) {
               System.out.println("Per-pixel translucency is not supported");
          SwingUtilities.invokeLater(new Runnable() {
               public void run() {
                    new TestBufferedPerPixelTranslucency();
     public TestBufferedPerPixelTranslucency() {
          this.setPreferredSize(new Dimension(400, 400));
          this.setMinimumSize(new Dimension(400, 400));
//          this.setBackground(new Color(0, 0, 0, 200)); // Using BufferStrategy doesn't work
          this.setBackground(new Color(0, 0, 0, 255)); // Using BufferStrategy works
          Thread t = new Thread(new Runnable() {
               public void run() {
                    while (true) {
                         BufferStrategy strategy = getBufferStrategy();
                         Graphics g = strategy.getDrawGraphics();
                         g.clearRect(0, 0, getSize().width, getSize().height);
                         g.fillRect(0, 0, (int)Math.floor(200 * Math.random()), (int)Math.floor(200 * Math.random()));
                         if (!strategy.contentsLost()) {
                         try {
                         } catch (InterruptedException e) {
Edited by: 973311 on Nov 26, 2012 1:50 PM


  • Correct Answers - 10 points
  • Helpful Answers - 5 points