Java创建透明与任意形状窗口

Java创建透明与任意形状窗口

一: Java 2D图形基础知识

自从Java Swing发布以来,Java的图形能力得到了很大的提升,JDK6的高级版本开始支持创

建自定义形状的JFrame,结合Java 2D图形的相关API,可以实现任意形状JFrame的创建。当

前JAVA 2提供可以创建的基本形状如下:

 

当前Java提供的对2D图形的主要操作有裁剪和路径覆盖,适当的运用Java 2D与Swing的其

他相关API,可以创建出任意形状的JFrame。

 

二:API支持透明和自定义形状

从JDK6的release 10开始支持设置透明和自定义形状,支持的对象有Swing的JFrame, JDialog,

AWT的Frame.以及所以继承java.awt.Window的子类。

 

Windows的透明效果又分为两类,简单的透明效果和基于像素位的透明效果。简单透明效果使

用alpha来设置所有的象素位,值越小越透明,最小值是完全透明,最大值代表完全不透明

(opaque)windows, 基于像素为的透明效果需要通过以下API来启用:

AWTUtilities.setWindowOpaque (frame, false);

 

对于透明度和形状的设定都是通过Java的反射机制完成调用,在JDK的官方网站有一个很好的Demo里面有个AWTUtilitiesWrappe类是一个很好的示例.

 

三:一个可以改变自身形状与透明效果的JFrame

程序的主要功能是选择你想要的形状,然后点击【OK】按钮,应用程序的窗口随之改变

点击【Cancel】按钮将推出程序,移动鼠标到窗口上面,可以拖动窗口程序。

 

涉及到Swing组件有JFrame, JPanel, JButton, JComboBox

组件的位置放置使用了绝对定位的方式,需要调用setLayout(null)来声明绝对定位方式

程序启动的运行效果如下:

 

选择 Circle并点击【OK】按钮以后的效果如下:


选择 Area并点击【OK】按钮以后的效果如下:


主要的代码为JCustomFrame

[java]  view plain  copy
  1. import java.awt.BorderLayout;  
  2. import java.awt.Dimension;  
  3. import java.awt.Shape;  
  4. import java.awt.Toolkit;  
  5. import java.awt.event.ActionEvent;  
  6. import java.awt.event.ActionListener;  
  7. import java.awt.event.ComponentAdapter;  
  8. import java.awt.event.ComponentEvent;  
  9. import java.awt.geom.Area;  
  10. import java.awt.geom.Ellipse2D;  
  11. import java.awt.geom.RoundRectangle2D;  
  12. import java.io.IOException;  
  13. import java.util.logging.Level;  
  14. import java.util.logging.Logger;  
  15.   
  16. import javax.imageio.ImageIO;  
  17. import javax.swing.JFrame;  
  18. import javax.swing.SwingUtilities;  
  19. import javax.swing.UIManager;  
  20.   
  21. public class JCustomFrame extends JFrame {  
  22.       
  23.     /** 
  24.      * gloomy fish 
  25.      */  
  26.     private static final long serialVersionUID = -523336873755438297L;  
  27.     private Shape shape;  
  28.     private float alpha = 1f;  
  29.     private Dimension arcSize = new Dimension(5050);  
  30.     protected static final int CIRCLE_TYPE = 1;  
  31.     protected static final int RECTANGEL_TYPE = 0;  
  32.     protected static final int AREA_TYPE = 2;  
  33.       
  34.     public JCustomFrame() {  
  35.         setUndecorated(true);  
  36.         setVisible(true);  
  37.         setListenersForEffects();  
  38.     }  
  39.       
  40.     public JCustomFrame(int width, int height) {  
  41.         this();  
  42.         setSize(width, height);  
  43.     }  
  44.   
  45.     public JCustomFrame(Shape shape, int width, int height) {  
  46.         this(width, height);  
  47.         setShape(shape);  
  48.     }  
  49.   
  50.     public void setShape(Shape shape) {  
  51.         this.shape = shape;  
  52.     }  
  53.   
  54.     public JCustomFrame(float alpha, Shape shape, int width, int height) {  
  55.         this(shape, width, height);  
  56.         setAlpha(alpha);  
  57.     }   
  58.       
  59.     public void setAlpha(float alpha) {  
  60.         this.alpha = alpha;  
  61.     }  
  62.   
  63.     private void setListenersForEffects() {  
  64.         //It is important to upadate visual effect on form resize.  
  65.         addComponentListener(new ComponentAdapter() {  
  66.   
  67.             @Override  
  68.             public void componentResized(ComponentEvent evt) {  
  69.                 updateFrameEffects();  
  70.             }  
  71.         });  
  72.     }  
  73.   
  74.     /** 
  75.      * This updates visual effects like SHAPE form and transparency. You have to 
  76.      * update also <b>shape</b> property or it paints old shape ( if you resize 
  77.      * frame without resize shape .. ) 
  78.      */  
  79.     public void updateFrameEffects() {  
  80.         updateShape();  
  81.         try {  
  82.             AWTUtilitiesWrapper.setWindowShape(this, shape);  
  83.             if (shape != null) {  
  84.                 AWTUtilitiesWrapper.setWindowOpacity(this, alpha);  
  85.             }  
  86.         } catch (Exception ex) {  
  87.             Logger.getLogger(JCustomFrame.class.getName()).log(Level.SEVERE, null, ex);  
  88.         }  
  89.     }  
  90.       
  91.     public void updateShape() {  
  92.         if(shape == null) {  
  93.             shape = new RoundRectangle2D.Double(0d, 0d, getWidth(), getHeight(), arcSize.width, arcSize.height);  
  94.         }  
  95.           
  96.     }  
  97.       
  98.     public void updateShape(int type) {  
  99.         if(type == RECTANGEL_TYPE) {  
  100.             shape = new RoundRectangle2D.Double(0d, 0d, getWidth(), getHeight(), arcSize.width, arcSize.height);  
  101.         } else if(type == CIRCLE_TYPE) {  
  102.             shape = new Ellipse2D.Double(00,400400);  
  103.         } else if(type == AREA_TYPE) {        
  104.             Shape circle1 = new Ellipse2D.Double(00,400400);  
  105.             Shape circle2 = new Ellipse2D.Double(200100,400400);  
  106.             Area area1 = new Area(circle1);  
  107.             Area area2 = new Area(circle2);  
  108.             area1.subtract(area2);  
  109.             shape = area1;  
  110.         }  
  111.     }  
  112.       
  113.     public void center() {  
  114.         Toolkit tk = Toolkit.getDefaultToolkit();  
  115.         Dimension screenSize = tk.getScreenSize();  
  116.         int screenHeight = screenSize.height;  
  117.         int screenWidth = screenSize.width;  
  118.         this.setLocation((screenWidth - this.getWidth()) / 2, (screenHeight - this.getHeight()) / 2);  
  119.     }  
  120.       
  121.     public static void main(String[] args) {  
  122.         SwingUtilities.invokeLater(new Runnable() {  
  123.   
  124.             public void run() {  
  125.                 try {  
  126.                     // com.sun.java.swing.plaf.windows.WindowsLookAndFeel  
  127.                     UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");  
  128.                 } catch (Exception e) {  
  129.                 }  
  130.   
  131.                 /*These are simple custom panel generated with vidual editor of Netbeans 
  132.                   don't care about it, take a look only to ImagePanel inherit ( why?...) 
  133.                  */  
  134.                   
  135.                 // Cool transparent Frame  
  136.                 final JCustomFrame customFrame = new JCustomFrame();  
  137.                 customFrame.setLayout(new BorderLayout());  
  138.                   
  139.                 // create custom JPanel  
  140.                 final JImagePanel panel = new JImagePanel();  
  141.                 java.net.URL image1 = this.getClass().getResource("ball.jpg");  
  142.                 try {  
  143.                     panel.setImage(ImageIO.read(image1));  
  144.                     panel.addActionListener(new ActionListener() {  
  145.                           
  146.                         @Override  
  147.                         public void actionPerformed(ActionEvent e) {  
  148.                             if(e.getActionCommand().equals("OK")) {  
  149.                                 System.out.println("Transfer now......");  
  150.                                 customFrame.updateShape(panel.getSelectedIndex());  
  151.                                 if(panel.getSelectedIndex() == CIRCLE_TYPE) {  
  152.                                     customFrame.setSize(400400);  
  153.                                 } else if(panel.getSelectedIndex() == AREA_TYPE) {  
  154.                                     customFrame.setSize(400399); // force layout Manager re-computation  
  155.                                 } else {  
  156.                                     customFrame.setSize(400300);  
  157.                                 }  
  158.                             } else if(e.getActionCommand().equals("Cancel")) {  
  159.                                 System.out.println("System Exit......");  
  160.                                 customFrame.setVisible(false);  
  161.                                 customFrame.dispose();  
  162.                                 System.exit(0);  
  163.                             }  
  164.                         }  
  165.                     });  
  166.                 } catch (IOException e) {  
  167.                     e.printStackTrace();  
  168.                 }  
  169.   
  170.                 DragBarHandler dragHandle = new DragBarHandler(customFrame);  
  171.                 customFrame.add(panel, BorderLayout.CENTER);  
  172.                 customFrame.add(dragHandle, BorderLayout.NORTH);  
  173.                 customFrame.setTitle("Ttranslucency JFrame");  
  174.                 customFrame.setSize(400300);  
  175.                 customFrame.setAlpha(0.8f);  
  176.                 customFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  177.                 customFrame.center();  
  178.   
  179.             }  
  180.         });  
  181.   
  182.     }  
  183.   
  184. }  
原博主留下的全部源码下载地址:http://download.csdn.net/download/jia20003/4334289
  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值