home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / in4wjcxu / src / como / commlet / draw / goimage.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  3.6 KB  |  180 lines

  1.  
  2. package como.commlet.draw;
  3.  
  4. import como.io.*;
  5. import como.util.*;
  6. import como.sys.*;
  7. import como.awt.*;
  8. import java.awt.*;
  9. import java.io.*;
  10. import java.util.*; 
  11. import java.lang.*;
  12.  
  13. /******
  14. * GOPolygon
  15. */
  16. public class GOImage extends GORect
  17. {
  18.  
  19.     String imagename;
  20.     Image image;
  21.  
  22.     Component canvas;
  23.     ComObj comobj;
  24.  
  25.     Frame fdFrame;
  26.     Panel proppanel;
  27.  
  28.     public GOImage()
  29.     {
  30.         super();
  31.         imagename = "defaultimg";
  32.         image = null;
  33.     }        
  34.     public void setData(ComObj c,Component d) {
  35.     // urgs.
  36.         canvas = d;
  37.         comobj = c;
  38.         if (image == null) image = c.loadImage(imagename);
  39.     }
  40.     public void setImage(String n) {
  41.         imagename = n;
  42.         image = comobj.loadImage(imagename);
  43.     }
  44.     public GraphicsObject getNew ()  {
  45.         GOImage go = new GOImage();
  46.         go.color = color;
  47.         go.name = name;
  48.         go.x = x;
  49.         go.y = y;
  50.         go.width = 0;
  51.         go.height = 0;
  52.         go.filled = filled;
  53.         go.imagename = imagename;
  54.         go.image = image;
  55.         go.canvas = canvas;
  56.         go.comobj = comobj;
  57.         go.fdFrame = fdFrame;
  58.         go.proppanel = proppanel;
  59.         return go;
  60.     }
  61.         public void showFD() {
  62.     if (fdFrame == null) {
  63.         RemoteFileDialog rfd = new RemoteFileDialog( comobj, "Draw.images", proppanel );
  64.         fdFrame = new Frame();
  65.         fdFrame.setTitle("Choose an image to paste");
  66.         fdFrame.add("Center",rfd);
  67.         fdFrame.pack();
  68.         fdFrame.show();
  69.     }
  70.     }
  71.     public void updatePropPanel() {
  72.         if ((proppanel != null) && (proppanel.getParent() != null)) {
  73.             proppanel.getParent().repaint();
  74.         }
  75.     }
  76.         public void layoutPropPanel(Panel p) {
  77.         proppanel = p;
  78.         p.removeAll();
  79.         p.setLayout(new BorderLayout());
  80.         p.add("Center",new Button("Choose Picture"));
  81.         updatePropPanel();
  82.         showFD();
  83.     }
  84.         public boolean handlePropPanelEvent(Event evt,DrawCanvas d) {
  85.         if  (evt.target instanceof Button) {
  86.             showFD();
  87.         }
  88.         else if  (evt.target instanceof RemoteFileDialog) {
  89.             String s = (String)evt.arg;
  90.             if (s != null) {
  91.                 imagename = s;
  92.                 setImage(s);
  93.             }
  94.             else {
  95.             fdFrame.hide();
  96.             fdFrame.dispose();
  97.             fdFrame = null;
  98.             }
  99.         }
  100.     return true;
  101.     }
  102.     public void draw(Graphics g,Color c)
  103.     {
  104.         lastgraphics = g;
  105.         g.setColor(c);
  106.         if ((width !=0) && (height !=0)) {
  107.             int x0,y0,w,h;
  108.             if (width > 0) {
  109.                 x0 = x; 
  110.                 w = width; 
  111.             } 
  112.             else { 
  113.                 x0 = x + width; 
  114.                 w = -width; 
  115.             }
  116.             if (height > 0) {
  117.                 y0 = y; 
  118.                 h = height; 
  119.             } 
  120.             else {
  121.                 y0 = y + height; 
  122.                 h = -height; 
  123.             }
  124.             if (image != null) {
  125.                 if (((Component.ALLBITS | Component.ERROR) & canvas.checkImage(image,w,h,canvas)) == 0) {
  126.                     g.drawRect(x0,y0,w,h);
  127.                 }
  128.                 g.drawImage(image,x0,y0,w,h,canvas);
  129.             }
  130.             else g.fillRect(x0,y0,w,h);
  131.         }
  132.     }        
  133.     public boolean handleEvent(Event evt)
  134.     {
  135.         if (evt.id == Event.MOUSE_DRAG)
  136.         {
  137.             height = evt.y - y;
  138.             width = evt.x - x;
  139.             if (evt.metaDown()) {
  140.                 if ((image != null) && (image.getHeight(canvas) != 0) && (image.getWidth(canvas) != 0))
  141.                     height = width * image.getHeight(canvas) / image.getWidth(canvas);
  142.             }
  143.             if (evt.target instanceof DrawCanvas) ((DrawCanvas)evt.target).repaint();
  144.             return true;
  145.         }
  146.         else if (evt.id == Event.MOUSE_UP)
  147.         {
  148.             evt.id = DrawCanvas.EVENT_ACCEPT;
  149.             return true;
  150.         }
  151.         else if (evt.id == Event.WINDOW_DESTROY)
  152.         {
  153.             if (fdFrame != null) {
  154.                 fdFrame.hide();
  155.                 fdFrame.dispose();
  156.                 fdFrame = null;
  157.             }
  158.             return true;
  159.         }
  160.         else if (evt.id == Event.MOUSE_DOWN)
  161.         {
  162.             x = evt.x;
  163.             y = evt.y;
  164.             return true;
  165.         }
  166.         return false;
  167.     }
  168.     public String toString() {
  169.     return super.toString() + " Name = "+imagename;
  170.     }
  171.     public void save(ObjectOutputStream s) throws IOException {
  172.         super.save(s);
  173.         s.writeString(imagename);
  174.     }
  175.     public void load(ObjectInputStream s) throws IOException {
  176.         super.load(s);
  177.         imagename = s.readString();
  178.     }
  179. }
  180.