Transparent and irregular swing windows in Java

Source: Internet
Author: User
Tags java se

Supporting transparent and irregular windows has been a long coveted feature of the AWT and Swing teams. Although native applications have been using this feature for a major operating system for a long time, it is not available in core Java. The upcoming release of the "Consumer JRE" is being modified, which is a major update to Java SE 6. Java SE 6 provides an API for creating a top-level window that is irregular, fully transparent, and transparent to each pixel.

History

Developers of native applications typically enjoy more advanced flexibility in developing UI applications. But the price to pay for this is to limit the application to a particular platform, and in many cases this flexibility is less important than getting a richer UI experience and a tight desktop integration. Traditionally, Cross-platform UI toolkits, such as Swing, SWT, QT, and wxWidgets, tend to passively cope with well-known dilemmas. What happens when only certain target platforms support the required functionality? In this case, the simulation of missing features may only make you the opposite.

Irregular and transparent windows are the best examples of the limitations of Cross-platform UI Toolbox. If this feature is not supported on a specific target platform, then there is nothing more to do on the platform, and this feature may be used as a powerful parameter to add that functionality to the toolbox. However, the Swing developer community has long argued that the main target platform will soon provide these capabilities. In fact, Windows has been supporting irregular windows since Windows 95 (see the SetWindowRgn documentation on MSDN). Matching features in X11 has been available since 1989 (see the X nonrectangular Window Shape Extension Library PDF). In OS X you can only set the transparent background color on the JFrame.

Until now, there are three main options for Swing applications that are interested in cross-platform transparent and irregular windows.

Use Java.awt.Robot to capture the desktop before the target window is displayed. This approach has been commented on in the 41st chapter of Swing Hacks written by Joshua Marinacci and Chris Adamson.

Use JNI to wrap the native APIs for the target platform.

Use the JNA library developed by the Timothy Wall. The library came out in 2007, and Timothy has already posted a blog about irregular windows and the transparency of the letter mask.

The main problem with the first approach is to use the Robot class. Even if you have permission to get screenshots, you must do so before displaying the window. In addition, how do you keep your desktop background sync? Suppose you are playing a YouTube video in the background. Unlike the events generated by the window (resizing, moving), AWT does not provide any way to register listeners on the redraw of any cross window. Although Chris and Joshua provide a workaround by making snapshots at least every second, this is not enough for overwriting background video playback. And you need to hide the window before each snapshot, which can cause a visible flicker.

The use of JNI and JNA results in significant visual fidelity improvements. Pure JNI can lead to a steep drop in overhead: you must bind each of the relevant APIs for the target platform and bundle the native libraries. JNA for you to share this important task; It bundles the host libraries and provides class loaders that can extract and load them at run time. It supports Linux, OS X, Windows, Solaris, and FreeBSD.

Consumer JRE

Java SE 6 Update N, commonly referred to as the Consumer JRE, is the result of Sun's efforts to reconfigure Java as a viable way to develop rich desktop applications. The new features and major improvements list in the Consumer JRE are quite extensive and hide the particularly sparkling gems in the release notes of one of the most recent week's build code. Bug 6633275 is simply given the title "need to support irregular/transparent windows." But the potential for implementing the new functionality of the core JDK has far-reaching implications for Swing developers. The remainder of this article shows a few examples of how this functionality can be implemented and implemented.

Before further study, there is a very important consideration. Since the Consumer JRE is officially considered a minor update to the stable JDK release, no new APIs (classes, methods, etc.) can be added to the "public" package, such as java.awt or javax.swing. All the APIs discussed in this article appear in the new Com.sun.awt.AWTUtilities class, which is not an officially supported part of the API. Its location in Java SE 7 is most likely to change, and the signature method may change slightly between now and the final Consumer JRE release. So when this change happens, prepare to change your own code.

Awtutilities class

Let me first discuss the Com.sun.awt.AWTUtilities class, see the transparent and Irregular window blog entries in core Java. First we start with the simple window in Figure 1:

Figure 1. A window with controls

To make the window transparent, you can use the awtutilities.setwindowopacity (window, float) method, as shown in Figure 2:

Figure 2. Same window, but with 50% opacity

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.