Swing 根据图片设置不规则窗体

  1 import com.sun.awt.AWTUtilities;
  2 import java.awt.Image;
  3 import java.awt.MediaTracker;
  4 import java.awt.Point;
  5 import java.awt.Rectangle;
  6 import java.awt.Shape;
  7 import java.awt.Toolkit;
  8 import java.awt.event.MouseAdapter;
  9 import java.awt.event.MouseEvent;
 10 import java.awt.event.MouseMotionAdapter;
 11 import java.awt.geom.Area;
 12 import java.awt.image.PixelGrabber;
 13 import java.io.IOException;
 14 import java.util.ArrayList;
 15 import javax.swing.*;
 16 /**
 17  *
 18  * @author 19  */
 20 public class CreateShape {
 21     private Image img;
 22     private JFrame jf;
 23     private Point origin;
 24     public CreateShape( JFrame jf) throws InterruptedException, IOException {
 25         this.jf = jf;
 26         MediaTracker mt = new MediaTracker(jf);
 27         //获取指定图片
 28         img = new ImageIcon("src/main/images/bg.png").getImage();
 29         mt.addImage(img, 0);
 30         //等待就绪
 31         mt.waitForAll();
 32         initialize(); //窗体初始化
 33     }
 34     private void initialize() throws IOException {
 35         //设定窗体大小和图片一样大
 36         jf.setSize(img.getWidth(null), img.getHeight(null));
 37         //设定禁用窗体装饰,这样就取消了默认的窗体结构
 38         jf.setUndecorated(true);
 39         //初始化用于移动窗体的原点
 40         origin = new Point();
 41         //调用AWTUtilities的setWindowShape方法设定本窗体为制定的Shape形状
 42         AWTUtilities.setWindowShape(jf, getImageShape(img));
 43         //设定窗体可见度
 44         AWTUtilities.setWindowOpacity(jf, 0.9f);
 45         jf.setLocationRelativeTo(null);
 46     }
 47 
 48     public Shape getImageShape(Image img) {
 49         ArrayList<Integer> x = new ArrayList<Integer>();
 50         ArrayList<Integer> y = new ArrayList<Integer>();
 51         int width = img.getWidth(null);//图像宽度
 52         int height = img.getHeight(null);//图像高度
 53         //筛选像素
 54         //首先获取图像所有的像素信息
 55         PixelGrabber pgr = new PixelGrabber(img, 0, 0, -1, -1, true);
 56         try {
 57             pgr.grabPixels();
 58         } catch (InterruptedException ex) {
 59             ex.getStackTrace();
 60         }
 61         int pixels[] = (int[]) pgr.getPixels();
 62         for (int i = 0; i < pixels.length; i++) {
 63             //筛选,将不透明的像素的坐标加入到坐标ArrayList x和y中
 64             int alpha = getAlpha(pixels[i]);
 65             if (alpha == 0) {
 66                 continue;
 67             } else {
 68                 x.add(i % width > 0 ? i % width - 1 : 0);
 69                 y.add(i % width == 0 ? (i == 0 ? 0 : i / width - 1) : i / width);
 70             }
 71         }
 72         int[][] matrix = new int[height][width];
 73         for (int i = 0; i < height; i++) {
 74             for (int j = 0; j < width; j++) {
 75                 matrix[i][j] = 0;
 76             }
 77         }
 78         //导入坐标ArrayList中的不透明坐标信息
 79         for (int c = 0; c < x.size(); c++) {
 80             matrix[y.get(c)][x.get(c)] = 1;
 81         }
 82         Area rec = new Area();
 83         int temp = 0;
 84         for (int i = 0; i < height; i++) {
 85             for (int j = 0; j < width; j++) {
 86                 if (matrix[i][j] == 1) {
 87                     if (temp == 0) {
 88                         temp = j;
 89                     } else if (j == width) {
 90                         if (temp == 0) {
 91                             Rectangle rectemp = new Rectangle(j, i, 1, 1);
 92                             rec.add(new Area(rectemp));
 93                         } else {
 94                             Rectangle rectemp = new Rectangle(temp, i, j - temp, 1);
 95                             rec.add(new Area(rectemp));
 96                             temp = 0;
 97                         }
 98                     }
 99                 } else {
100                     if (temp != 0) {
101                         Rectangle rectemp = new Rectangle(temp, i, j - temp, 1);
102                         rec.add(new Area(rectemp));
103                         temp = 0;
104                     }
105                 }
106             }
107             temp = 0;
108         }
109         return rec;
110     }
111     private int getAlpha(int pixel) {
112         return (pixel >> 24) & 0xff;
113     }
114     public static void main(String[] args) {
115         try {
116             JFrame jf = new JFrame();
117             CreateShape c  = new CreateShape(jf);
118             jf.setVisible(true);
119         } catch (InterruptedException e) {
120             e.printStackTrace();
121         } catch (IOException e) {
122             e.printStackTrace();
123         }
124     }
125 }
126  

 

posted @ 2018-09-14 01:52  aloneOnWorld  阅读(403)  评论(1编辑  收藏  举报