Java7 之透明和规则形状的窗口

红薯 发布于 2010/06/02 21:31
阅读 2K+
收藏 4
JDK

Java 7 为桌面应用开发带来了几个新的特性:

  • 透明窗口,可根据不同的alpha值设置透明度
  • 像素半透明,窗口部分透明化
  • 各种规则形状的窗口,可创建例如圆形、椭圆等各种形状的窗口

接下来我们给出一些示例代码来演示这些新的特性:

1. 透明窗口

可以通过 GraphicsDevice 类来判断是否支持透明窗口,代码如下:

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
if (ge.getDefaultScreenDevice().isWindowTranslucencySupported(GraphicsDevice.WindowTranslucency.TRANSLUCENT)) {
    System.out.println("Window translucency isn't supported on your system. ");
}
下面是一个透明窗口的例子:

package com.wicht.java7.swing;
 
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
 
import java.awt.*;
 
public class TranslucentWindow extends JFrame {
    public TranslucentWindow() {
        super("Simple Translucency Demo");
 
        setSize(300, 200);
 
        setDefaultCloseOperation(EXIT_ON_CLOSE);
 
        getRootPane().setDoubleBuffered(false);
        setOpacity(0.5f);
 
        setLocationRelativeTo(null);
        setVisible(true);
    }
 
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run() {
                GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
 
                if (ge.getDefaultScreenDevice().isWindowTranslucencySupported(GraphicsDevice.WindowTranslucency.TRANSLUCENT)) {
                    new TranslucentWindow();
                }
            }
        });
    }
}

执行后的截图如下:

2. 点阵半透明

package com.wicht.java7.swing;
 
import javax.swing.*;
 
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 
public class GriddedTranslucentWindow extends JFrame {
    public GriddedTranslucentWindow() {
        super("Java 7 Per Pixel Translucency");
 
        setSize(300, 320);
 
        getContentPane().setLayout(new GridLayout(4, 4));
 
        for(int i = 0; i < 16; i++){
            add(new AlphaPanel(255 - i * 12));
        }
 
        setBackground(new Color(0, 0, 0, 0));
 
        setLocationRelativeTo(null);
        setVisible(true);
    }
 
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run() {
                GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
 
                if (ge.getDefaultScreenDevice().isWindowTranslucencySupported(GraphicsDevice.WindowTranslucency.PERPIXEL_TRANSLUCENT)) {
                    new GriddedTranslucentWindow();
                }
            }
        });
    }
 
    private class AlphaPanel extends JPanel {
        private AlphaPanel(int alpha) {
            super();
 
            setBackground(new Color(0, 0, 255, alpha));
        }
    }
}

执行后截图:

3. 规则形状窗口

package com.wicht.java7.swing;
 
import javax.swing.*;
 
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.GeneralPath;
 
public class ShapedWindow extends JFrame {
    public ShapedWindow() {
        super("Shaped Window");
 
        setUndecorated(true);
 
        setSize(new Dimension(200, 300));
 
        Polygon polygon = new Polygon();
        polygon.addPoint(0, 200);
        polygon.addPoint(100, 0);
        polygon.addPoint(200, 200);
 
        Ellipse2D.Double theCircle = new Ellipse2D.Double(0, 100, 1.0*200, 1.0*200);
 
        GeneralPath path = new GeneralPath();
        path.append(polygon, true);
        path.append(theCircle, true);
 
        setShape(path);
 
        getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.X_AXIS));
 
        add(Box.createHorizontalGlue());
        JLabel label = new JLabel("Shaped window");
        label.setForeground(Color.white);
        add(label);
        add(Box.createHorizontalGlue());
 
        getContentPane().setBackground(Color.blue);
 
        setLocationRelativeTo(null);
        setVisible(true);
    }
 
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run() {
                GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
 
                if (ge.getDefaultScreenDevice().isWindowTranslucencySupported(GraphicsDevice.WindowTranslucency.PERPIXEL_TRANSPARENT)) {
                    new TranslucentShapedWindow();
                }
            }
        });
    }
}

执行后截图:

这些特性将为 Java 开发桌面应用带来更丰富的效果,非常棒。

加载中
0
navyblue
navyblue
win7 eclispe Indigo 测试报错,需 JFrame.setDefaultLookAndFeelDecorated(true);设置成java外观
返回顶部
顶部