How to create Translucent and Shaped Windows in Java?
Overview: In this article we will discuss about the transparency and different shaped windows in Java. In java 7 swing supports this feature and making swing UI components more flexible and user friendly.
Introduction: In some application, transparent window is a requirement to support its functionality. Now in Java, transparency can be implemented. A translucent window is created by altering its opacity by implementing a method called setOpacity on a JFrame component. But we must understand that a translucent window is only possible if the underlying operating system supports it. And we also need to make sure that the window is not decorated. To make a window undecorated, you need to call setUndecorated (true) method. Some time it is also required to change the shape of a window UI. To implement it, we need to call setShape method within the componentResized method. It will recalculate the shape when the window is resized.
Back ground: In java UI, support for translucency and shaped window was a long time demand for Swing/AWT components. For native development access to these properties are available from long time back. But it was not accessible to core java components. In Java6 onward, support for translucency and shaped window is available. Even per pixel level translucency is also supports in Java7.
Type of support for translucency and transparency: Java 7 supports the following three type of support.
TRANSLUCENT: In this feature the window can have different colors but with the same level of opacity. So the alpha value is same for all the pixels. This effect is suitable for fade off a window and then gradually decreases and increases the alpha value.
PERPIXEL_TRANSLUCENT: This property supports different alpha values within the window itself. This is used to fade selected region of the window.
PERPIXEL_TRANSPARENT: This property supports different shapes of windows rather than traditional rectangle. If per pixel transparency is supported then different shapes like circle, triangle can be created.
Before we use any of the above properties we need to test the support of under lying operating system. The test can be done by using method isWindowTranslucencySupported belonging to the java.awt.GraphicsDevice class. It takes the type of transparency as input and returns true/false to indicate the support.
Let us check a sample code to implement the translucency feature. In this example we have created a rectangular translucent window on a text area. First we have disabled the decoration mode to get the effect of transparency. Then we have checked if the underlying operating system supports it or not. If supported the effect is visible on the frame window.
Listing1: Sample showing Translucency of a JFrame window
import java.awt.Color;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.GridBagLayout;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.geom.Rectangle2D;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
publicclassTransRecFrameextends JFrame {
/**
* Create a transparent rectangular frame with 85% transparency
*/
public TransRecFrame() {
super(“Translucent Rectangular Frame”);
//Set layout
setLayout(new GridBagLayout());
//Create a text area
final JTextArea txtArea = new JTextArea(5, 50);
txtArea.setBackground(Color.CYAN);
add(txtArea);
//Call to disable decoration
setUndecorated(true);
//Call setShape to resize the shape when widnow is resized
if (genv.getDefaultScreenDevice().isWindowTranslucencySupported(
GraphicsDevice.WindowTranslucency.TRANSLUCENT)) {
System.out.println(“OS supports translucency”);
new TransRecFrame();
}
}
}
Features to support translucency and shape: In Java7 there are mainly three features available to support translucency and shape of a window.
Full window translucency: In this approach full window is translucent.
Per pixel translucency: In this approach a part of the window is translucent
Shaped windows: Make different shaped windows like ovale, circle, rectangular etc.
Per pixel translucency:
We have already seen how to make a complete window translucent in the previous example. Now we will discuss the second part of making a set of pixels translucent by using their background color. There are some limitations to implement this scenario. The window should not be full screen and the system must support pixel level translucency. The rest of the procedure is similar to the above example.
In the following example we will see how pixel level translucency is set in a frame.
Now we will discuss about another important feature supported by Java7.The shaped window supports all types of shapes whatever be the requirement of the user. This feature helps you to create any shape like circle, triangle, polygon or any possible complex shape. The setShape method of the window class is available for setting the property. But again we have to remember that full screen mode should not be allowed and the operating system supports translucency.
The following example shows the usage of shaped window.
Listing3: The sample code showing the usage of shaped window.
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.GeneralPath;
publicclassJavaShapedWindowextends JFrame {
public JavaShapedWindow() {
super(“Set shaped Window”);
//Set undecorated OFF to get an effect
setUndecorated(true);
//Set size
setSize(new Dimension(250, 250));
//Set polygon properties
Polygon polygon = new Polygon();
polygon.addPoint(0, 100);
polygon.addPoint(50, 0);
polygon.addPoint(100, 100);
//Set the values of the shape
Ellipse2D.Double newCircle = new Ellipse2D.Double(0, 50, 1.0*100, 1.0*100);
We can also implement a combination of two features like translucency and shaped window. To implement this, call setOpacity method to your frame. The result window will display the combined effect. But we should remember the underlying operating system must support pixel level translucency and pixel level transparency.
if (refreshRequested && ((now – lastupdate) > 1000))
{
if (frame.isVisible())
{
Point location = frame.getLocation();
frame.setVisible(false);
updateBackground();
frame.setVisible(true);
frame.setLocation(location);
refresh();
}
lastupdate = now;
refreshRequested = false;
}
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
원리는 updateBackground() 가 호출될 때 현재 영역의 Screen을 캡쳐한 다음 paintComponent() 메소드가 호출될 때 캡쳐한 이미지를 보여 주는 것입니다.
public class TransparentWindowExample { public static void main(String[] args) { JFrame frame = new JFrame(“배경이 투명한 윈도우”); TransparentBackground bg = new TransparentBackground(frame); bg.setLayout(new BorderLayout()); JButton button = new JButton(“버튼 Component”); bg.add(“North”, button); JLabel label = new JLabel(“이것은 새로 생성한 윈도우에서 만든겁니다”); bg.add(“South”, label); frame.getContentPane().add(“Center”, bg); frame.pack(); frame.setSize(150, 100); frame.setVisible(true); }
}
문제는 투명도 % 조정이 안된다는 것입니다. ^^
다음은 똑같은 내용으로 이미지를 이용하여 윈도우를 만든 것입니다. 이미지 만들때 배경이 투명하게 만들어야 합니다.
public class TransparentWindowExample { public static void main(String[] args) { JFrame frame = new JFrame(“Transparent Window”); frame.setUndecorated(true);
TransparentBackground bg = new TransparentBackground(frame); bg.setLayout(new BorderLayout());
JPanel panel = new JPanel() { public void paintComponent(Graphics g) { g.setColor(Color.blue); Image img = new ImageIcon(“clock.png”).getImage(); g.drawImage(img, 0, 0, null); } }; panel.setOpaque(false);
음.. 예전에 자바로 작성된 프로그램중에 반투명으로 표시되는.. 그러니까 창이 뜨면 뒤에 배경까지 보이는 반투명으로된 프로그램을 본듯 한데.. 어떻게 하는 것인가요? JFrame이나 Frame의 LayerPane에 반투명을 설정하는 메소드가 있는 건가요? Document 뒤져봐도 찾을수 가 없군요..
혹시 아시는분 답변좀..
답변:re: 자바 스윙으로 반투명창을 만들수 있을까?
imsangchin/ 2004-02-17 22:04
다음은, swing을 이용한, 투명 윈도우 만드는 소스 입니다.참고로, java에서는 JFrame, JWindow같은, 중량 컴포넌트에 대해서 투명도를 설정할수 있는 메소드를 제공하지는 않습니다.위소스는 편법으로, Robet클래스를 이용해서, 배경화면을 캡쳐해서, 현재 윈도우가 투명하게끔 보이게 만든 것입니다. 흠이라면, 약간 속도 처리가 느리다는거죠.일반적으로 반투명윈도우 는 주로 VC++로 만든 프로그램입니다. VC++에서는 윈도우를 반투명으로 만드는 API를 제공하거든요.import java.awt.*; import java.awt.event.*; import javax.swing.*;public class TransparentWnd extends JWindow implements FocusListener, MouseMotionListener, MouseListener { Image img, tim; Graphics tig; Point mp; Robot r; public TransparentWnd() { setBounds(170, 170, 100, 100); try { r = new Robot(); } catch (AWTException awe) { System.out.println("robot excepton occurred"); } capture(); addMouseMotionListener(this); addMouseListener(this); addFocusListener(this); setVisible(true); } public void mouseDragged(MouseEvent m) { if (mp == null) { return; } Point p = m.getPoint(); int x = getX() + p.x - mp.x; int y = getY() + p.y - mp.y; setLocation(x, y); paintP(getGraphics()); } public void mouseMoved(MouseEvent m) {} public void mouseClicked(MouseEvent m) {} public void mouseEntered(MouseEvent m) {} public void mouseExited(MouseEvent m) {} public void mouseReleased(MouseEvent m) { mp = null; } public void mousePressed(MouseEvent m) { mp = m.getPoint(); } public void focusGained(FocusEvent fe) { setSize(0, 0); capture(); setSize(100, 100); } public void focusLost (FocusEvent fe) {} public void capture() { Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); img = r.createScreenCapture(new Rectangle(0, 0, d.width, d.height)); } public void captureX() { Rectangle rect = getBounds(); setVisible(false); Thread.yield(); Image xmg = r.createScreenCapture(rect); img.getGraphics().drawImage(xmg, rect.x, rect.y, rect.width, rect.height, null); setVisible(true); } public void paint(Graphics g) { Rectangle rect = g.getClipBounds(); if (tim == null) { tim = createImage(getWidth(), getHeight()); tig = tim.getGraphics(); } if (!rect.getSize().equals(getSize())) { captureX(); } else { paintP(g); } } public void paintP(Graphics g) { tig.drawImage(img, 0, 0, getWidth(), getHeight(), getX(), getY(), getX() + getWidth(), getY() + getHeight(), null); tig.setColor(Color.orange); tig.fillOval(10, 20, 70, 80); tig.setColor(Color.green); tig.fillOval(21, 16, 20, 10); tig.fillOval(40, 02, 11, 21); g.drawImage(tim, 0, 0, null); } public void update(Graphics g) { this.paint(g); } public static void main(String[] args) { new TransparentWnd(); } }
Not all platforms support all of these capabilities. An UnsupportedOperationException exception is thrown when code attempts to invoke the setShape or setOpacity methods on a platform that does not support these capabilities. Therefore, it is best practice to first check that the platform supports the capability that you want to implement. The GraphicsDevice class provides theisWindowTranslucencySupported(GraphicsDevice.WindowTranslucency) method that you can use for this purpose. You pass one of three enum values, defined in GraphicsDevice.WindowTranslucency, to this method:
TRANSLUCENT – The underlying platform supports windows with uniform translucency, where each pixel has the same alpha value.
PERPIXEL_TRANSLUCENT – The underlying platform supports windows with per-pixel translucency. This capability is required to implement windows that fade away.
PERPIXEL_TRANSPARENT – The underlying platform supports shaped windows.
The GraphicsConfiguration class also provides the isTranslucencyCapable method to determine if PERPIXEL_TRANSLUCENT translucency is supported by the given GraphicsConfiguration object.
Version note: The translucent and shaped window API was first added to the Java SE 6 Update 10 release as a private API. This functionality was moved to the public AWT package in the JDK 7 release. This tutorial describes the API that is available in the JDK 7 release. See Java SE 6 Update 10 API for a mapping of the private API in the Java SE 6 Update 10 release to the public API in the JDK 7 release.
The following code shows how to check for all three capabilities:
import static java.awt.GraphicsDevice.WindowTranslucency.*; // Determine what the default GraphicsDevice can support. GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice gd = ge.getDefaultScreenDevice(); boolean isUniformTranslucencySupported = gd.isWindowTranslucencySupported(TRANSLUCENT); boolean isPerPixelTranslucencySupported = gd.isWindowTranslucencySupported(PERPIXEL_TRANSLUCENT); boolean isShapedWindowSupported = gd.isWindowTranslucencySupported(PERPIXEL_TRANSPARENT);
Note: None of these capabilities work on windows in full-screen mode. Invoking any of the relevant methods while in full-screen mode causes an IllegalComponentStateException exception to be thrown.
How to Implement Uniform Translucency
You can create a window where each pixel has the same translucency by invoking the setOpacity(float) method in the Window class. The float argument passed to this method represents the translucency of the window and should be a value between 0 and 1, inclusive. The smaller the number, the more transparent the window. There is also a corresponding getOpacity method.
The TranslucentWindowDemo.java example creates a window that is 55 percent opaque (45 percent translucent). If the underlying platform does not support translucent windows, the example exits. The code relating to opacity is shown in bold.
import java.awt.*; import javax.swing.*; import static java.awt.GraphicsDevice.WindowTranslucency.*; public class TranslucentWindowDemo extends JFrame { public TranslucentWindowDemo() { super("TranslucentWindow"); setLayout(new GridBagLayout()); setSize(300,200); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Add a sample button. add(new JButton("I am a Button")); } public static void main(String[] args) { // Determine if the GraphicsDevice supports translucency. GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice gd = ge.getDefaultScreenDevice(); //If translucent windows aren't supported, exit. if (!gd.isWindowTranslucencySupported(TRANSLUCENT)) { System.err.println( "Translucency is not supported"); System.exit(0); } JFrame.setDefaultLookAndFeelDecorated(true); // Create the GUI on the event-dispatching thread SwingUtilities.invokeLater(new Runnable() { @Override public void run() { TranslucentWindowDemo tw = new TranslucentWindowDemo(); // Set the window to 55% opaque (45% translucent). tw.setOpacity(0.55f); // Display the window. tw.setVisible(true); } }); } }
Note that the button is also affected by the uniform translucency. Setting the opacity affects the whole window, including any components that the window contains.
How to Implement Per-Pixel Translucency
Creating a window that uses per-pixel translucency involves defining alpha values over the rectangular area that the window occupies. When a pixel's alpha value is zero, that pixel is fully transparent. When a pixel's alpha value is 255, that pixel is fully opaque. When a pixel's alpha value is 128, that pixel is 50 percent translucent, and so on. An easy way to create a smooth interpolation between alpha values is to use the GradientPaint class. The included example uses this approach.
Invoking setBackground(new Color(0,0,0,0)) on the window causes the software to use the alpha values to render per-pixel translucency. In fact, invoking setBackground(new Color(0,0,0,alpha), where alpha is less than 255, installs per-pixel transparency. So, if you invoke setBackground(new Color(0,0,0,128)) and do nothing else, the window is rendered with 50 percent translucency for each background pixel. However, if you are creating your own range of alpha values, you most likely will want an alpha value of 0.
While not prohibited by the public API, you will generally want to enable per-pixel translucency on undecorated windows. In most cases, using per-pixel translucency on decorated windows does not make sense. Doing so can disable the decorations, or cause other platform-dependent side effects.
To determine if a window is using per-pixel translucency, you can use the isOpaque method.
An example follows. First, here are the steps required to implement the example:
Invoke setBackground(new Color(0,0,0,0)) on the window.
Create a JPanel instance that overrides the paintComponent method.
In the paintComponent method, create a GradientPaint instance.
In the example, the top of the rectangle has an alpha value of 0 (the most transparent) and the bottom has an alpha value of 255 (the most opaque). The GradientPaint class smoothly interpolates the alpha values from the top to the bottom of the rectangle.
Set the GradientPaint instance as the panel's paint method.
Here is the code for the GradientTranslucentWindowDemo.java example. If the underlying platform does not support per-pixel translucency, this example exits. The code specifically relating to creating the gradient window is shown in bold.
import java.awt.*; import javax.swing.*; import static java.awt.GraphicsDevice.WindowTranslucency.*; public class GradientTranslucentWindowDemo extends JFrame { public GradientTranslucentWindowDemo() { super("GradientTranslucentWindow"); setBackground(new Color(0,0,0,0)); setSize(new Dimension(300,200)); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel() { @Override protected void paintComponent(Graphics g) { if (g instanceof Graphics2D) { final int R = 240; final int G = 240; final int B = 240; Paint p = new GradientPaint(0.0f, 0.0f, new Color(R, G, B, 0), 0.0f, getHeight(), new Color(R, G, B, 255), true); Graphics2D g2d = (Graphics2D)g; g2d.setPaint(p); g2d.fillRect(0, 0, getWidth(), getHeight()); } } }; setContentPane(panel); setLayout(new GridBagLayout()); add(new JButton("I am a Button")); } public static void main(String[] args) { // Determine what the GraphicsDevice can support. GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice gd = ge.getDefaultScreenDevice(); boolean isPerPixelTranslucencySupported = gd.isWindowTranslucencySupported(PERPIXEL_TRANSLUCENT); //If translucent windows aren't supported, exit. if (!isPerPixelTranslucencySupported) { System.out.println( "Per-pixel translucency is not supported"); System.exit(0); } JFrame.setDefaultLookAndFeelDecorated(true); // Create the GUI on the event-dispatching thread SwingUtilities.invokeLater(new Runnable() { @Override public void run() { GradientTranslucentWindowDemo gtw = new GradientTranslucentWindowDemo(); // Display the window. gtw.setVisible(true); } }); } }
Note that the button is not affected by the per-pixel translucency. Setting the per-pixel translucency affects the background pixels only. If you want a window that has a uniformly translucent effect on the background pixels only, you can invoke setBackground(new Color(0,0,0,alpha)) where alpha specifies your desired translucency.
How to Implement a Shaped Window
You can create a shaped window by invoking the setShape(Shape) method in the Window class. The Shape argument that is passed to the method determines how the window is clipped. When a shape is set on a window, the window decorations are not re-formed to the new shape, so setting a shape works best on undecorated windows.
The best practice for setting the window's shape is to invoke setShape in the componentResized method of the component event listener. This practice will ensure that the shape is correctly calculated for the actual size of the window. The following example uses this approach.
The ShapedWindowDemo.java example creates an oval-shaped window with 70 percent opacity. If the underlying platform does not support shaped windows, the example exits. If the underlying platform does not support translucency, the example uses a standard opaque window. You could modify this example to create a shaped window that also uses per-pixel translucency.
The code relating to shaping the window is shown in bold.
import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.awt.geom.Ellipse2D; import static java.awt.GraphicsDevice.WindowTranslucency.*; public class ShapedWindowDemo extends JFrame { public ShapedWindowDemo() { super("ShapedWindow"); setLayout(new GridBagLayout()); // It is best practice to set the window's shape in // the componentResized method. Then, if the window // changes size, the shape will be correctly recalculated. addComponentListener(new ComponentAdapter() { // Give the window an elliptical shape. // If the window is resized, the shape is recalculated here. @Override public void componentResized(ComponentEvent e) { setShape(new Ellipse2D.Double(0,0,getWidth(),getHeight())); } }); setUndecorated(true); setSize(300,200); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); add(new JButton("I am a Button")); } public static void main(String[] args) { // Determine what the GraphicsDevice can support. GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice gd = ge.getDefaultScreenDevice(); final boolean isTranslucencySupported = gd.isWindowTranslucencySupported(TRANSLUCENT); //If shaped windows aren't supported, exit. if (!gd.isWindowTranslucencySupported(PERPIXEL_TRANSPARENT)) { System.err.println("Shaped windows are not supported"); System.exit(0); } //If translucent windows aren't supported, //create an opaque window. if (!isTranslucencySupported) { System.out.println( "Translucency is not supported, creating an opaque window"); } // Create the GUI on the event-dispatching thread SwingUtilities.invokeLater(new Runnable() { @Override public void run() { ShapedWindowDemo sw = new ShapedWindowDemo(); // Set the window to 70% translucency, if supported. if (isTranslucencySupported) { sw.setOpacity(0.7f); } // Display the window. sw.setVisible(true); } }); } }