Transparent Java Windows on X11

Transparent windows don't really work in Java. You have four choices, and they all suck:

1. Use non-opaque Color instances. Apple even supports setting a non-opaque background Color, but you need all the components in your UI to behave, and that really makes this awkward. And slow. This may be the method of the future, but at the moment, not enough pieces are there for it to work.

2. Use undocumented/unsupported Java methods. The beauty of this is that it's easy. The trouble with this is that it's not really what you want. Start GNOME Terminal, for example, and drag the transparency slider in the preferences dialog. (You need to be running Compiz.) You'll notice that although the terminal's transparency changes, the window decorations supplied by the system and the menu bar and scroll bar supplied by the application all keep the default fully-opaque level of transparency. (More subtly than that, you'll notice that it's really just the default background that's transparent. Anything rendered on top of that is rendered normally.)

3. Native code. This is currently your only choice on MS Windows. I've never explored this because I don't care enough about transparency (or Windows) to go to the trouble.

4. Swing Hacks's embarrassing copy-the-background trick. I sometimes think that book was on a secret mission to make Java look bad. (Then again, when GNOME Terminal switched to real transparency from a similarly ugly hack, some people complained because they could no longer see XEarth through the whole window stack; they were using "transparent" terminals specifically because they wanted to see the root window. It takes all sorts.)

Here, I'm talking about method 2 for X11 (Linux and Solaris). The Mac OS equivalent is well-known, and can be found every so often on Apple's java-dev mailing list, or around the web if you search for "CWindow.setAlpha". I've yet to see the X11 equivalent, though, so that's what I'm going to show you today.

The code's really just these two lines:

long windowId = peer.getWindow();
sun.awt.X11.XAtom.get("_NET_WM_WINDOW_OPACITY").setCard32Property(windowId, value);

Things are made more complicated by the fact that Component's peer field is only accessible within its package (or via a deprecated method), and XAtom isn't necessarily available on all platforms, and will win you a warning even on Sun's Unix JDK. So it's the usual reflection dance:

import java.awt.*;
import java.lang.reflect.*;
import javax.swing.*;
import javax.swing.event.*;

public class TransparencyTest extends JFrame implements ChangeListener {
private JSlider slider;

public TransparencyTest() {
super("Transparency Test");

slider = new JSlider(0, 255);

JPanel panel = new JPanel(new BorderLayout());
panel.add(slider, BorderLayout.CENTER);
setSize(new Dimension(640, 480));

public void stateChanged(ChangeEvent e) {
double value = ((double) slider.getValue())/((double) slider.getMaximum());
setWindowOpacity(this, value);

public void setWindowOpacity(Frame frame, double opacity) {
long value = (int) (0xff * opacity) << 24;
try {
// long windowId = peer.getWindow();
Field peerField = Component.class.getDeclaredField("peer");
Class<?> xWindowPeerClass = Class.forName("sun.awt.X11.XWindowPeer");
Method getWindowMethod = xWindowPeerClass.getMethod("getWindow", new Class[0]);
long windowId = ((Long) getWindowMethod.invoke(peerField.get(frame), new Object[0])).longValue();

// sun.awt.X11.XAtom.get("_NET_WM_WINDOW_OPACITY").setCard32Property(windowId, value);
Class<?> xAtomClass = Class.forName("sun.awt.X11.XAtom");
Method getMethod = xAtomClass.getMethod("get", String.class);
Method setCard32PropertyMethod = xAtomClass.getMethod("setCard32Property", long.class, long.class);
setCard32PropertyMethod.invoke(getMethod.invoke(null, "_NET_WM_WINDOW_OPACITY"), windowId, value);
} catch (Exception ex) {
// Boo hoo! No transparency for you!

public static void main(String[] args) {
new TransparencyTest().setVisible(true);

I'm not sure how useful this is, but at least you can no longer complain you weren't aware you had the option.

There are cases where you might actually want this kind of transparency, the sole example amongst things I've written was an application that monitored what iTunes was playing and, whenever it changed, faded in a transparent output-only window containing huge text telling me what I was now listening to.

Such uses, though, are probably few and far between.


Review: Intel Q6600 processor and DP35DP motherboard

I've been waiting for a good enough Mac mini to replace my dual G5, but you'll forgive me if I'm not exactly excited by the "new" Mac mini. It's a year overdue, and it's stupidly expensive for what it is. I don't want to get into the whole "Apple versus Dell" nonsense, and I think some of Apple's line-up is reasonably priced, but if the Mac mini were a child, the state would have intervened by now. It's so neglected it seems not so much unloved as actively hated.

For a cost falling somewhere between the two currently-available Mac mini models (USD599 and USD799), I just put together a machine with a quad-core 2.4 GHz processor (twice as many faster cores), twice the memory, twice the disk, and a P35 motherboard rather than some kind of hamstrung low-end laptop motherboard.

Sure, I had a case, a fancy PSU, and a low-end (by PC standards; high-end by Apple's standards) graphics card lying around, but that's the other thing that's unappealing about Apple's current line-up: this idea that I want to chuck out everything I've got. Both in terms of "why can't I run Mac OS on the perfectly good and significantly better than my dual G5 machine that I've just upgraded from?" and in terms of "why would I want an iMac, where I have to upgrade everything or nothing, and can't independently upgrade disk, graphics card, or processor as/when needed?".

Deeper, though, my annoyance is about the lack of orthogonality.

Example: I'd love one of the new small keyboards if it were wired. Other people would like one if it had a numeric keypad, which is essentially the opposite complaint. Apple says (wired,big) or (wireless,small) are the only choices, where I want (wired,small) and others want (wireless,big), the two missing choices.

Example: I'd like to run Mac OS on my spare amd64 box because it's more powerful than the dual G5, but (non-apple-hardware,mac-os) isn't a legal choice (in both senses of the world "legal"). Sure, the web leads me to believe could probably buy it and run it anyway, but that's not a particularly tempting option.

Example: I'd like a Mac mini with "lots" of memory and a "fast" processor and a small disk (where Apple defines "lots" as 2 GiB and "fast" as 2.0 GHz x 2 cores on a bus so slow you may as well walk), but -- even in the build-to-order options -- you can't have that.

The whole Intel thing has worked out interestingly in my eyes. I cut Apple less slack now because they've got a close enough relationship with Intel that they're shipping iMacs with Intel X7900 processors before they're available to anyone else, but they happily shipped Mac minis with Core Duos long after those processors were otherwise obsolete.

So. Naughty Apple, no cash for you.

Intel, though, did manage to tempt me. For the first time, quad-core processors are available for reasonable prices. For about USD300 (wholesale price currently USD266), you can get Intel's Core 2 Quad Q6600, a 2.4 GHz quad-core processor. As I said, I already had cases and graphics cards and PSUs, but not having had an Intel machine before (ever), I needed a new motherboard and new RAM.

Motherboard choice is something I find interesting. Obviously, I want a motherboard that supports my hardware (which varies) and my software (Ubuntu Linux), but more philosophically I want one whose manufacturer seems to support (in the "actively assist and encourage" sense rather than the "allow the use of" sense used earlier) my hardware and software. Tyan, for example, seem like good people. Quality motherboards made by people who care about running Linux (and LinuxBIOS). Biostar, on the other hand, also make (in my limited experience) nice motherboards, especially for the price, but their attitude seems to be the typical Taiwanese "use Windows, sucka!". I won't even mention Asus, though I hope one day to spit on their grave. I don't want to have to use Windows to be able to use all my hardware, I don't want to have to use Windows to be able to flash the latest version of my BIOS, and I sure as hell don't want to have to use Windows to be able to read the motherboard manual!

Intel, though they've not been great in the past, seem pretty well-behaved these days. I've no doubt it's because they realize there's money in Linux, but there's no point worrying about motives. The fact is that they make it easy to do everything from/with Linux, and they provide explicit instructions for Linux users. If you want to flash the BIOS from a bootable DOS floppy, that's fine, but you can do it from a .iso or USB thumbdrive too. Whatever you consider most convenient.

Intel's motherboard manuals, too, are a breed apart. If you've ever looked at the crap that comes out of Taiwan, you probably felt like you'd wasted your time. Asus, in particular, have no excuse for producing such appalling documentation. I've no idea whether their Chinese documentation is acceptable, but their English versions have the air of being done by someone who (a) didn't know the subject and (b) didn't give a shit. Asus' motherboard manuals have zero useful content, and if you think they're bad, you should see their graphics card manuals.

Funnily enough, Intel's manuals are PDF. I'd already read the manual on the web, and was surprised that the box only contained a manual on CD-ROM. There was the usual sticker for the inside of the case showing you the locations of everything interesting, and the jumper/header pinouts, and a nice Sun-style fold-out sheet showing what to do with IKEA-like simplicity (marred slightly by some copy/paste from the documentation from related but different boards).

The box was full of junk mail, though. A million leaflets for all kinds of crap. Not classy.

There was a 3.5" floppy disk with some kind of Windows RAID driver. May the pink unicorn in the sky grant me the wisdom to see why, given that this is a legacy-free board that doesn't have so much as PS/2 keyboard and mouse connectors, let alone anything as profane as a floppy disk connector. (There's an IDE connector so you don't need to buy a new optical drive, assuming your last machine wasn't already SATA in that regard.)

There was a metal back-panel header that was bent. Seriously. It wasn't made of flimsy metal, either. So either there are some weird bent cases and PCI cards out there, or something really unfortunate happened to that on its way into the box.

The build quality of the board was okay, but looked and felt cheaper than Tyan (which, of course, it was), and even Biostar (which it wasn't). I'd expected better from Intel, but it's still adequate.

The PCIe x16 slot is too close to the DIMM slots. You are not going to be able to add/remove memory without removing your graphics card. Other than that, everything's fine. It might have been nice to have the IDE connector over by where the optical drives are going to be, but anyone who cares that much about getting air in through the front has probably switched to SATA or taped their IDE cable into a roll already anyway.

The Q6600 fitted easily (despite being my first socket 775), and the supplied heatsink/fan is fine. I was actually prepared for something as noisy as my dual G5, so I was pleasantly surprised. It's not silent, but it's quiet, and if you open a window or have music on, you're not going to hear it. (A loaded dual G5 is more than capable of making enough noise to be heard over any background noise you throw at it, short of a hand grenade. An idle dual G5 is significantly noisier than my Q6600, despite the latter being in a CoolerMaster Stacker, with all the holes that entails.)

Power consumption's not too bad, either. The whole machine idles around 95W, and load is about 120W, but the thing's so damn fast it doesn't stay at full load long.

Since you ask, here's a comparison with a few other machines:

idle load
PowerMac dual G5 2.5GHz 170W 320W
Intel Q6600 95W 120W
Athlon X2 3800+ 80W 110W
Original Xbox 46W 53W

Now you know why the dual G5 PowerMac was so loud.

Quad core really is an improvement, for the things I do. I haven't felt this improvement since I first had a dual-processor machine to myself, eight years ago. Most of the stuff I was doing was already pretty fast (a few seconds) so this boost has pulled a lot of my prior pauses down into the sub-second range. And although that's not that much of a difference really, it's a big difference psychologically. It also helps point out those things you've yet to parallelize. Whenever something's not impressively fast, it's a fair bet a glance at the GNOME System Monitor in the panel will confirm that 3/4 of your processing power was idle. (Amusingly, at the default size on a 30" display, one core's worth of load is hardly visible.)

Speaking of software, I'm having to run Ubuntu 7.10 before it's released. The DP35DP isn't supported by anything earlier. Under 7.10, though, everything works out of the box. But bear in mind if you prefer not to live on the bleeding edge, you might want to wait until October before going for a P35 motherboard for Linux use. I've only had my installation made unusable once in the month I've been using it, and that was easily worked around by booting the previous kernel until the necessary drivers for the new kernel appeared.

Booting doesn't improve that much. Starting GNOME is still strangely slow (and now gives no indication of what it's doing). If your main tasks aren't easily parallelized or are sufficiently long-running that halving the time it takes to do them isn't significant (5 hours instead of 10, for example, would probably still mean "once a day"), there might not be much point in quad core. Judging by the web, Windows gamers mostly prefer faster-clocked dual-core machines because their games won't use the extra two cores. But real people doing real work with a real OS are likely to see good results. If I had my USD300 again, I wouldn't think twice.

Speaking of Ubuntu 7.10, Compiz is coming along nicely. To be honest, Ubuntu looks like shit compared to SuSE. Literally, thanks to all that brown. But the whole experience is kind of like booting Windows compared to booting Mac OS. The former looks like a kludge, flashing in and out of various resolutions and between text and graphics modes, where the latter is seamless. (SuSE still does a better job of hiding the boot-time gore from your other family members while letting you see everything when you want to, too.)

Ubuntu 7.10's alt-tab is okay, though, with thumbnails of the windows sliding past, as if Mac OS' Expose had bred with Windows' alt-tab. It does suffer the same ugly aliasing effects that Expose has, though. The virtual desktop switching is very nice, visibly sliding the current desktop out of the way slow enough that you feel the change but fast enough that you don't feel like you're waiting watching it; much better than Mac OS' achingly slow and pointless rotating cube for "fast" user switching. Window shadows look reasonable now, and the transparency of non-focused title bars is no worse than Mac OS 10.2; hopefully they, like Apple, will make them less transparent as time goes on. Long-time Mac users know that effect didn't really work as much as it was cool. My internal jury's still out on the way that windows shuffle out of the way like cards when you change the focus between overlapping or nearly-overlapping windows. I think I like it, but I'm not sure (after only a month) that isn't just the excitement of novelty. I feel like I have a better idea of where my window's gone in the stack, but do I really, and did I ever actually have a problem that needed solving in that regard?

My fix for the GNOME weather applet bug that meant that the icon would switch to night visibly far too early if you were far from UTC is in 7.10, too, which means I'm spared that minor aggravation every evening.

Firefox continues to be fine, Evolution continues to suck. I gave Balsa another go the other day, for the first time since I used to run my own fork of it in the 1990s, but I wasn't impressed. I felt like it was a KDE app in GNOME's clothing. So I'm still looking for a good Linux mailer.

I'm not, though, currently looking for any new Linux hardware. My Q6600 and DP35DP is a great combination (though I do wish Intel would make a discrete graphics card with a Free driver).