Java 创建不规则窗口

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java 创建不规则窗口相关的知识,希望对你有一定的参考价值。

 

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 创建不规则窗口的主要内容,如果未能解决你的问题,请参考以下文章

Windows SDK 实现不规则窗口介绍

如何创建不规则形状的qml组件

R:具有可调节窗口和步长的滚动窗口功能,用于不规则间隔的观察

QT笔记之不规则窗口的实现

不规则时间序列上的滚动窗口

Qml 透明窗口,设置不规则等透明窗口,鼠标可穿透到桌面