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 [http://docs.oracle.com/javase/tutorial/uiswing/misc/trans_shaped_windows.html])

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");
               System.exit(0);
          }
          SwingUtilities.invokeLater(new Runnable() {
               @Override
               public void run() {
                    new TestBufferedPerPixelTranslucency();
               }
          });
     }
     
     public TestBufferedPerPixelTranslucency() {
          this.setDefaultCloseOperation(EXIT_ON_CLOSE);
          this.setUndecorated(true);
          this.setResizable(false);
          this.setPreferredSize(new Dimension(400, 400));
          this.setMinimumSize(new Dimension(400, 400));
          this.getRootPane().setOpaque(false);
          
          this.pack();
          
//          this.setBackground(new Color(0, 0, 0, 200)); // Using BufferStrategy doesn't work
          this.setBackground(new Color(0, 0, 0, 255)); // Using BufferStrategy works
          this.setVisible(true);
          
          this.createBufferStrategy(2);
          
          Thread t = new Thread(new Runnable() {
               @Override
               public void run() {
                    while (true) {
                         BufferStrategy strategy = getBufferStrategy();
                         Graphics g = strategy.getDrawGraphics();
                         
                         g.clearRect(0, 0, getSize().width, getSize().height);
                         g.setColor(Color.RED);
                         g.fillRect(0, 0, (int)Math.floor(200 * Math.random()), (int)Math.floor(200 * Math.random()));
                         
                         if (!strategy.contentsLost()) {
                              strategy.show();
                         }
                         
                         g.dispose();
                         
                         Toolkit.getDefaultToolkit().sync();
                         
                         try {
                              Thread.sleep(500);
                         } catch (InterruptedException e) {
                              e.printStackTrace();
                         }
                    }
               }
          });
          t.setDaemon(true);
          t.start();
     }
}
Edited by: 973311 on Nov 26, 2012 1:50 PM

Legend

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