jfimage.java
来自「用Java开发的、实现类似Visio功能的应用程序源码」· Java 代码 · 共 674 行 · 第 1/2 页
JAVA
674 行
InputStream in = getClass().getResourceAsStream(imageFile);
if (in == null) {
m_lastError ="Image file: "+imageFile+" not found!";
System.err.println(m_lastError);
return false;
}
m_buffer = new byte[in.available()];
in.read(m_buffer);
in.close();
return loadImageFromBuffer();
}
catch (Exception e) {
m_lastError =e.getMessage();
System.out.println("Error: "+m_lastError);
return true;
}
*/
}
/**
* Load an image file to this object.
*
* @param File A binary image file.
*
*/
public boolean loadImage(File imageFile){
try{
FileInputStream stream=new FileInputStream(imageFile);
m_buffer =new byte[stream.available()];
stream.read(m_buffer);
stream.close();
stream =null;
return loadImageFromBuffer();
}catch(Exception e){
m_lastError =e.getMessage();
m_logger.error("loadImage,File: "+e);
return false;
}
}
/** load image from buffer */
private boolean loadImageFromBuffer(){
try{
if (m_canvas==null){
m_lastError ="loadImageFromBuffer : unassigned image observer!";
m_logger.error(m_lastError);
return false;
}
m_image = null;
m_image = Toolkit.getDefaultToolkit().createImage(m_buffer);
//Awaiting for completly loaded image.
MediaTracker tracker = new MediaTracker(m_canvas);
tracker.addImage(m_image, 0);
tracker.waitForID(0);
return true;
}catch(Exception e){
m_lastError =e.getMessage();
m_logger.error("loadImageFromBuffer: "+e);
return false;
}
}
/** finish loading image
* for loading an image from a buffer, we need to call this method later after loading the binary data
* into buffer.
*/
public boolean finishLoading(Component canvas){
m_canvas =canvas;
return loadImageFromBuffer();
}
/**
* Set the intial position of this internal label.
*
*/
protected void initLabel(){
m_label.setParent(this);
Rect rect=getBounds();
JFPoint pos =rect.getCenter();
pos.setY(pos.getY()-rect.getHeight()/2-GeomConst.PICK_OFFSET*5);
m_label.setLabelPoint(pos);
}
/**
* Creates a new AbstractObject of the same class and with the same contents as this object.
* This method implements the method defined in AbstractObject.
*
* @return A clone of this class.
*
*/
protected AbstractObject cloneMe() throws CloneNotSupportedException{
return new JFImage();
}
/**
* Creates a new object of the same class and with the same contents as this object.
*
* @return A clone of this instance.
*
*/
public Object clone() throws CloneNotSupportedException{
try{
Object obj =super.clone();
if (obj==null){
return null;
}
JFImage img=(JFImage) obj;
img.m_canvas =m_canvas;
img.m_buffer =new byte[m_buffer.length];
System.arraycopy(m_buffer,0,img.m_buffer,0,m_buffer.length);
img.loadImageFromBuffer();
return img;
}catch(Exception e){
throw new CloneNotSupportedException(e.getMessage());
}
}
/**
* Returns the hashcode for this Object.
*
* @return hash code for this Point2D.
*
*/
public int hashCode(){
return super.hashCode() ^ CollectionUtil.getHashCode(m_buffer);
}
/**
* Determines whether or not two objects are equal.
*
* @param obj an object to be compared with this object
*
* @return true if the object to be compared is an instance of Port and has the same values; false otherwise.
*
*/
public boolean equals(Object obj){
if (!super.equals(obj))
return false;
if (obj == this)
return true;
if (!(obj instanceof JFImage))
return false;
JFImage img= (JFImage)obj;
return CollectionUtil.compareArrays(m_buffer,img.m_buffer);
}
/**
* Append necessary xml child for current element,
* this method will be called internally by toDOM.
*
* @param element A XML element to append child xml nodes
*
* @param version A file version notification so this object can obey the rules to save data.
*/
protected void appendChildToDOM(Element element,JFVersion version){
if (element==null)
return;
super.appendChildToDOM(element,version);
element.addChild(new Element(XML_IMAGEDATA, Base64.encodeBytes(m_buffer)));
}
/**
* Extract needed xml child from current element,
* this method will be called internally by fromDOM.
*
* @param element An element used to extract needed xml child
* @param version A file version notification so this object can obey the rules to fetch data.
*
*/
protected void extractChildFromDOM(Element element,JFVersion version){
if (element==null)
return;
super.extractChildFromDOM(element,version);
m_buffer =Base64.decode(Element.getStringValue(element.getChild(XML_IMAGEDATA)));
if (m_buffer==null){
m_logger.error("extractChildFromDOM: null image data!");
}
//here we have not load the image and create a Image object completely,
//we need to call a finishLoading method later.
}
/**
* Save this object to a binary stream
*
* @param stream An binary output stream
*
* @param version A file version notification so this object can obey the rules to save data.
* @exception java.io.IOException
*
*/
public void saveToStream(com.jfimagine.utils.io.JFWriter stream,JFVersion version) throws IOException{
super.saveToStream(stream,version);
int bufSize =m_buffer.length;
//save length of image buffer
stream.writeInt(bufSize);
if (bufSize>0){
stream.write(m_buffer,0,bufSize);
}
}
/**
* Load object data from a binary stream <br>
*
* @param stream An binary input stream
*
* @param skipHead Skip head 'TYPE' check, an shape object should always
* has its own shape-type stored, if this shape-type has already been readed,
* this loadFromStream should/could not read the type anymore.
*
* @param version A file version notification so this object can obey the rules to fetch data.
* @exception java.io.IOException
*
*/
public void loadFromStream(com.jfimagine.utils.io.JFReader stream,boolean skipHead,JFVersion version) throws IOException{
super.loadFromStream(stream,skipHead,version);
//load the length of image buffer first
int bufSize =stream.readInt();
//load the image data
if (bufSize>0){
m_buffer =new byte[bufSize];
stream.read(m_buffer,0,bufSize);
//here we have not load the image and create a Image object completely,
//we need to call a finishLoading method later.
}
}
/**
* for loading an image, we need to call this finish loading image method to completely create a new image.
*
* @param objList An objectList that contains shapes.
* @param canvas A component for creating an image object.
*/
public static boolean finishLoadingImages(ObjectList objList,java.awt.Component canvas){
if (objList==null || objList.size()==0 || canvas==null)
return false;
Iterator it =objList.getList().iterator();
while (it!=null && it.hasNext()){
AbstractObject obj =(AbstractObject)it.next();
finishLoadingImages(obj,canvas);
}
return true;
}
/**
* for loading an image, we need to call this finish loading image method to completely create a new image.
*
* @param obj An Abstract object.
* @param canvas A component for creating an image object.
*/
public static boolean finishLoadingImages(AbstractObject obj,java.awt.Component canvas){
if (obj==null || canvas==null)
return false;
if (obj instanceof JFPage){
JFPage page =(JFPage)obj;
return page.finishLoadingImages(canvas);
}
try{
AbstractShape aobj =(AbstractShape)obj;
if (aobj instanceof JFImage){
JFImage img =(JFImage)aobj;
img.finishLoading(canvas);
}else if (aobj instanceof JFGroup){
JFGroup g =(JFGroup)aobj;
ObjectList l =g.getList();
finishLoadingImages(l,canvas);
}
return true;
}catch(Exception e){
m_logger.error("finishLoadingImages: "+e);
return false;
}
}
/**
* Free the Resource of current object used.<br>
*
* This is not trying to free the memory that current object used.
* It's only used to release some memory that the GC can not work with.
*
*/
public void freeResources(){
m_buffer =null;
m_image =null;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?