⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cusimage.java

📁 Java画板
💻 JAVA
字号:
package test.paint;

import java.io.IOException;
import java.io.Serializable;
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.MouseEvent;

/**
 * CusImage类,用于保存图象类,实现了MyShape,Serializable,Transferable接口。
 * 作者:曾燕秋
 * 初始时间:2007 5-17
 * 最后一次修改时间:2007 6-17
 */

public class CusImage implements MyShape,Serializable,Transferable{
  private Color col=Color.black;
  transient private BufferedImage img0=null,img1=null;
  private int x=0,y=0;
  private double zoomSize=1;
  private int [] pixel;
  private int width=0,height=0;
  /**
   * 构造函数
   * @param img Image
   * @param x int
   * @param y int
   */
  public CusImage(Image img,int x,int y)
  {
    width=img.getWidth(null);
    height=img.getHeight(null);
    img0=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
    img1=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
    img0.createGraphics().drawImage(img,0,0,width,height,null);
    img1.createGraphics().drawImage(img,0,0,width,height,null);
    this.x=x;
    this.y=y;
  }
  /**
   * 构造函数
   * @param img BufferedImage
   * @param x int
   * @param y int
   */
  public CusImage(BufferedImage img ,int x,int y)
  {
    width=img.getWidth(null);
    height=img.getHeight(null);
    img0=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
    img1=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
    img0.createGraphics().drawImage(img,0,0,width,height,null);
    img1.createGraphics().drawImage(img,0,0,width,height,null);
    this.x=x;
    this.y=y;
  }
  
  /**
   * 判断是否和x,y,w,h够成的矩形相交
   * @param x double
   * @param y double
   * @param w double
   * @param h double
   * @return boolean
   */
  public boolean intersects(double x,double y,double w,double h)
  {
    Rectangle2D rec2D=new Rectangle2D.Float(this.x,this.y,(int)(img1.getWidth()*zoomSize),(int)(img1.getHeight()*zoomSize));
    return rec2D.intersects(x,y,w,h);
  }
  /**
   * 得到边界
   * @return Rectangle
   */
  public Rectangle getBounds()
  {
    return new Rectangle(this.x,this.y,(int)(img1.getWidth()*zoomSize),(int)(img1.getHeight()*zoomSize));
  }
  /**
   * 设置位置
   * @param x int
   * @param y int
   */
  public void setLocation(int x,int y)
  {
    this.x=x;
    this.y=y;
  }
  /**
   * 设置颜色
   * @param col Color
   */
  public void setColor(Color col)
  {
    this.col=col;
  }
  /**
   * 得到颜色
   * @return Color
   */
  public Color getColor()
  {
    return col;
  }
  /**
   * 绘制方法
   * @param g Graphics
   */
  public void draw(Graphics2D g)
  {
    
    g.setColor(col);
    g.drawImage(img1,x,y,(int)(img1.getWidth()*zoomSize),(int)(img1.getHeight()*zoomSize),null);
  }
  /**
   * 设置填充,无意义
   * @param isFill boolean
   */
  public void setIsFill(boolean isFill)
  {
  }
  /**
   * 得到是否填充,无意义
   * @return boolean
   */
  public boolean getIsFill()
  {
    return false;
  }
  /**
   * 设置线型,无意义
   * @param aStroke BasicStroke
   */
  public void setStroke(BasicStroke aStroke)
  {

  }
 
  /**
   * 判断是否为一个图象
   * @return boolean
   */
  public boolean isImage()
  {
    return true;
  }

  /**
   * 保存图象
   */
  public void setSaveImage()
  {
    pixel=new int[(img1.getWidth()+1)*(img1.getHeight()+1)];
    pixel=img1.getRGB(0,0,img1.getWidth(),img1.getHeight(),pixel,0,img1.getWidth()+1);
  }
  /**
   * 设置图象
   */
  public void setOpenImage()
  {
    img0=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
    img1=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
    img0.setRGB(0,0,width,height,pixel,0,width+1);
    img1.setRGB(0,0,width,height,pixel,0,width+1);
  }
  /**
   * 得到传输数据
   * @param flavor DataFlavor
   * @return Object
   */
  public Object getTransferData(DataFlavor flavor)
  {
    if(flavor==DataFlavor.imageFlavor||flavor==MyShape.CUSSHAPEFLAVOR)
      {
        BufferedImage bimg=new BufferedImage(img1.getWidth(),img1.getHeight(),BufferedImage.TYPE_INT_RGB);
        bimg.createGraphics().drawImage(img1,0,0,null);
        return bimg;
      }
      else
      {
          return null;
      }
  }
  /**
   * 得到有效的DataFlavor
   * @return DataFlavor[]
   */
  public DataFlavor [] getTransferDataFlavors()
  {
    DataFlavor [] df=new DataFlavor[2];
    df[0]=DataFlavor.imageFlavor;
    df[1]=MyShape.CUSSHAPEFLAVOR;
    return df;
  }
  /**
   * 判断flavor是否有效
   * @param flavor DataFlavor
   * @return boolean
   */
  public boolean isDataFlavorSupported(DataFlavor flavor)
  {
    if(flavor==DataFlavor.imageFlavor||flavor==MyShape.CUSSHAPEFLAVOR)
    {
      return true;
    }
    else
    {
        return false;
    }
  }
  public String getShapeData()
  {
	 return " "; 
  }
  
  public void setShapeData(String data) throws Exception
  {
	  
  }
  public void processCursorEvent(MouseEvent evt, int type)
  {
	  
  }
public int getX() {
	// TODO Auto-generated method stub
	return 0;
}
public int getY() {
	// TODO Auto-generated method stub
	return 0;
}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -