Back to index

moin  1.9.0~rc2
ImageFigure.java
Go to the documentation of this file.
00001 /*
00002  * @(#)ImageFigure.java 5.1
00003  *
00004  */
00005 
00006 package CH.ifa.draw.figures;
00007 
00008 import java.awt.*;
00009 import java.io.*;
00010 import java.util.Vector;
00011 import java.awt.image.ImageObserver;
00012 import CH.ifa.draw.framework.*;
00013 import CH.ifa.draw.standard.*;
00014 import CH.ifa.draw.util.*;
00015 
00021 public  class ImageFigure
00022         extends AttributeFigure implements ImageObserver {
00023 
00024     private String   fFileName;
00025     private transient Image fImage;
00026     private Rectangle fDisplayBox;
00027     /*
00028      * Serialization support.
00029      */
00030     private static final long serialVersionUID = 148012030121282439L;
00031     private int imageFigureSerializedDataVersion = 1;
00032 
00033     public ImageFigure() {
00034         fFileName = null;
00035         fImage = null;
00036         fDisplayBox = null;
00037     }
00038 
00039     public ImageFigure(Image image, String fileName, Point origin) {
00040         fFileName = fileName;
00041         fImage = image;
00042         fDisplayBox = new Rectangle(origin.x, origin.y, 0, 0);
00043         fDisplayBox.width = fImage.getWidth(this);
00044         fDisplayBox.height = fImage.getHeight(this);
00045     }
00046 
00047     public void basicDisplayBox(Point origin, Point corner) {
00048         fDisplayBox = new Rectangle(origin);
00049         fDisplayBox.add(corner);
00050     }
00051 
00052     public Vector handles() {
00053         Vector handles = new Vector();
00054         BoxHandleKit.addHandles(this, handles);
00055         return handles;
00056     }
00057 
00058     public Rectangle displayBox() {
00059         return new Rectangle(
00060             fDisplayBox.x,
00061             fDisplayBox.y,
00062             fDisplayBox.width,
00063             fDisplayBox.height);
00064     }
00065 
00066     protected void basicMoveBy(int x, int y) {
00067         fDisplayBox.translate(x,y);
00068     }
00069 
00070     public void draw(Graphics g) {
00071         if (fImage == null)
00072             fImage = Iconkit.instance().getImage(fFileName);
00073         if (fImage != null)
00074             g.drawImage(fImage, fDisplayBox.x, fDisplayBox.y, fDisplayBox.width, fDisplayBox.height, this);
00075         else
00076             drawGhost(g);
00077     }
00078 
00079     private void drawGhost(Graphics g) {
00080         g.setColor(Color.gray);
00081         g.fillRect(fDisplayBox.x, fDisplayBox.y, fDisplayBox.width, fDisplayBox.height);
00082     }
00083 
00087     public boolean imageUpdate(Image img, int flags, int x, int y, int w, int h) {
00088            if ((flags & (FRAMEBITS|ALLBITS)) != 0) {
00089                invalidate();
00090                if (listener() != null)
00091                    listener().figureRequestUpdate(new FigureChangeEvent(this));
00092            }
00093            return (flags & (ALLBITS|ABORT)) == 0;
00094     }
00095 
00100     public void write(StorableOutput dw) {
00101         super.write(dw);
00102         dw.writeInt(fDisplayBox.x);
00103         dw.writeInt(fDisplayBox.y);
00104         dw.writeInt(fDisplayBox.width);
00105         dw.writeInt(fDisplayBox.height);
00106         dw.writeString(fFileName);
00107     }
00108 
00114     public void read(StorableInput dr) throws IOException {
00115         super.read(dr);
00116         fDisplayBox = new Rectangle(
00117             dr.readInt(),
00118             dr.readInt(),
00119             dr.readInt(),
00120             dr.readInt());
00121         fFileName = dr.readString();
00122         Iconkit.instance().registerImage(fFileName);
00123     }
00124 
00125     private void readObject(ObjectInputStream s)
00126         throws ClassNotFoundException, IOException {
00127 
00128         s.defaultReadObject();
00129         Iconkit.instance().registerImage(fFileName);
00130         fImage = null;
00131     }
00132 }