home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / prmjatgt / workpanel.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  29.9 KB  |  1,278 lines

  1. import java.awt.*;
  2. import java.applet.*;
  3. import java.util.*;
  4. import java.net.*;
  5. import java.io.*;
  6.  
  7. /**
  8.  * A class where draw the drawing
  9.  *
  10.  * @version     1.0 03/24/96
  11.  * @author     Kang, Dae Woong (namu@star.elim.net)
  12.  */
  13. class WorkPanel extends Panel// implements ImageObserver
  14. {
  15.     Image offImage; // for double buffering
  16.     Graphics offGraphics;
  17.     FileFrame fileFrame = null; // for select OBJECT
  18.  
  19.     static Applet applet;
  20.     static WorkPanel me = null;
  21.     URL urlDocument;
  22.     int writePort;
  23.  
  24.     int stateGrid = 0;
  25.     boolean isFirstPaint = false;
  26.  
  27.     //*************************  Vector's    ************************************************        
  28.     Vector drawVector = new Vector(); // draw saving area
  29.     Vector bufVector = new Vector(); // for select drawings, delelte, copy, paste
  30.     Vector objVector = new Vector(); // picture, draws vector
  31.     Vector logoVector = new Vector(); // for displaying logo
  32.     
  33.     //***************************************************************************************
  34.     boolean isDimensionSetted = false;
  35.     Dimension dimension;
  36.     Point ptScroll = new Point(0, 0);
  37.     String comment = new String();
  38.  
  39.     Draw curDraw;
  40.     Rectangle rcShift = new Rectangle();
  41.     
  42.     Point ptStart = new Point(0, 0);
  43.     boolean isFirst = false;
  44.     Tracker tracker = new Tracker();
  45.  
  46.     Label lbPosition  = new Label("x : 0000, y : 000");;
  47.     TextField tfInput = new TextField();
  48.  
  49.     //****************** constant *********************************************************************
  50.     static final int CHART_ID = 0x3140;
  51.     static final int CHART_VERSION = 0x0100;
  52.  
  53.     /**
  54.       * Constructor
  55.       */
  56.     public WorkPanel(Applet applet)
  57.     {
  58.         Draw draw;
  59.         Dimension dim = size();
  60.         dimension = new Dimension(dim.width, dim.height);
  61.  
  62.         this.applet = applet;
  63.         me = this;
  64.         urlDocument = applet.getDocumentBase();
  65.         String tmp = applet.getParameter("port");
  66.         writePort = (tmp == null) ? 3140 : Integer.parseInt(tmp);
  67.         if (writePort == 0)
  68.             writePort = 3140;
  69.         
  70.  
  71.         fileFrame = new FileFrame(applet, this, urlDocument);
  72.         fileFrame.pack();
  73.  
  74.         setBackground(Color.lightGray);
  75.         
  76.         
  77.         // add logoVector
  78.         draw = new Draw(Draw.OVAL, Draw.CONTENT_FILL, Draw.SHAPE_UP, Color.lightGray, 0, 0, 1040, 760);
  79.         logoVector.addElement(draw);
  80.         draw = new Draw(Draw.OVAL, Draw.CONTENT_UNFILL, Draw.SHAPE_DOWN, Color.lightGray, 80, 80, 880, 600);
  81.         logoVector.addElement(draw);
  82.         draw = new Draw(Draw.STRING, Draw.CONTENT_FILL, Draw.SHAPE_UP, Color.red, 0, 220, 0, 130);
  83.         draw.text = "Web Draw 1.0";
  84.         draw.fontName = "TimesRoman";
  85.         draw.fontStyle = Font.BOLD;
  86.         draw.width = getStringWidth(draw);
  87.         draw.x = (1040 - draw.width)/2;
  88.         logoVector.addElement(draw);
  89.         
  90.         draw = new Draw(Draw.STRING, Draw.CONTENT_FILL, Draw.SHAPE_PLAIN, Color.black, 0, 360, 0, 60);
  91.         draw.text = "96.3.24";
  92.         draw.fontName = "TimesRoman";
  93.         draw.fontStyle = Font.PLAIN;
  94.         draw.width = getStringWidth(draw);
  95.         draw.x = (1040 - draw.width)/2;
  96.         logoVector.addElement(draw);
  97.  
  98.         draw = new Draw(Draw.STRING, Draw.CONTENT_FILL, Draw.SHAPE_UP, Color.orange, 0, 420, 0, 70);
  99.         draw.text = "Author : Kang, Daewoong";
  100.         draw.fontName = "TimesRoman";
  101.         draw.fontStyle = Font.PLAIN;
  102.         draw.width = getStringWidth(draw);
  103.         draw.x = (1040 - draw.width)/2;
  104.         logoVector.addElement(draw);
  105.  
  106.         draw = new Draw(Draw.STRING, Draw.CONTENT_FILL, Draw.SHAPE_PLAIN, Color.black, 0, 510, 0, 60);
  107.         draw.text = "namu@star.elim.net";
  108.         draw.fontName = "TimesRoman";
  109.         draw.fontStyle = Font.PLAIN;
  110.         draw.width = getStringWidth(draw);
  111.         draw.x = (1040 - draw.width)/2;
  112.         logoVector.addElement(draw);
  113.     }
  114.  
  115.     //****************** set method *********************************************************
  116.  
  117.     public void setColor(Color color)
  118.     {
  119.         Draw.curColor = color;
  120.  
  121.         if (Draw.curMethod == Draw.SELECT && bufVector.size() > 0)
  122.         {
  123.             Rectangle clip = tracker.getClipRect();
  124.             Enumeration enum = bufVector.elements();
  125.             Draw dd;
  126.             while (enum.hasMoreElements())
  127.             {
  128.                 dd = (Draw) enum.nextElement();
  129.                 clip = clip.union(dd.getClipRect());
  130.                 dd.color = new Color(color.hashCode());
  131.             }
  132.             repaint(clip);
  133.         }
  134.     }
  135.  
  136.     public void setMethod(int method)
  137.     {
  138.         if ( (Draw.curMethod == Draw.STRING || Draw.curMethod == Draw.SELECT || Draw.curMethod == Draw.ANCHOR) && tfInput.isVisible())
  139.             endString();
  140.         if (Draw.curMethod == Draw.SELECT && bufVector.size() > 0)
  141.             unselect(null);
  142.  
  143.         if (Draw.curMethod == Draw.IMAGE || Draw.curMethod == Draw.DRAW)
  144.             fileFrame.hide();
  145.  
  146.         if (Draw.curMethod == Draw.ANCHOR)
  147.             repaint();
  148.         Draw.curMethod = method;
  149.         isFirst = true;
  150.  
  151.         if (Draw.curMethod == Draw.ANCHOR)
  152.             repaint();
  153.  
  154.         switch (Draw.curMethod)
  155.         {
  156.         case Draw.IMAGE: fileFrame.refImage(); break;
  157.         case Draw.DRAW: fileFrame.refDraw(); break;
  158.         }
  159.     }
  160.  
  161.     public void setShape(int shape)
  162.     {
  163.         Draw.curShape = shape;
  164.         isFirst = true;
  165.  
  166.         if (Draw.curMethod == Draw.SELECT && bufVector.size() > 0)
  167.         {
  168.             Rectangle clip = tracker.getClipRect();
  169.             Enumeration enum = bufVector.elements();
  170.             Draw dd;
  171.             while (enum.hasMoreElements())
  172.             {
  173.                 dd = (Draw) enum.nextElement();
  174.                 clip = clip.union(dd.getClipRect());
  175.                 dd.shape = shape;
  176.             }
  177.             repaint(clip);
  178.         }
  179.     }
  180.  
  181.     public void setContent(int fill)
  182.     {
  183.         Draw.curContent = fill;
  184.         isFirst = true;
  185.         if (Draw.curMethod == Draw.SELECT && bufVector.size() > 0)
  186.         {
  187.             Rectangle clip = tracker.getClipRect();
  188.             Enumeration enum = bufVector.elements();
  189.             Draw dd;
  190.             while (enum.hasMoreElements())
  191.             {
  192.                 dd = (Draw) enum.nextElement(); 
  193.                 clip = clip.union(dd.getClipRect());
  194.                 dd.content = fill;
  195.             }
  196.             repaint(clip);
  197.         }
  198.     }
  199.  
  200.     public void setFontName(String fontName)
  201.     {
  202.         Draw.curFontName = fontName;
  203.         if (Draw.curMethod == Draw.SELECT && bufVector.size() > 0)
  204.         {
  205.             Rectangle clip = tracker.getClipRect();
  206.             Enumeration enum = bufVector.elements();
  207.             Draw dd;
  208.             while (enum.hasMoreElements())
  209.             {
  210.                 dd = (Draw) enum.nextElement();
  211.                 if (dd.hasText())
  212.                 {
  213.                     clip = clip.union(dd.getClipRect());
  214.                     dd.fontName = new String(fontName);
  215.                     dd.width = getStringWidth(dd);
  216.                     clip = clip.union(dd.getClipRect());
  217.                 }
  218.             }
  219.             clip = clip.union(tracker.getClipRect());
  220.             repaint(clip);
  221.         }
  222.     }
  223.  
  224.     public void setFontStyle(int style)
  225.     {
  226.         Draw.curFontStyle = style;
  227.         if (Draw.curMethod == Draw.SELECT && bufVector.size() > 0)
  228.         {
  229.             Rectangle clip = tracker.getClipRect();
  230.             Enumeration enum = bufVector.elements();
  231.             Draw dd;
  232.             while (enum.hasMoreElements())
  233.             {
  234.                 dd = (Draw) enum.nextElement();
  235.                 if (dd.hasText())
  236.                 {
  237.                     clip = clip.union(dd.getClipRect());
  238.                     dd.fontStyle = style;
  239.                     dd.width = getStringWidth(dd);
  240.                     clip = clip.union(dd.getClipRect());
  241.                 }
  242.             }
  243.             clip = clip.union(tracker.getClipRect());
  244.             repaint(clip);
  245.         }
  246.     }
  247.  
  248.     public void setSize(int wid, int hig, String comment)
  249.     {
  250.  
  251.         dimension.width = wid;
  252.         dimension.height = hig;
  253.         this.comment = comment;
  254.         isDimensionSetted = true;
  255.  
  256.         ((Panel)getParent()).layout();
  257.  
  258.         repaint();
  259.     }    
  260.     public void runCommand(int cmd)
  261.     {
  262.         if (Draw.curMethod == Draw.SELECT && bufVector.size() > 0)
  263.         {
  264.             Rectangle clip = tracker.getClipRect();
  265.             Draw dd;
  266.             int i, count;
  267.  
  268.             switch (cmd)
  269.             {
  270.             case RunCommandBar.COPY:
  271.                 i = 0;
  272.                 count = bufVector.size(); 
  273.                 while (i < count)
  274.                 {
  275.                     dd = (Draw) bufVector.elementAt(i);
  276.                     clip = clip.union(dd.getClipRect());
  277.  
  278.                     Draw clone = new Draw(dd);
  279.                     clone.transelate(10, 10);
  280.                     clone.isSelected = true;
  281.                     drawVector.addElement(clone);
  282.                     if (dd == tracker.draw)
  283.                     {
  284.                         tracker.setDraw(clone, 0, 0);
  285.                     }
  286.  
  287.                     dd.isSelected = false;
  288.                     bufVector.setElementAt(clone, i);
  289.  
  290.                     i++;
  291.                 }
  292.                 repaint(clip.x, clip.y, clip.width + 10, clip.height + 10);
  293.                 break;
  294.             case RunCommandBar.DELETE:
  295.                 i = 0;
  296.                 count = bufVector.size(); 
  297.                 tracker.stateSelect = tracker.OFF;
  298.                 while (i < count)
  299.                 {
  300.                     dd = (Draw) bufVector.elementAt(i);
  301.                     if (dd.method == Draw.IMAGE || dd.method == Draw.DRAW)
  302.                     {
  303.                         dd.objTank.refCount--;
  304.                         if (dd.objTank.refCount-- == 0)
  305.                         {
  306.                             objVector.removeElement(dd.objTank);
  307.                         }
  308.                     }
  309.                     clip = clip.union(dd.getClipRect());
  310.                     drawVector.removeElement(dd);
  311.  
  312.                     i++;
  313.                 }
  314.                 bufVector.removeAllElements();
  315.                 repaint(clip);
  316.                 break;
  317.             }
  318.         }
  319.  
  320.         switch (cmd)
  321.         {
  322.         case RunCommandBar.SET_SIZE:
  323.             SetSizeFrame frm = new SetSizeFrame(this, applet);
  324.             frm.tfWidth.setText(Integer.toString(dimension.width));
  325.             frm.tfHeight.setText(Integer.toString(dimension.height));
  326.             frm.tfComment.setText(comment);
  327.             frm.pack();
  328.             frm.show();
  329.             applet.disable();
  330.             break;
  331.         case RunCommandBar.GRID:
  332.             stateGrid = ++stateGrid % 3;
  333.             repaint();
  334.             break;
  335.         case RunCommandBar.WRITE_DRAW:
  336.             fileFrame.writeDraw();
  337.             break;
  338.         case RunCommandBar.READ_DRAW:
  339.             fileFrame.readDraw();
  340.             break;
  341.         case RunCommandBar.ALIGN_TOP:
  342.             if (bufVector.size() > 1)
  343.             {
  344.                 Enumeration enum = bufVector.elements();
  345.                 Rectangle clip = tracker.getClipRect();
  346.                 Draw dd;
  347.                 while (enum.hasMoreElements())
  348.                 {
  349.                     dd = (Draw) enum.nextElement();
  350.                     clip = clip.union(dd.getClipRect());
  351.                     dd.move(dd.x, tracker.draw.y);
  352.                     clip = clip.union(dd.getClipRect());
  353.                 }
  354.                 repaint(clip);
  355.             } 
  356.             break;
  357.         case RunCommandBar.ALIGN_LEFT:
  358.             if (bufVector.size() > 1)
  359.             {
  360.                 Enumeration enum = bufVector.elements();
  361.                 Rectangle clip = tracker.getClipRect();
  362.                 Draw dd;
  363.                 int val = tracker.draw.x;
  364.                 while (enum.hasMoreElements())
  365.                 {
  366.                     dd = (Draw) enum.nextElement();
  367.                     clip = clip.union(dd.getClipRect());
  368.                     dd.move(tracker.draw.x, dd.y);
  369.                     clip = clip.union(dd.getClipRect());
  370.                 }
  371.                 repaint(clip);
  372.             } 
  373.             break;
  374.         case RunCommandBar.ALIGN_BOTTOM:
  375.             if (bufVector.size() > 1)
  376.             {
  377.                 Enumeration enum = bufVector.elements();
  378.                 Rectangle clip = tracker.getClipRect();
  379.                 Draw dd;
  380.                 while (enum.hasMoreElements())
  381.                 {
  382.                     dd = (Draw) enum.nextElement();
  383.                     clip = clip.union(dd.getClipRect());
  384.                     dd.move(dd.x, tracker.draw.y + tracker.draw.height - dd.y);
  385.                     clip = clip.union(dd.getClipRect());
  386.                 }
  387.                 repaint(clip);
  388.             } 
  389.             break;
  390.         case RunCommandBar.ALIGN_RIGHT:
  391.             if (bufVector.size() > 1)
  392.             {
  393.                 Enumeration enum = bufVector.elements();
  394.                 Rectangle clip = tracker.getClipRect();
  395.                 Draw dd;
  396.                 while (enum.hasMoreElements())
  397.                 {
  398.                     dd = (Draw) enum.nextElement();
  399.                     clip = clip.union(dd.getClipRect());
  400.                     dd.move(tracker.draw.x + tracker.draw.width - dd.x, dd.y);
  401.                     clip = clip.union(dd.getClipRect());
  402.                 }
  403.                 repaint(clip);
  404.             } 
  405.             break;
  406.         case RunCommandBar.BRING_FRONT:
  407.             if (bufVector.size() == 1)
  408.             {
  409.                 Rectangle clip = tracker.getClipRect();
  410.                 Draw dd = new Draw(tracker.draw);
  411.  
  412.                 drawVector.removeElement(tracker.draw);
  413.                 drawVector.addElement(dd);
  414.  
  415.                 bufVector.removeElement(tracker.draw);
  416.                 bufVector.addElement(dd);
  417.  
  418.                 tracker.setDraw(dd, 0, 0);
  419.  
  420.                 repaint(clip);
  421.             }
  422.             break;
  423.         case RunCommandBar.BRING_BACK:
  424.             if (bufVector.size() == 1)
  425.             {
  426.                 Rectangle clip = tracker.getClipRect();
  427.                 Draw dd = new Draw(tracker.draw);
  428.  
  429.                 drawVector.removeElement(tracker.draw);
  430.                 drawVector.insertElementAt(dd, 0);
  431.  
  432.                 bufVector.removeElement(tracker.draw);
  433.                 bufVector.addElement(dd);
  434.  
  435.                 tracker.setDraw(dd, 0, 0);
  436.  
  437.                 repaint(clip);
  438.             }
  439.             break;
  440.         case RunCommandBar.REMOVE_ALL:
  441.             drawVector.removeAllElements();
  442.             bufVector.removeAllElements();
  443.             curDraw = null;
  444.             tracker.stateSelect = tracker.OFF;
  445.             repaint();
  446.             break;
  447.         case RunCommandBar.ABOUT_DRAW:
  448.             isFirstPaint = true;
  449.             repaint();
  450.             break;
  451.             
  452.         default:
  453.         }
  454.     }
  455.     //*************************** Event Handlere ********************************************
  456.  
  457.     public boolean handleEvent(Event e)
  458.     {
  459.         e.translate(ptScroll.x, ptScroll.y);
  460.  
  461.         return super.handleEvent(e);
  462.     }
  463.  
  464.     public boolean mouseDown(Event e, int x, int y)
  465.     {
  466.         if ((e.modifiers & 0x4) == 0x4) //right mouse button down
  467.         {
  468.             switch  (Draw.curMethod)    
  469.             {
  470.             case Draw.POLYGON:
  471.                 isFirst = true; // polygon end
  472.                 break;
  473.             case Draw.SELECT:
  474.                 Draw dd;
  475.                 int i = drawVector.size() - 1;
  476.                     
  477.                 while (i >= 0)
  478.                 {
  479.                     dd = (Draw) drawVector.elementAt(i);
  480.                     if (dd.method == Draw.STRING && dd.inside(x, y)) 
  481.                     {
  482.                         if (tfInput.isVisible())
  483.                             endString();
  484.                         curDraw = dd;
  485.                         tfInput.setText(curDraw.text);
  486.                         beginString();
  487.                         break;
  488.                     }
  489.                     i--;
  490.                 }
  491.                 break;                
  492.             }
  493.             return true;
  494.         }
  495.  
  496.         ptStart.x = x;
  497.         ptStart.y = y;
  498.         switch  (Draw.curMethod)
  499.         {
  500.         case Draw.POLYGON:
  501.             if (isFirst)
  502.             {
  503.                 drawVector.addElement(new Draw(x, y, 1, 1));
  504.                 curDraw = (Draw)drawVector.lastElement();
  505.                 curDraw.addParam(x, y, x, y);
  506.                 isFirst = false;
  507.             }
  508.             else
  509.             {
  510.                 curDraw.addParam(x, y);
  511.             }
  512.             break;
  513.         case Draw.STRING:
  514.             if (tfInput.isVisible())
  515.                 endString();
  516.  
  517.             tfInput.reshape(x  - ptScroll.x, y - ptScroll.y, 1, 1);
  518.             tfInput.setText("");
  519.             tfInput.show();
  520.  
  521.             drawVector.addElement(new Draw(x, y, 1, 1));
  522.             curDraw = (Draw)drawVector.lastElement();
  523.  
  524.             curDraw.fontName = new String(Draw.curFontName);
  525.             curDraw.fontStyle = Draw.curFontStyle;
  526.             break;
  527.         case Draw.SELECT:
  528.             if (e.shiftDown())
  529.             {
  530.                 rcShift.reshape(x, y, 0, 0);
  531.                 break;
  532.             }
  533.             Rectangle clip = null;
  534.             Rectangle rcPrev = tracker.getClipRect();
  535.             if (!tracker.isInTracker(x, y))
  536.             {
  537.                 Draw dd;
  538.                 int i = drawVector.size() - 1;
  539.                     
  540.                 while (i >= 0)
  541.                 {
  542.                     dd = (Draw) drawVector.elementAt(i);
  543.                     if (dd.inside(x, y)) 
  544.                     {
  545.                         if (e.controlDown())
  546.                         { 
  547.                             if (dd != curDraw)
  548.                             {
  549.                                 if (dd.isSelected)
  550.                                     clip = dd.getClipRect();
  551.  
  552.                                 dd.toggleSelect();
  553.  
  554.                                 if (dd.isSelected)
  555.                                 {
  556.                                     bufVector.addElement(dd);
  557.                                     if (bufVector.size() == 1)
  558.                                     {
  559.                                         tracker.setDraw(dd, x, y);
  560.                                         curDraw = dd;
  561.                                         clip = tracker.getClipRect();
  562.  
  563.                                     }
  564.                                     else
  565.                                     {
  566.                                         clip = dd.getClipRect();
  567.                                     }
  568.                                 }
  569.                                 else
  570.                                 {
  571.                                     bufVector.removeElement(dd);
  572.                                 }
  573.  
  574.                                 tracker.stateSelect = tracker.SELECT;
  575.                                 break;
  576.                             }
  577.                         }
  578.                         else
  579.                         {
  580.                             if (!dd.isSelected)
  581.                             {
  582.                                 unselect(rcPrev);
  583.                                 dd.isSelected = true;
  584.                                 bufVector.addElement(dd);
  585.                             }
  586.  
  587.                             tracker.setDraw(dd, x, y);
  588.                             curDraw = dd;
  589.  
  590.                             clip = tracker.getClipRect();
  591.                             break;
  592.                         }
  593.                     }
  594.                     i--;
  595.                 }
  596.  
  597.                 if (i < 0)
  598.                 {
  599.                     unselect(rcPrev);
  600.                 }
  601.                 else
  602.                 {
  603.                     if (!rcPrev.isEmpty())
  604.                         clip = clip.union(rcPrev);
  605.                     if (!clip.isEmpty())
  606.                         repaint(clip);
  607.                 }
  608.             }
  609.             break;
  610.         case Draw.ANCHOR:
  611.         {
  612.             Draw dd;
  613.             int i = drawVector.size() - 1;
  614.                 
  615.             while (i >= 0)
  616.             {
  617.                 dd = (Draw) drawVector.elementAt(i);
  618.                 if (dd.inside(x, y)) 
  619.                 {
  620.                     if (tfInput.isVisible())
  621.                         endString();
  622.                     curDraw = dd;
  623.                     beginString();
  624.                     break;
  625.                 }
  626.                 i--;
  627.             }
  628.         }
  629.             break;
  630.         default:
  631.             drawVector.addElement(new Draw(x, y, 1, 1));
  632.             curDraw = (Draw)drawVector.lastElement();
  633.             switch (Draw.curMethod)
  634.             {
  635.             case Draw.LINE:
  636.                 curDraw.addParam(x, y, x, y);
  637.                 isFirst = false;
  638.                 break;
  639.             case Draw.ROUND_RECT:
  640.                 curDraw.addParam(10, 10);
  641.                 break;
  642.             case Draw.PIE:
  643.             case Draw.ARC:
  644.                 curDraw.addParam(0, 270);
  645.                 break;
  646.             case Draw.IMAGE:
  647.             case Draw.DRAW:
  648.                 String path = fileFrame.getPath();
  649.                 if (path != null)
  650.                 {
  651.                     int count = objVector.size();
  652.                     boolean found = false;
  653.                     ObjTank obj;
  654.                     int i = 0;
  655.                     while ( i < count)
  656.                     {
  657.                         obj = (ObjTank)objVector.elementAt(i);
  658.                         if (obj.path.equals(path))
  659.                         {
  660.                             obj.refContent(curDraw);
  661.                             found = true;
  662.                             repaint(curDraw);
  663.                             break;
  664.                         }
  665.                         i++;
  666.                     }
  667.     
  668.                     if (!found)
  669.                     {
  670.                         if (Draw.curMethod == Draw.IMAGE)
  671.                         {
  672.                             obj = new ObjTank(path, true);
  673.  
  674.                             if (obj.setContent(applet))
  675.                             {
  676.                                 obj.refContent(curDraw);
  677.                                 objVector.addElement(obj);
  678.                                 repaint(curDraw);
  679.                             }
  680.                             else 
  681.                             {
  682.                                 drawVector.removeElement(curDraw);
  683.                                 curDraw = null;
  684.                             }
  685.                         }
  686.                         else //OBJECT
  687.                         {
  688.                             obj = new ObjTank(path, false);
  689.                             if (obj.setContent(applet))
  690.                             {
  691.                                 obj.refContent(curDraw);
  692.                                 objVector.addElement(obj);
  693.                                 repaint(curDraw);
  694.                             }
  695.                             else 
  696.                             {
  697.                                 drawVector.removeElement(curDraw);
  698.                                 curDraw = null;
  699.                             }
  700.                         }
  701.                     }
  702.                 }
  703.                 break;
  704.             }
  705.             break;
  706.         }
  707.             
  708.         return true;
  709.     }
  710.  
  711.  
  712.     public boolean mouseDrag(Event e, int x, int y)
  713.     {
  714.         if ((e.modifiers & 0x4) == 0x4 || curDraw == null) //right mouse button down
  715.             return true;
  716.         
  717.         switch (Draw.curMethod)
  718.         {
  719.         case Draw.SELECT:
  720.             if (e.shiftDown())
  721.             {
  722.                 Graphics g = getGraphics();
  723.                 g.setColor(Color.blue);
  724.                 g.setXORMode(Color.white);
  725.  
  726.                 g.drawRect(rcShift.x, rcShift.y, rcShift.width, rcShift.height);
  727.                 rcShift.reshape(Math.min(ptStart.x, x), Math.min(ptStart.y, y),
  728.                                 Math.abs(ptStart.x - x), Math.abs(ptStart.y - y));    
  729.                 g.drawRect(rcShift.x, rcShift.y, rcShift.width, rcShift.height);
  730.  
  731.                 g.dispose();
  732.                 break; 
  733.             }
  734.  
  735.             Rectangle clip = tracker.getClipRect();
  736.             if(tracker.drag(x, y))
  737.             {
  738.                 if (tracker.stateSelect == tracker.MOVE)
  739.                 {
  740.                         
  741.                     Enumeration enum = bufVector.elements();
  742.                     Draw dd;
  743.                     while (enum.hasMoreElements())
  744.                     {
  745.                         dd = (Draw) enum.nextElement();
  746.                         clip = clip.union(dd.getClipRect());
  747.                         dd.transelate(tracker.w, tracker.h);
  748.                         clip = clip.union(dd.getClipRect());
  749.                     }
  750.                     clip = clip.union(tracker.getClipRect());
  751.                     repaint(clip);
  752.                     break;
  753.                 }
  754.                 if (tracker.draw.method == Draw.STRING && tracker.draw.width < getStringWidth(tracker.draw))
  755.                 {
  756.                     tracker.draw.width = getStringWidth(tracker.draw);
  757.                 }
  758.                 clip = clip.union(tracker.getClipRect());
  759.  
  760.                 if (!clip.isEmpty())
  761.                     repaint(clip);
  762.             }
  763.             break;
  764.         case Draw.STRING:
  765.             setCurRect(x, y);
  766.             tfInput.reshape(curDraw.x - ptScroll.x, curDraw.y - ptScroll.y, curDraw.width, curDraw.height);
  767.             break;
  768.         case Draw.IMAGE:
  769.         case Draw.DRAW:
  770.         case Draw.ANCHOR:
  771.             break;
  772.         default:
  773.             Rectangle rc = curDraw.getClipRect();
  774.                     
  775.             if (curDraw.isPointsOnlyType())
  776.             {
  777.                 if  (!isFirst)
  778.                 {
  779.                     curDraw.setLastPoint(x, y);
  780.                     curDraw.setRect( curDraw.getPolygon().getBoundingBox() );
  781.  
  782.                     if (curDraw.method == Draw.LINE)
  783.                         curDraw.setLineAngle();
  784.                     if (curDraw.isEmpty())
  785.                         curDraw.grow((curDraw.width == 0) ? 2 : 0, (curDraw.height == 0) ? 2 : 0);
  786.                 }
  787.             }
  788.             else
  789.             {
  790.                 setCurRect(x, y);
  791.             }
  792.  
  793.             Rectangle clipRect = rc.union(curDraw.getClipRect());
  794.             clipRect.grow(2, 2);
  795.             repaint(clipRect);
  796.         }
  797.         return true;
  798.     }
  799.  
  800.     public boolean mouseUp(Event e, int x, int y)
  801.     {
  802.         switch (Draw.curMethod)
  803.         {
  804.         case Draw.SELECT:
  805.             if (!rcShift.isEmpty())
  806.             {
  807.                 Graphics g = getGraphics();
  808.                 g.setXORMode(Color.white);
  809.                 g.setColor(Color.blue);
  810.  
  811.                 g.drawRect(rcShift.x, rcShift.y, rcShift.width, rcShift.height);
  812.  
  813.                 g.dispose();
  814.  
  815.                 Enumeration enum = drawVector.elements();
  816.                 Draw dd;
  817.                 boolean check = false;        
  818.                 while (enum.hasMoreElements())
  819.                 {
  820.                     dd = (Draw) enum.nextElement();
  821.                     if (dd.equals( rcShift.intersection(dd) ) && !dd.isSelected)
  822.                     {            
  823.                         dd.isSelected = true;
  824.                         bufVector.addElement(dd);    
  825.                         check = true;
  826.                     }
  827.                 }
  828.                 if (check)
  829.                 {
  830.                     rcShift.grow(2, 2);
  831.                     if (tracker.stateSelect == tracker.OFF)
  832.                     {
  833.                         tracker.setDraw((Draw)bufVector.firstElement(), -1, -1);
  834.                         rcShift = rcShift.union(tracker.getClipRect());
  835.                     }
  836.                     repaint(rcShift);        
  837.                 }
  838.                 
  839.                 rcShift.reshape(-1, -1, 0, 0);
  840.             }
  841.             break; 
  842.         case Draw.STRING:
  843.             beginString();
  844.             break;
  845.         }
  846.         return true;
  847.     }
  848.  
  849.     public boolean mouseMove(Event e, int x, int y)
  850.     {
  851.         lbPosition.setText("x : " + x + ", y : " + y);
  852.         return true;
  853.     }
  854.         
  855.     public boolean keyDown(Event e, int key)
  856.     {
  857.         if (e.target == tfInput)
  858.         {
  859.             if (key == 27 || key == 10)         // esc key or enter key
  860.             {
  861.                 endString();
  862.                 return true;
  863.             }
  864.         }
  865.         return false;
  866.     }
  867.  
  868.     public boolean keyUp(Event e, int key)
  869.     {
  870.         if (e.target == tfInput)
  871.         {
  872.             if (!(key == 27 || key == 10)) // esc key or enter key
  873.             {
  874.                 if (Draw.curMethod == Draw.ANCHOR)
  875.                     curDraw.urlName = new String(tfInput.getText());
  876.                 else
  877.                     curDraw.text = new String(tfInput.getText());
  878.             }
  879.         }
  880.             
  881.         return false;
  882.     }
  883.     
  884.     //*********************** read/ write ****************************************************************
  885.  
  886.     public boolean writeDraw(String dir, String file)
  887.     {
  888.         Socket sock = null;
  889.         if (file.indexOf(".draw") == -1)
  890.         {
  891.             file += ".draw";
  892.         }
  893.  
  894.         try
  895.         {
  896.             Enumeration enum;
  897.  
  898.             sock = new Socket(urlDocument.getHost(), writePort);
  899.             DataOutputStream dos = new DataOutputStream(sock.getOutputStream());
  900.  
  901.             dos.writeBytes(dir);
  902.             dos.writeByte(0); // directory end mark
  903.             dos.writeBytes(file);
  904.             dos.writeByte(-1); //file name end mark
  905.  
  906.             dos.writeShort(CHART_ID);    
  907.             dos.writeShort(CHART_VERSION); 
  908.             dos.writeByte(comment.length());
  909.             dos.writeBytes(comment);
  910.  
  911.             dos.writeShort(dimension.width);
  912.             dos.writeShort(dimension.height);
  913.  
  914.             dos.writeShort(objVector.size());
  915.             enum = objVector.elements();
  916.             while (enum.hasMoreElements())
  917.                 ((ObjTank) enum.nextElement()).write(dos);
  918.             
  919.             dos.writeShort(drawVector.size()); // draws count
  920.             enum = drawVector.elements();
  921.             while (enum.hasMoreElements())
  922.                 ((Draw) enum.nextElement()).write(dos, objVector);
  923.  
  924.             dos.flush();
  925.             sock.close();
  926.             sock = null;
  927.         }
  928.         catch (SocketException soe) 
  929.         {
  930.             System.err.println("server not response");
  931.         }
  932.         catch (IOException ioeWrite)
  933.         {
  934.             if (sock != null)
  935.             {
  936.                 try 
  937.                     sock.close();
  938.                 catch (IOException ioe2)
  939.                     System.err.println("close error" + ioe2);
  940.             }
  941.             System.err.println(ioeWrite);
  942.             return false;
  943.         }
  944.         return true;
  945.     }
  946.  
  947.     public void readDraw(String nmFile)
  948.     {
  949.         URL  url = null;
  950.         try
  951.         {
  952.             url =  new URL(urlDocument, nmFile);
  953.         }
  954.         catch (MalformedURLException e)
  955.         {
  956.             System.err.println(nmFile + " document not found" + e);
  957.             return;
  958.         }
  959.  
  960.         try
  961.         {
  962.             DataInputStream dis = new DataInputStream(url.openStream());
  963.  
  964.             int count;
  965.             int str_len; // string len
  966.             byte[] str_byte; // string buf
  967.  
  968.             unselect(null);
  969.  
  970.             bufVector.removeAllElements();
  971.             tracker.stateSelect = tracker.OFF;
  972.             curDraw = null;
  973.  
  974.             if (CHART_ID !=  dis.readShort())
  975.                 throw (new IOException("This is not draw file"));
  976.  
  977.             if (CHART_VERSION !=  dis.readShort())
  978.                 throw (new IOException("This is invalid version draw file"));
  979.  
  980.             str_len = dis.readByte();
  981.             str_byte = new byte[str_len];
  982.             dis.readFully(str_byte);
  983.             comment = new String(str_byte, 0);
  984.  
  985.             dimension.width = dis.readShort();
  986.             dimension.height = dis.readShort();
  987.         
  988.             objVector.removeAllElements();
  989.             count = dis.readShort();
  990.             for (int i = 0 ; i < count; i++)
  991.             {
  992.                 ObjTank obj = new ObjTank();
  993.                 obj.read(dis, applet);
  994.                 objVector.addElement(obj);
  995.             }
  996.  
  997.             drawVector.removeAllElements();
  998.             repaint(); //clear screen
  999.             count = dis.readShort();
  1000.             for (int i = 0 ; i < count; i++)
  1001.             {
  1002.                 Draw dd = new Draw();
  1003.                 dd.read(dis, objVector);
  1004.                 drawVector.addElement(dd);
  1005.             }
  1006.             repaint();
  1007.         }
  1008.         catch (IOException ioeRead)
  1009.         {
  1010.             System.err.println(ioeRead);
  1011.             return;
  1012.         }
  1013.     }
  1014.  
  1015.     //***************************************************************************************
  1016.     private int getStringWidth(Draw draw)
  1017.     {
  1018.         FontMetrics fm = getFontMetrics(new Font(draw.fontName, draw.fontStyle, draw.getFontSize()));
  1019.  
  1020.         return fm.stringWidth(draw.text);
  1021.     }
  1022.  
  1023.     private int getURLWidth(Draw draw)
  1024.     {
  1025.         FontMetrics fm = getFontMetrics(draw.getURLFont());
  1026.  
  1027.         return fm.stringWidth(draw.urlName);
  1028.     }
  1029.  
  1030.     private void beginString()
  1031.     {
  1032.         if (Draw.curMethod == Draw.ANCHOR)
  1033.         {
  1034.             int wid = 140;
  1035.             int sx = curDraw.x + (curDraw.width - wid)/2;
  1036.             if (sx < 0)
  1037.             {
  1038.                 sx = 0;
  1039.             }
  1040.             else 
  1041.             {
  1042.                 Dimension dim = size();
  1043.                 if (dim.width < sx + wid)
  1044.                     sx = dim.width - wid;
  1045.             }
  1046.             tfInput.reshape(sx - ptScroll.x, curDraw.y + (curDraw.height - Draw.URL_FONT_HEIGHT)/2 - ptScroll.y, wid, Draw.URL_FONT_HEIGHT);
  1047.             tfInput.setForeground(Color.blue);
  1048.             tfInput.setFont(curDraw.getURLFont()); 
  1049.             tfInput.setText(curDraw.hasURL() ? curDraw.urlName : "");
  1050.             tfInput.show();
  1051.             tfInput.requestFocus();
  1052.         }
  1053.         else
  1054.         {
  1055.             tfInput.reshape(curDraw.x - ptScroll.x, curDraw.y - ptScroll.y, curDraw.width + ((curDraw.hasText()) ? curDraw.height : 0), curDraw.height);
  1056.             tfInput.setForeground(curDraw.color);
  1057.             tfInput.setFont(new Font(curDraw.fontName, curDraw.fontStyle, curDraw.getFontSize()));
  1058.             tfInput.show();
  1059.             tfInput.requestFocus();
  1060.         }
  1061.     }
  1062.           
  1063.     private void endString()
  1064.     {
  1065.         if (Draw.curMethod == Draw.ANCHOR)
  1066.         {
  1067.             if (curDraw.hasURL())
  1068.             {
  1069.                 int wid = getURLWidth(curDraw);
  1070.                 repaint(curDraw.x + (curDraw.width - wid)/2, curDraw.y + (curDraw.height - Draw.URL_FONT_HEIGHT)/2, wid, Draw.URL_FONT_HEIGHT);
  1071.             }
  1072.         }
  1073.         else
  1074.         {
  1075.             if (curDraw.hasText())
  1076.             {
  1077.                 curDraw.resize(getStringWidth(curDraw), curDraw.height);
  1078.                 if (curDraw.width > tfInput.size().width)
  1079.                     repaint(curDraw);
  1080.             }
  1081.             else
  1082.             {
  1083.                 drawVector.removeElement(curDraw);
  1084.             }
  1085.         }
  1086.         tfInput.hide();
  1087.     }
  1088.  
  1089.  
  1090.     public void paint(Graphics g)
  1091.     {
  1092.         update(g);
  1093.     }
  1094.  
  1095.     public void repaint(int x, int y, int width, int height)
  1096.     {
  1097.         super.repaint(x - ptScroll.x, y-ptScroll.y, width, height);
  1098.     }
  1099.  
  1100.     public void repaint(Rectangle rc)
  1101.     {
  1102.         Rectangle clone = new Rectangle(rc.x, rc.y, rc.width, rc.height);
  1103.         clone.translate(-ptScroll.x, -ptScroll.y);
  1104.         super.repaint(clone.x, clone.y, clone.width, clone.height);
  1105.     }
  1106.     
  1107.     public void update(Graphics g)
  1108.     {
  1109.         Rectangle clipRect;
  1110.  
  1111.         if (isFirstPaint)
  1112.         {
  1113.             drawLogo(g);
  1114.             isFirstPaint = false;
  1115.         }
  1116.         g.clipRect(0, 0, dimension.width, dimension.height);
  1117.         clipRect = g.getClipRect();
  1118.  
  1119.         clipRect.translate(ptScroll.x, ptScroll.y);
  1120.         offGraphics.translate(-ptScroll.x, -ptScroll.y);
  1121.  
  1122.         offGraphics.setColor(getBackground());
  1123.         offGraphics.fillRect(clipRect.x, clipRect.y, clipRect.width, clipRect.height);
  1124.  
  1125.         if (stateGrid != 0)
  1126.         {
  1127.             int gap = (stateGrid == 1) ? 5 : 10;
  1128.  
  1129.             int sx = clipRect.x - (clipRect.x % gap);
  1130.             int sy = clipRect.y - (clipRect.y % gap);
  1131.             int ex = clipRect.x + clipRect.width;
  1132.             int ey = clipRect.y + clipRect.height;
  1133.             offGraphics.setColor(getBackground().darker());
  1134.             for (int x = sx; x < ex; x+= gap)
  1135.                 offGraphics.drawLine(x, sy, x, ey);
  1136.             for (int y = sy; y < ey; y+= gap)
  1137.                 offGraphics.drawLine(sx, y, ex, y);
  1138.         }
  1139.  
  1140.         Enumeration e = drawVector.elements();
  1141.         Draw dd;        
  1142.         while (e.hasMoreElements())
  1143.         {
  1144.             dd = (Draw) e.nextElement();
  1145.             if (!(clipRect.intersection(dd.getClipRect())).isEmpty())            
  1146.             {
  1147.                 dd.draw(offGraphics);
  1148.             }
  1149.         }
  1150.         // draw selected mark
  1151.         offGraphics.setXORMode(Color.blue);
  1152.         offGraphics.setColor(Color.white);
  1153.  
  1154.         e = bufVector.elements();
  1155.         while (e.hasMoreElements())
  1156.         {
  1157.             dd = (Draw) e.nextElement();
  1158.             if (!(clipRect.intersection(dd.getClipRect())).isEmpty())
  1159.             {
  1160.                 offGraphics.drawRect(dd.x, dd.y, dd.width, dd.height);
  1161.             }
  1162.         }
  1163.         offGraphics.setPaintMode();        
  1164.  
  1165.         if (!(clipRect.intersection(tracker.getClipRect())).isEmpty())            
  1166.             tracker.draw(offGraphics);
  1167.  
  1168.         clipRect.translate(-ptScroll.x, -ptScroll.y);
  1169.         offGraphics.translate(ptScroll.x, ptScroll.y);
  1170.         Dimension dm = size();            
  1171.         if (clipRect.x <= 0 || clipRect.y <= 0 || clipRect.x + clipRect.width >= dm.width || clipRect.y + clipRect.height >= dm.height)            
  1172.         {
  1173.             offGraphics.setColor(Color.black);
  1174.             offGraphics.drawRect(0, 0, dm.width -1, dm.height - 1);
  1175.         }
  1176.         if (clipRect.x + clipRect.width >= dimension.width || clipRect.y + clipRect.height >= dimension.height)            
  1177.         {
  1178.             offGraphics.setColor(Color.black);
  1179.             offGraphics.drawRect(0, 0, dimension.width -1, dimension.height - 1);
  1180.         }
  1181.  
  1182.         g.drawImage(offImage, 0, 0, null);
  1183.     }
  1184.  
  1185.     void setCurRect(int x, int y)
  1186.     {
  1187.         curDraw.reshape(Math.min(ptStart.x, x), Math.min(ptStart.y, y),
  1188.                                   Math.abs(ptStart.x - x), Math.abs(ptStart.y - y));    
  1189.     }
  1190.  
  1191.     private void unselect(Rectangle clip)
  1192.     {
  1193.         Draw dd;        
  1194.         Rectangle rc;
  1195.  
  1196.         if (bufVector.size() < 1)
  1197.             return;
  1198.  
  1199.         rc = ((Draw)bufVector.firstElement()).getClipRect();
  1200.  
  1201.         if (!tracker.getClipRect().isEmpty())
  1202.         {
  1203.             rc = rc.union(tracker.getClipRect());
  1204.             tracker.stateSelect = tracker.OFF;
  1205.         }
  1206.  
  1207.         if (clip != null && !clip.isEmpty())
  1208.             rc = rc.union(clip);
  1209.         Enumeration e = bufVector.elements();
  1210.         while (e.hasMoreElements())
  1211.         {
  1212.             dd = (Draw) e.nextElement();
  1213.             rc = rc.union(dd.getClipRect());
  1214.             dd.isSelected = false;
  1215.         }
  1216.         bufVector.removeAllElements();
  1217.         repaint(rc);
  1218.     }
  1219.  
  1220.     public synchronized void layout()
  1221.     {
  1222.  
  1223.         Dimension dm = size();
  1224.         offImage = createImage(dm.width, dm.height); 
  1225.         offGraphics = offImage.getGraphics();
  1226.         
  1227.         if (!isDimensionSetted)
  1228.             dimension = dm;
  1229.         
  1230.     }
  1231.  
  1232.     public synchronized void drawLogo(Graphics g)
  1233.     {
  1234.  
  1235.         Dimension dm = size();
  1236.         Dimension bound = new Dimension(dm.width*2/3, dm.height*2/3);
  1237.         Enumeration enum;
  1238.         Point m = new Point(0, 0);
  1239.  
  1240.         double i = 0.05;
  1241.         double wid = 0.0;
  1242.         double hig = 0.0;
  1243.         while (wid < bound.width && hig < bound.height)
  1244.         {
  1245.             enum = logoVector.elements();
  1246.  
  1247.             wid = 1040.0 * i;
  1248.             hig = 760.0 * i;
  1249.  
  1250.             m.x = (int)(dm.width - wid)/2;
  1251.             m.y = (int)(dm.height - hig)/2;
  1252.             while(enum.hasMoreElements())
  1253.             {
  1254.                 Draw dd = (Draw) enum.nextElement();
  1255.                 Draw clone = new Draw(dd);
  1256.                 clone.multiply(i, i);
  1257.                 clone.transelate(m.x, m.y);
  1258.  
  1259.                 clone.draw(offGraphics);
  1260.  
  1261.             }
  1262.             g.drawImage(offImage, 0, 0, null);
  1263.  
  1264.             i *= 1.1;
  1265.  
  1266.             try
  1267.                 wait(80);
  1268.             catch (InterruptedException e)
  1269.                 return;
  1270.         }
  1271.         try
  1272.             wait(3000);
  1273.         catch (InterruptedException e)
  1274.             return;
  1275.     }
  1276. }
  1277.  
  1278.