Using transparent window in both Java 6 and Java 7
Asked Answered
C

1

6

I'm developing application in Java 6 (1.6.0_24) which using transparent JFrame to get disappearing animation. Here is my code:

public static void slowDisappearWindowAction(Window source, int milisSlow, int milisFast) throws InterruptedException{
    float level = 1.0f;
    //slow effect -> 50%
    for(int i=0; i<8 ; i++){
        level=level-0.05f;
        AWTUtilities.setWindowOpacity(source,level);
        Thread.sleep(milisSlow);
    }
    //fast effect -> 0% 
    for(int i=0; i<8 ; i++){
        level=level-0.05f;
        AWTUtilities.setWindowOpacity(source,level);
        Thread.sleep(milisFast);
    }
    AWTUtilities.setWindowOpacity(source,0.1f);
}

It works fine on my machine, but when I tested it on another PC with Java 7 installed I have fallowing error:

 Exception in thread "AWT-EventQueue-0" java.awt.IllegalComponentStateException:
 The frame is decorated
    at java.awt.Frame.setOpacity(Unknown Source)
    at java.awt.Window$1.setOpacity(Unknown Source)
    at com.sun.awt.AWTUtilities.setWindowOpacity(Unknown Source)
    at pl.design.bead.pattern.model.window.WindowHelper.slowDisappearWindowAction(WindowHelper.java:21)
    at pl.design.bead.pattern.forms.MainForm$ExitController.windowClosing(MainForm.java:123)
    at java.awt.AWTEventMulticaster.windowClosing(Unknown Source)
    at java.awt.Window.processWindowEvent(Unknown Source)
    at javax.swing.JFrame.processWindowEvent(Unknown Source)
    at java.awt.Window.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

I think it is because in Java 7 I should use Window.setOpacity(...) instead of AWTUtilities methods.

It is possible to use transparency in Java 6 app, that will run on Java 7 ?

Condolent answered 18/7, 2012 at 7:7 Comment(1)
if it's only about compiling in jdk6 and running in jdk7, you could use reflection for both 6 and 7 api. No way to hack around the missing transparency support for undecorated windows in jdk7, though.Fauch
B
3

See How to Create Translucent and Shaped Windows.. It mentions a "per pixel" translucency that you can leverage to make a window with java 7 that has the look and feel of the translucent window from java 6 which is no longer available. So basically you'd have to code to accommodate either, or you could go with a "uniform translucency" which works with both.

Gradient Translucent Window

Boiardo answered 18/7, 2012 at 7:10 Comment(3)
Thanks for your answer. So I need to check compatibility first and resign from disappearing animation if it is not supported?Condolent
What? I thought your current method worked for 1.6. If so, use that up until the new transparency was supported, then use the new functionality for the rest of 1.6 and 1.7+.Boiardo
Yes, it is working, but in fact my question is how to compile transparent JFrame on computer with Java 6 only, so the program will run correctly on computer with Java 7 only. But I think the simplest solution is switch to Java 7.Condolent

© 2022 - 2024 — McMap. All rights reserved.