`
longhua2003
  • 浏览: 13882 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

Java图片透明-很有意思

    博客分类:
  • Java
阅读更多
   先声明一下,以下的代码不是我本人写的,我只是在这个基础上小修了一把:
package com.dlh;

import java.awt.Graphics;
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.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.JFrame;

//import com.sun.awt.AWTUtilities;
import com.sun.jna.platform.*;
import com.sun.jna.win32.*;


public class IrregularFormSample extends JFrame {

  private static final long serialVersionUID = 1L;
  private Point origin;
  private Image img;

  public IrregularFormSample() {
    super();
    MediaTracker mt=new MediaTracker(this); //dog.png,Image0.jpg
    img=Toolkit.getDefaultToolkit().createImage("image//dog.png");
    mt.addImage(img, 0);
    try {
      mt.waitForAll();
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
   
    try {
      initialize();
    } catch (IOException e) {
      e.printStackTrace();
    }   
  }

 
  private void initialize() throws IOException {
    this.setSize(img.getWidth(null), img.getHeight(null));
    this.setUndecorated(true);
    this.origin=new Point();
   

    WindowUtils.setWindowMask(this, getImageShape(img));
    //AWTUtilities.setWindowShape (this, getImageShape(img));
    //AWTUtilities.setWindowOpacity(this, 0.8f);
   
    this.setLocationRelativeTo(null);
    addMouseListener(
        new MouseAdapter(){
          public void mousePressed(MouseEvent e){
            origin.x = e.getX();
            origin.y = e.getY();
          }
         
          public void mouseClicked(MouseEvent e) {
            if(e.getButton()==MouseEvent.BUTTON3)
              System.exit(0);
          }
          public void mouseReleased(MouseEvent e) {
            super.mouseReleased(e);
          }
          @Override
          public void mouseEntered(MouseEvent e) {
            repaint();             
          }           
        }
    );

    addMouseMotionListener(
        new MouseMotionAdapter(){
          public void mouseDragged(MouseEvent e){
            Point p =    getLocation();
            setLocation(p.x + e.getX() - origin.x, p.y + e.getY() - origin.y );
          }         
        }
    );   
  }

 

  public Shape getImageShape(Image img) {
    ArrayList<Integer> x=new ArrayList<Integer>();
    ArrayList<Integer> y=new ArrayList<Integer>();   
    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){
        continue;       
      }else{
        x.add(i%width>0 ? i%width-1:0);
        y.add(i%width==0 ? (i==0 ? 0:i/width-1):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[y.get(c)][x.get(c)]=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;
  }
 
 

  @Override
  public void paint(Graphics g) {
    super.paint(g);
    g.drawImage(img, 0, 0, null);
  }
 
  public static void main(String[] args) {
        IrregularFormSample sample = new IrregularFormSample();
        sample.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        sample.setVisible(true);
  } 
}

要引入附件两个包,这个就可以跨平台了(我也只是听说,你可以验证一下),如果用jdk中com.sun.awt.AWTUtilities,在linux下是不行的。

看一下,这个狗有趣吧



  • 大小: 182.3 KB
  • 大小: 171.6 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics