Transparent and shaped windows
At the risk of making a total fool of myself...
I got very excited when I discovered that Java's "only opaque rectangular windows" limitation has been removed. Full release is intended for Java 7, but working methods can be accessed thru com.sun.awt.AWTUtilities in recent releases of Java 6 (I'm using update 20).
I put together this little demo that adds some animation to the effects to give a hint of the potential. It runs for me under Windows 7 Home Premium; I just had to adjust Eclipse's compiler settings to warn rather then error for access rule violations.
I'd be very interested to hear anyone else's experience of these exciting new capabilities.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Shape;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;
import com.sun.awt.AWTUtilities;
@SuppressWarnings("restriction")
public class DemoWindows implements ActionListener {
public static void main(String[] args) {
// create a new demo, and update it every 50 mSec
new Timer(50, new DemoWindows()).start();
}
int phase = 0; // demo runs a number of consecutive phases
int count = 0; // each of which takes a number of timesteps
JFrame window1 = new JFrame("Java windows demo");
JLabel text1 = new JLabel("<HTML><H1>This is a demo of some of the effects"
+ "<BR>that can be achieved with the new Java"
+ "<BR>transparent window methods</H1>"
+ "<BR>(requires latest version of Java)");
JFrame window2 = new JFrame("Java windows demo");
JLabel text2 = new JLabel("<HTML><center>Java<BR>rocks");
int w, h, r, x, y; // parameters of iris circle
DemoWindows() {
// build and diplay the windows
window1.add(text1);
window1.pack();
centerOnScreen(window1);
window1.setVisible(true);
window2.setUndecorated(true);
setTransparent(window2);
setAlpha(window2, 0.0f);
text2.setFont(new Font("Arial", 1, 60));
text2.setForeground(Color.red);
window2.add(text2);
window2.pack();
centerOnScreen(window2);
window2.setVisible(true);
// parameters of the smallest circle that encloses window2
w = window2.getWidth();
h = window2.getHeight();
r = (int) Math.sqrt(w * w + h * h) / 2; // radius
x = w / 2 - r; // top left coordinates of circle
y = h / 2 - r;
}
@Override
public void actionPerformed(ActionEvent e) {
// called by timer 20 times per sec
// goes thru a number of phases, each a few seconds long
switch (phase) {
case 0: {
// initial pause
if (++count > 50) {
phase = 1; // go to next phase
count = 0;
}
break;
}
case 1: {
// fade in
if (++count < 100) {
setAlpha(window2, 0.01f * count);
} else {
phase = 2; // go to next phase
count = 0;
}
break;
}
case 2: {
// move
if (++count < 160) {
if (count <30 || count > 80) // pause for best effect
window2.setLocation(window2.getX() +1, window2.getY() +1);
} else {
phase = 3; // go to next phase
count = 0;
}
break;
}
case 3: {
// iris out
if (++count < r) {
Shape shape = new Ellipse2D.Double(x + count, y + count,
2 * (r - count), 2 * (r - count));
setShape(window2, shape);
} else {
phase = 99; // go to final (exit) phase
}
break;
}
case 99:
System.exit(0);
}
}
void centerOnScreen(JFrame window) { // convenience method
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
window.setLocation((screenSize.width - window.getWidth()) / 2,
(screenSize.height - window.getHeight()) / 2);
}
void setTransparent(JFrame window) {
// cover for temporary API expected to change for Java 7
// should become: window.setBackground(new Color(0,0,0,0));
AWTUtilities.setWindowOpaque(window, false);
window.getRootPane().setOpaque(false);
}
void setAlpha(JFrame window, float alpha) {
// cover for temporary API expected to change for Java 7
// should become: window.setOpacity(alpha);
AWTUtilities.setWindowOpacity(window, alpha);
}
void setShape(JFrame window, Shape shape) {
// cover for temporary API expected to change for Java 7
// should become window.setShape(Shape)
AWTUtilities.setWindowShape(window, shape);
}
}
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
I'm interested to see what this looks like - could you post a screenshot of your application running?
I'm using Ubuntu at the moment and can't get it working. Will try it later on Windows 7 when I get a chance.
leiger
Junior Poster in Training
91 posts since Jun 2010
Reputation Points: 33
Solved Threads: 6
of app is thyast it animates transparent & shaped windows - so screenshot not very helpful! Let meknow how it goes on Win 7 for you.
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
Haven't had a chance to try Windows 7 just yet but I did do a few searches on this and found some documentation and example screen captures - transparent and shaped windows.
Not too sure about the animated part. What is being animated... the transition?
leiger
Junior Poster in Training
91 posts since Jun 2010
Reputation Points: 33
Solved Threads: 6
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
The second one I've already read as I found that one while searching myself. The first one is interesting. Thanks for the links.
leiger
Junior Poster in Training
91 posts since Jun 2010
Reputation Points: 33
Solved Threads: 6
I have another version of the demo that just displays a transparent .png (eg a splash screen) with fade in and iris out effects if you're interested. It also calls the temporary methods via Reflection as recommended in the second link - this also avoids access restriction problems with the latest 1.6 builds.
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
Update Nov '11...
Just to confirm that the APIs were released in Java 1.7 and work as expected.
James
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073