码迷,mamicode.com
首页 > 编程语言 > 详细

Java 创建不规则窗口

时间:2017-09-16 16:05:15      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:rup   adapter   for   exit   released   intvalue   int()   set   efault   

 

import com.sun.awt.AWTUtilities;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.geom.Area;
import java.awt.image.PixelGrabber;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.JFrame;

public class createShape
{
  private Image img;
  private JFrame jf;
  private Point origin;
 
  public createShape(JFrame jf, String image)
  {
    this.jf = jf;
    MediaTracker mt = new MediaTracker(jf);
    this.img = Toolkit.getDefaultToolkit().getImage(getClass().getResource(image));
    mt.addImage(this.img, 0);
    try
    {
      mt.waitForAll();
    }
    catch (InterruptedException e)
    {
      e.printStackTrace();
    }
    try
    {
      initialize();
    }
    catch (IOException e)
    {
      e.printStackTrace();
    }
  }
 
  private void initialize()
    throws IOException
  {
    this.jf.setSize(this.img.getWidth(null), this.img.getHeight(null));
   
    this.jf.setUndecorated(true);
   
    this.origin = new Point();
   
    AWTUtilities.setWindowShape(this.jf, getImageShape(this.img));
   
    this.jf.setLocationRelativeTo(null);
  }
 
  public void addDragLisener()
  {
    this.jf.addMouseListener(new MouseAdapter()
    {
      public void mousePressed(MouseEvent e)
      {
        createShape.this.origin.x = e.getX();
        createShape.this.origin.y = e.getY();
      }
     
      public void mouseClicked(MouseEvent e)
      {
        if (e.getButton() == 3) {
          System.exit(0);
        }
      }
     
      public void mouseReleased(MouseEvent e)
      {
        super.mouseReleased(e);
      }
     
      public void mouseEntered(MouseEvent e)
      {
        createShape.this.jf.repaint();
      }
    });
    this.jf.addMouseMotionListener(new MouseMotionAdapter()
    {
      public void mouseDragged(MouseEvent e)
      {
        Point p = createShape.this.jf.getLocation();
        createShape.this.jf.setLocation(p.x + e.getX() - createShape.this.origin.x, p.y + e.getY() - createShape.this.origin.y);
      }
    });
  }
 
  public Shape getImageShape(Image img)
  {
    ArrayList<Integer> x = new ArrayList();
    ArrayList<Integer> y = new ArrayList();
    int width = img.getWidth(null);
    int height = img.getHeight(null);
   
    PixelGrabber pgr = new PixelGrabber(img, 0, 0, -1, -1, true);
    try
    {
      pgr.grabPixels();
    }
    catch (InterruptedException ex)
    {
      ex.getStackTrace();
    }
    int[] pixels = (int[])pgr.getPixels();
    for (int i = 0; i < pixels.length; i++)
    {
      int alpha = getAlpha(pixels[i]);
      if (alpha != 0)
      {
        x.add(Integer.valueOf(i % width > 0 ? i % width - 1 : 0));
        y.add(Integer.valueOf(i % width == 0 ? i / width - 1 : i == 0 ? 0 : i / width));
      }
    }
    int[][] matrix = new int[height][width];
    for (int i = 0; i < height; i++) {
      for (int j = 0; j < width; j++) {
        matrix[i][j] = 0;
      }
    }
    for (int c = 0; c < x.size(); c++) {
      matrix[((Integer)y.get(c)).intValue()][((Integer)x.get(c)).intValue()] = 1;
    }
    Area rec = new Area();
    int temp = 0;
    for (int i = 0; i < height; i++)
    {
      for (int j = 0; j < width; j++) {
        if (matrix[i][j] == 1)
        {
          if (temp == 0) {
            temp = j;
          } else if (j == width) {
            if (temp == 0)
            {
              Rectangle rectemp = new Rectangle(j, i, 1, 1);
              rec.add(new Area(rectemp));
            }
            else
            {
              Rectangle rectemp = new Rectangle(temp, i, j - temp, 1);
              rec.add(new Area(rectemp));
              temp = 0;
            }
          }
        }
        else if (temp != 0)
        {
          Rectangle rectemp = new Rectangle(temp, i, j - temp, 1);
          rec.add(new Area(rectemp));
          temp = 0;
        }
      }
      temp = 0;
    }
    return rec;
  }
 
  private int getAlpha(int pixel)
  {
    return pixel >> 24 & 0xFF;
  }
}

Java 创建不规则窗口

标签:rup   adapter   for   exit   released   intvalue   int()   set   efault   

原文地址:http://www.cnblogs.com/xxsl/p/7531362.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!