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

  1.  
  2. package como.commlet.draw;
  3.  
  4. import como.io.*;
  5. import como.util.*;
  6. import java.awt.*;
  7. import java.io.*;
  8. import java.util.*; import java.lang.*;
  9.  
  10. /******
  11. * GOPolygon
  12. */
  13. public class GORect extends GraphicsObject
  14. {
  15.     int width;
  16.     int height;
  17.  
  18.     public GORect()
  19.     {
  20.         super();
  21.     }        
  22.     public GraphicsObject getNew ()  {
  23.         GORect go = new GORect();
  24.         go.color = color;
  25.         go.name = name;
  26.         go.x = x;
  27.         go.y = y;
  28.         go.width = 0;
  29.         go.height = 0;
  30.         go.filled = filled;
  31.         return go;
  32.     }
  33.  
  34.     public void draw(Graphics g,Color c)
  35.     {
  36.         lastgraphics = g;
  37.         g.setColor(c);
  38.         if ((width !=0) && (height !=0)) {
  39.             int x0,y0,w,h;
  40.             if (width > 0) {
  41.                 x0 = x; 
  42.                 w = width; 
  43.             } 
  44.             else { 
  45.                 x0 = x + width; 
  46.                 w = -width; 
  47.             }
  48.             if (height > 0) {
  49.                 y0 = y; 
  50.                 h = height; 
  51.             } 
  52.             else {
  53.                 y0 = y + height; 
  54.                 h = -height; 
  55.             }
  56.             if (filled) g.fillRect(x0,y0,w,h); 
  57.             else g.drawRect(x0,y0,w,h);
  58.         }
  59.     }        
  60.     public boolean handleEvent(Event evt)
  61.     {
  62.         if (evt.id == Event.MOUSE_DRAG)
  63.         {
  64.             height = evt.y - y;
  65.             width = evt.x - x;
  66.             if (evt.metaDown()) {
  67.                 if (height > width) height = width;
  68.                 else width = height;
  69.             }
  70.             if (evt.target instanceof DrawCanvas) ((DrawCanvas)evt.target).repaint();
  71.             return true;
  72.         }
  73.         else if (evt.id == Event.MOUSE_UP)
  74.         {
  75.             evt.id = DrawCanvas.EVENT_ACCEPT;
  76.             return true;
  77.         }
  78.         else if (evt.id == Event.MOUSE_DOWN)
  79.         {
  80.             x = evt.x;
  81.             y = evt.y;
  82.             return true;
  83.         }
  84.         return false;
  85.     }
  86.     public String toString() {
  87.     return super.toString() + "Width = "+width+"Height = "+height;
  88.     }
  89.     public void save(ObjectOutputStream s) throws IOException {
  90.         super.save(s);
  91.         s.writeInt(width);
  92.         s.writeInt(height);
  93.     }
  94.     public void load(ObjectInputStream s) throws IOException {
  95.         super.load(s);
  96.         width = s.readInt();
  97.         height = s.readInt();
  98.     }
  99. }
  100.