?? cusimage.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 + -