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

  1. import java.awt.*;
  2. import java.applet.*;
  3.  
  4. /**
  5.  * A applet that edits vector drawings
  6.  *
  7.  * @version     1.0 03/24/96
  8.  * @author         Kang, Dae Woong (namu@star.elim.net)
  9.  */
  10.  
  11. public class EditDraw extends Applet 
  12. {
  13.     public void init()
  14.     {
  15.         Panel pnlDraw = new Panel();
  16.         Panel pnlSouth = new Panel();
  17.  
  18.         pnlDraw.setLayout(new FlowLayoutCust(FlowLayoutCust.VERTICAL));
  19.         pnlSouth.setLayout(new FlowLayoutCust(FlowLayoutCust.HORIZONTAL));
  20.  
  21.         WorkPanel work = new WorkPanel(this);
  22.         work.setLayout(null);
  23.         work.tfInput.resize(0, 0);
  24.         work.tfInput.hide();
  25.         work.add(work.tfInput);
  26.  
  27.         ScrollPanel pnlCenter = new ScrollPanel(work);
  28.  
  29.           pnlDraw.add(new DrawMethodBar(work, Bar.VERTICAL));
  30.         pnlDraw.add(new DrawContentBar(work, Bar.VERTICAL));
  31.         pnlDraw.add(new DrawShapeBar(work, Bar.VERTICAL));
  32.  
  33.         pnlSouth.add(new ColorBar(work, Bar.HORIZONTAL));
  34.         pnlSouth.add(new ChoiceFont(work));
  35.         pnlSouth.add(new FontStyleBar(work, Bar.HORIZONTAL));
  36.         pnlSouth.add(new RunCommandBar(work, Bar.HORIZONTAL));
  37.         pnlSouth.add(work.lbPosition);
  38.  
  39.  
  40.         setLayout(new BorderLayout());
  41.         add("Center", pnlCenter);
  42.         add("South", pnlSouth);
  43.         add("West", pnlDraw);
  44.     }
  45.  
  46.     public String[][] getParameterInfo()
  47.     {
  48.          String pinfo[][] = 
  49.          {
  50.            {"draw",  "String",    "directory where exist draw files"},
  51.            {"image", "String",    "directory where exist image files(*.gif, *.jpg ..)"},
  52.            {"port",  "int",       "port number for writing draw files"}
  53.          };
  54.          return pinfo;
  55.     }
  56.  
  57.     public String getAppletInfo()
  58.     {
  59.         return new String("Web Draw 1.0  Author:Kang, Daewoong");
  60.     }
  61. }
  62.  
  63.  
  64. /**
  65.  * A container class that can scroll area with scrollbar
  66.  *
  67.  * @version     1.0 03/24/96
  68.  * @author         Kang, Dae Woong (gothic@star.elim.net)
  69.  */
  70.  
  71. class ScrollPanel extends Panel
  72. {
  73.     /**
  74.      * The target panel that will be scrolled
  75.      */
  76.     WorkPanel target;
  77.     /**
  78.      * Scrollbars that will be displayed in south, east.
  79.      */
  80.     Scrollbar scbVertical, scbHorizontal;
  81.  
  82.     /**
  83.      * Constructs a ScrollPanel with a WorkPanel
  84.      * @param target that will be scrolled
  85.      */
  86.     public ScrollPanel(WorkPanel target)
  87.     {
  88.         this.target = target;
  89.         setLayout(new BorderLayout());
  90.  
  91.         add("Center", target);
  92.         add("South", scbHorizontal = new Scrollbar(Scrollbar.HORIZONTAL,0, 100, 0, 200));
  93.         add("East", scbVertical = new Scrollbar(Scrollbar.VERTICAL));
  94.     }
  95.  
  96.     /**
  97.      * set Scrollbar values
  98.      */
  99.     public void layout()
  100.     {
  101.  
  102.         Dimension dm;
  103.         super.layout();
  104.         dm = target.size();
  105.  
  106.         if (dm.height < target.dimension.height)
  107.         {
  108.             scbVertical.enable();
  109.             scbVertical.setValues(scbVertical.getValue(), dm.height, 0, target.dimension.height);
  110.         }
  111.         else
  112.         {
  113.             scbVertical.disable();
  114.         }
  115.  
  116.         if (dm.width < target.dimension.width)
  117.         {
  118.             scbHorizontal.enable();
  119.             scbHorizontal.setValues(scbHorizontal.getValue(), dm.width, 0, target.dimension.width);
  120.         }
  121.         else
  122.         {
  123.             scbHorizontal.disable();
  124.         }
  125.     }
  126.  
  127.     /**
  128.      * handle Scrollbar event
  129.      */
  130.     public boolean handleEvent(Event e)
  131.     {
  132.         if (e.target == scbVertical || e.target == scbHorizontal)
  133.         {
  134.             Scrollbar scr = (Scrollbar) e.target;
  135.             int val = 0;
  136.             int tmp;
  137.             boolean isDefault = false;
  138.             switch (e.id)
  139.             {
  140.             case e.SCROLL_ABSOLUTE:
  141.                 val = ((Integer)e.arg).intValue();
  142.                 break;
  143.             case e.SCROLL_LINE_DOWN:
  144.                 tmp = scr.getValue() + scr.getVisible()/10;
  145.                 scr.setValue((tmp  + scr.getVisible() > scr.getMaximum()) ? scr.getMaximum() - scr.getVisible() : tmp);
  146.                 val = scr.getValue();
  147.                 break;
  148.             case e.SCROLL_LINE_UP:
  149.                 scr.setValue(scr.getValue() - scr.getVisible()/10);
  150.                 val = scr.getValue();
  151.                 break;
  152.             case e.SCROLL_PAGE_DOWN:
  153.                 tmp = scr.getValue() + scr.getVisible();
  154.                 scr.setValue((tmp + scr.getVisible() > scr.getMaximum()) ? scr.getMaximum()  - scr.getVisible() : tmp);
  155.                 val = scr.getValue();
  156.                 break;
  157.             case e.SCROLL_PAGE_UP:
  158.                 scr.setValue(scr.getValue() - scr.getVisible());
  159.                 val = scr.getValue();
  160.                 break;
  161.             default:
  162.                 isDefault = true;
  163.             }
  164.             if (!isDefault)
  165.             {
  166.                 if (e.target == scbVertical)
  167.                     target.ptScroll.y = val;
  168.                 else
  169.                     target.ptScroll.x = val;
  170.                 target.repaint();
  171.                 return true;
  172.             }
  173.         }
  174.  
  175.         return super.handleEvent(e);
  176.     }
  177.  
  178. /**
  179.  * A Bar class that set drawing' method
  180.  *
  181.  * @version     1.0 03/24/96
  182.  * @author         Kang, Dae Woong (gothic@star.elim.net)
  183.  */
  184. class DrawMethodBar extends ChoiceBar
  185. {
  186.     /**
  187.      * drawing place
  188.      */
  189.     WorkPanel target;
  190.     /**
  191.      * it will be displayed in status line when mouse move over thr button
  192.      */
  193.     String[] help = 
  194.     {
  195.         "Line drawing", "Rectangle drawing", "Rounded Rectangle drawing",
  196.         "Polygon drawing", "Oval drawing", "Pie drawing", "Arc drawing",
  197.         "Text writng", "Picture reference", "Draw Reference", "Select Drawings",
  198.         "Add URL"
  199.     };
  200.  
  201.     /**
  202.      * Constructs a DrawMethodBar with a WorkPanel
  203.      * @param target : drawing place
  204.      * @param direction : bars direction (VERTICAL, HORIZONTAL)
  205.      * @see Bar
  206.      */
  207.     public DrawMethodBar(WorkPanel target, int direction)
  208.     {
  209.         super(Draw.METHOD_COUNT, direction);
  210.         this.target = target;
  211.         setHelp(help);
  212.         select(0);
  213.     }
  214.  
  215.     /**
  216.      * implement's abstract method in Bar
  217.      * @param g : graphics
  218.      * @param i : button index in Bar
  219.      * @param rc : drawing area
  220.      * @see Bar
  221.      */
  222.     void drawCell(Graphics g, int i, Rectangle rc)
  223.     {
  224.         Polygon shapePolygon, shapeSelect;
  225.         g.setColor(Color.blue);
  226.         
  227.         switch (i)
  228.         {
  229.         case Draw.LINE:
  230.             g.drawLine(rc.x, rc.y, rc.x + rc.width, rc.y + rc.height);
  231.             break;
  232.         case Draw.RECT:
  233.             g.drawRect(rc.x, rc.y, rc.width, rc.height);
  234.             break;
  235.         case Draw.ROUND_RECT:
  236.             g.drawRoundRect(rc.x, rc.y, rc.width, rc.height, 10, 10);
  237.             break;
  238.         case Draw.POLYGON:
  239.             shapePolygon = new Polygon();
  240.             shapePolygon.addPoint(rc.x, rc.y + rc.height);
  241.             shapePolygon.addPoint(rc.x + rc.width/2, rc.y);
  242.             shapePolygon.addPoint(rc.x + rc.width, rc.y  + rc.height);
  243.             g.drawPolygon(shapePolygon);
  244.             break;
  245.         case Draw.OVAL:
  246.             g.drawOval(rc.x, rc.y, rc.width, rc.height);
  247.             break;
  248.         case Draw.PIE:
  249.             g.drawArc(rc.x, rc.y, rc.width, rc.height, 0, 270);
  250.             g.drawLine(rc.x + rc.width/2, rc.y + rc.height/2, rc.x + rc.width/2, rc.y + rc.height);
  251.             g.drawLine(rc.x + rc.width/2, rc.y + rc.height/2, rc.x + rc.width, rc.y + rc.height/2);
  252.             break;
  253.         case Draw.ARC:
  254.             g.drawArc(rc.x, rc.y, rc.width, rc.height, 0, 270);
  255.             break;
  256.         case Draw.STRING:
  257.             g.drawString("A", rc.x + 3, rc.y + rc.height);
  258.             break;
  259.         case Draw.IMAGE:
  260.             g.fillOval(rc.x, rc.y, 5, 6);
  261.             g.fillOval(rc.x + 4, rc.y + 2, 5, 5);
  262.             g.setColor(Color.green);
  263.             g.fillOval(rc.x + 4, rc.y + 7, 4, 3);
  264.             g.setColor(Color.yellow);
  265.             g.fillRect(rc.x + 7, rc.y + 6, 4, 4);
  266.             g.setColor(Color.green);
  267.             g.fillOval(rc.x + 6, rc.y + 4, 3, 3);
  268.             g.setColor(Color.red);
  269.             g.fillOval(rc.x + 8, rc.y + 8, 3, 3);
  270.             break;
  271.         case Draw.DRAW:
  272.             g.fillRect(rc.x, rc.y, rc.width*2/3, rc.height*2/3);
  273.             g.setColor(Color.green);
  274.             g.fillOval(rc.x + rc.width/3, rc.y + rc.height/3, rc.width*2/3, rc.height*2/3);
  275.             break;
  276.         case Draw.SELECT:
  277.             shapeSelect = new Polygon();
  278.             shapeSelect.addPoint(rc.x + rc.width/2, rc.y);
  279.             shapeSelect.addPoint(rc.x, rc.y + rc.height/3);
  280.             shapeSelect.addPoint(rc.x + rc.width/3, rc.y  + rc.height/3);
  281.             shapeSelect.addPoint(rc.x + rc.width/3, rc.y  + rc.height);
  282.             shapeSelect.addPoint(rc.x + rc.width/3*2, rc.y  + rc.height);
  283.             shapeSelect.addPoint(rc.x + rc.width/3*2, rc.y  + rc.height/3);
  284.             shapeSelect.addPoint(rc.x + rc.width, rc.y  + rc.height/3);
  285.             shapeSelect.addPoint(rc.x + rc.width/2, rc.y);
  286.             g.drawPolygon(shapeSelect);
  287.             break;
  288.         case Draw.ANCHOR:
  289.             g.drawLine(rc.x, rc.y + rc.height/3, rc.x + rc.width/2, rc.y);
  290.             g.drawLine(rc.x + rc.width/2, rc.y, rc.x + rc.width, rc.y + rc.height/3);
  291.             g.drawLine(rc.x + rc.width/2, rc.y, rc.x + rc.width/2, rc.y + rc.height*2/3);
  292.             g.drawLine(rc.x + rc.width/2 - 2, rc.y + rc.height/2, rc.x + rc.width/2 + 2, rc.y + rc.height/2);
  293.             g.drawOval(rc.x + rc.width/3, rc.y + rc.height*2/3, rc.width/3, rc.height/3);
  294.             break;
  295.         }
  296.     }
  297.         
  298.     /**
  299.      * implement's abstract method in Bar
  300.      * set target's content variable
  301.      * @param i : pressed button index in Bar
  302.      * @see Bar
  303.      */
  304.     void select(int i)
  305.     {
  306.         target.setMethod(i);
  307.     }
  308. }
  309.  
  310. /**
  311.  * A Bar class that set drawing' content (fill, unfill)
  312.  *
  313.  * @version     1.0 03/24/96
  314.  * @author         Kang, Dae Woong (gothic@star.elim.net)
  315.  */
  316. class DrawContentBar extends ChoiceBar
  317. {
  318.     /**
  319.      * drawing place
  320.      */
  321.     WorkPanel target;
  322.     
  323.     /**
  324.      * it will be displayed in status line when mouse move over thr button
  325.      */
  326.     String[] help = {"Fill drawing's contents", "Unfill drawing's contents"};
  327.  
  328.  
  329.     /**
  330.      * Constructs a DrawContentBar with a WorkPanel
  331.      * @param target : drawing place
  332.      * @param direction : bars direction (VERTICAL, HORIZONTAL)
  333.      * @see Bar
  334.      */
  335.     public DrawContentBar(WorkPanel target, int direction)
  336.     {
  337.         super(Draw.CONTENT_COUNT, direction);
  338.         this.target = target;
  339.         setHelp(help);
  340.         select(0);
  341.     }
  342.  
  343.     /**
  344.      * implement's abstract method in Bar
  345.      * @param g : graphics
  346.      * @param i : button index in Bar
  347.      * @param rc : drawing area
  348.      * @see Bar
  349.      */
  350.     void drawCell(Graphics g, int i, Rectangle rc)
  351.     {
  352.         g.setColor(Color.black);
  353.         switch (i)
  354.         {
  355.         case Draw.CONTENT_FILL: 
  356.             g.fillRect(rc.x, rc.y, rc.width - 1, rc.height - 1);
  357.             break;
  358.         case Draw.CONTENT_UNFILL: 
  359.             g.drawRect(rc.x, rc.y, rc.width - 1, rc.height - 1);
  360.             break;
  361.         }
  362.         
  363.     }
  364.  
  365.     /**
  366.      * implement's abstract method in Bar
  367.      * set target's content variable
  368.      * @param i : pressed button index in Bar
  369.      * @see Bar
  370.      */
  371.     void select(int i)
  372.     {
  373.         target.setContent(i);
  374.     }
  375. }
  376.  
  377. /**
  378.  * A Bar class that set drawing' shape
  379.  *
  380.  * @version     1.0 03/24/96
  381.  * @author         Kang, Dae Woong (gothic@star.elim.net)
  382.  */
  383. class DrawShapeBar extends ChoiceBar
  384. {
  385.     /**
  386.      * drawing place
  387.      */
  388.     WorkPanel target;
  389.     /**
  390.      * it will be displayed in status line when mouse move over thr button
  391.      */
  392.     String[] help = {"Draw plain shape drawing", "Draw up shape drawing", "Draw down shape drawing"};
  393.  
  394.  
  395.     /**
  396.      * Constructs a DrawShapeBar with a WorkPanel
  397.      * @param target : drawing place
  398.      * @param direction : bars direction (VERTICAL, HORIZONTAL)
  399.      * @see Bar
  400.      */
  401.     public DrawShapeBar(WorkPanel target, int direction)
  402.     {
  403.         super(Draw.SHAPE_COUNT, direction);
  404.         this.target = target;
  405.         setHelp(help);
  406.         select(0);
  407.     }
  408.  
  409.     /**
  410.      * implement's abstract method in Bar
  411.      * @param g : graphics
  412.      * @param i : button index in Bar
  413.      * @param rc : drawing area
  414.      * @see Bar
  415.      */
  416.     void drawCell(Graphics g, int i, Rectangle rc)
  417.     {
  418.         switch (i)
  419.         {
  420.         case Draw.SHAPE_PLAIN:
  421.             g.setColor(Color.black);
  422.             g.drawRect(rc.x, rc.y, rc.width, rc.height);
  423.             break;
  424.         case Draw.SHAPE_UP:
  425.             g.setColor(getBackground());
  426.             g.draw3DRect(rc.x, rc.y, rc.width, rc.height, true);
  427.             break;
  428.         case Draw.SHAPE_DOWN:
  429.             g.setColor(getBackground());
  430.             g.draw3DRect(rc.x, rc.y, rc.width, rc.height, false);
  431.             break;
  432.         }
  433.     }
  434.         
  435.     /**
  436.      * implement's abstract method in Bar
  437.      * set target's content variable
  438.      * @param i : pressed button index in Bar
  439.      * @see Bar
  440.      */
  441.     void select(int i)
  442.     {
  443.         target.setShape(i);
  444.     }
  445. }
  446.  
  447. /**
  448.  * A Bar class that set drawing' Color
  449.  *
  450.  * @version     1.0 03/24/96
  451.  * @author         Kang, Dae Woong (gothic@star.elim.net)
  452.  */
  453. class ColorBar extends ChoiceBar
  454. {
  455.     /**
  456.      * drawing place
  457.      */
  458.     WorkPanel target;
  459.     
  460.     /**
  461.      * Colors that will be displayed
  462.      */
  463.     final static Color[] baseColors = 
  464.     {
  465.         Color.black, Color.darkGray, Color.gray, Color.lightGray, Color.white,
  466.         Color.red, Color.magenta, Color.pink,
  467.         Color.yellow, Color.orange,
  468.         Color.blue, Color.cyan,
  469.         Color.green
  470.     };
  471.     
  472.     /**
  473.      * Constructs a DrawContentBar with a WorkPanel
  474.      * @param target : drawing place
  475.      * @param direction : bars direction (VERTICAL, HORIZONTAL)
  476.      * @see Bar
  477.      */
  478.     public ColorBar(WorkPanel target, int direction)
  479.     {
  480.         super(baseColors.length, direction);
  481.         this.target = target;
  482.         select(0);
  483.     }
  484.  
  485.     /**
  486.      * implement's abstract method in Bar
  487.      * @param g : graphics
  488.      * @param i : button index in Bar
  489.      * @param rc : drawing area
  490.      * @see Bar
  491.      */
  492.     void drawCell(Graphics g, int i, Rectangle rc)
  493.     {
  494.         g.setColor(baseColors[i]);
  495.         g.fillRect(rc.x, rc.y, rc.width, rc.height);
  496.     }
  497.  
  498.     /**
  499.      * implement's abstract method in Bar
  500.      * set target's color variable
  501.      * @param i : pressed button index in Bar
  502.      * @see Bar
  503.      */
  504.     void select(int i)
  505.     {
  506.         target.setColor(baseColors[i]);
  507.     }
  508. }
  509.  
  510. /**
  511.  * A Choice class that set drawing' FontName
  512.  *
  513.  * @version     1.0 03/24/96
  514.  * @author         Kang, Dae Woong (gothic@star.elim.net)
  515.  */
  516. class ChoiceFont extends Choice
  517. {
  518.     /**
  519.      * drawing place
  520.      */
  521.     WorkPanel target;
  522.     /**
  523.      * Constructs a DrawContentBar with a WorkPanel
  524.      * @param target : drawing place
  525.      */
  526.     public ChoiceFont(WorkPanel target)
  527.     {
  528.         this.target = target;
  529.         String[] fonts = getToolkit().getFontList();
  530.         for (int i = 0; i < fonts.length - 1; i++) // font symbol not correctly work in graphics
  531.             addItem(fonts[i]);
  532.         target.setFontName(fonts[0]);
  533.     }
  534.     /*
  535.      *  set Drawing's font name When user select the font
  536.      */
  537.     public boolean action(Event e, Object what)
  538.     {
  539.         target.setFontName((String)e.arg);
  540.         return false;
  541.     }
  542. }
  543.  
  544. /**
  545.  * A Bar class that set drawing' FontStyle(PLAIN, BOLD, ITALIC)
  546.  *
  547.  * @version     1.0 03/24/96
  548.  * @author         Kang, Dae Woong (gothic@star.elim.net)
  549.  */
  550. class FontStyleBar extends ChoiceBar
  551. {
  552.     /**
  553.      * drawing place
  554.      */
  555.     WorkPanel target;
  556.     /**
  557.      * font style array
  558.      */
  559.     final static int[] fontStyles = { Font.PLAIN, Font.BOLD, Font.ITALIC };
  560.     /**
  561.      * it will be displayed in bar
  562.      */
  563.     final static String[] fontTexts = { "P", "B", "I"};
  564.     /**
  565.      * it will be displayed in status line when mouse move over thr button
  566.      */
  567.     String[] help = {"Plain font shape", "Bold font shape", "Italic font shape"};
  568.  
  569.     /**
  570.      * Constructs a FontStyleBar with a WorkPanel
  571.      * @param target : drawing place
  572.      * @param direction : bars direction (VERTICAL, HORIZONTAL)
  573.      * @see Bar
  574.      */
  575.     public FontStyleBar(WorkPanel target, int direction)
  576.     {
  577.         super(fontStyles.length, direction);
  578.         this.target = target;
  579.         setHelp(help);
  580.         select(0);
  581.     }
  582.  
  583.  
  584.     /**
  585.      * implement's abstract method in Bar
  586.      * @param g : graphics
  587.      * @param i : button index in Bar
  588.      * @param rc : drawing area
  589.      * @see Bar
  590.      */
  591.     void drawCell(Graphics g, int i, Rectangle rc)
  592.     {
  593.         if (i >= fontTexts.length || i < 0)
  594.             return;
  595.  
  596.         Font font = new Font("TimesRoman", fontStyles[i], rc.height);
  597.         g.setFont(font);
  598.         g.setColor(Color.blue);
  599.         g.drawString(fontTexts[i], rc.x, rc.y + rc.height);
  600.     }
  601.         
  602.     /**
  603.      * implement's abstract method in Bar
  604.      * set target's font style variable
  605.      * @param i : pressed button index in Bar
  606.      * @see Bar
  607.      */
  608.     void select(int i)
  609.     {
  610.         target.setFontStyle(fontStyles[i]);
  611.     }
  612. }
  613.  
  614. /**
  615.  * A Bar class that run command
  616.  *
  617.  * @version     1.0 03/24/96
  618.  * @author         Kang, Dae Woong (gothic@star.elim.net)
  619.  */
  620. class RunCommandBar extends CommandBar
  621. {
  622.     /**
  623.      * drawing place
  624.      */
  625.     WorkPanel target;
  626.  
  627.     /**
  628.      * Command IDs
  629.      */
  630.     final static int SET_SIZE = 0;
  631.     final static int GRID = 1;
  632.  
  633.     final static int DELETE = 2;
  634.     final static int COPY = 3;
  635.  
  636.     final static int READ_DRAW = 4;
  637.     final static int WRITE_DRAW = 5;
  638.  
  639.     final static int ALIGN_TOP = 6;
  640.     final static int ALIGN_BOTTOM = 7;
  641.     final static int ALIGN_LEFT = 8;
  642.     final static int ALIGN_RIGHT = 9;
  643.  
  644.     final static int BRING_FRONT = 10;
  645.     final static int BRING_BACK = 11;
  646.  
  647.     final static int REMOVE_ALL = 12;
  648.  
  649.     final static int ABOUT_DRAW = 13;
  650.  
  651.     final static int RUN_COUNT = 14;
  652.  
  653.     /**
  654.      * it will be displayed in status line when mouse move over thr button
  655.      */
  656.     String[] help = 
  657.     {
  658.         "Set Draw size", "Toggle Grid", "Delete selected drawing(s)", "Copy selected drawing(s)",
  659.         "Read Draw", "Save Draw", 
  660.         "Align selected drawings to Top", 
  661.         "Align selected drawings to Bottom", 
  662.         "Align selected drawings to Left", 
  663.         "Align selected drawings to Right",
  664.         "Bring selected drawing to Front", 
  665.         "Bring selected drawing to Back", 
  666.         "Remove All (Clear Screen)",
  667.         "About Draw?"
  668.     };
  669.  
  670.     /**
  671.      * Constructs a RunCommandBar with a WorkPanel
  672.      * @param target : drawing place
  673.      * @param direction : bars direction (VERTICAL, HORIZONTAL)
  674.      * @see Bar
  675.      */
  676.     public RunCommandBar(WorkPanel target, int direction)
  677.     {
  678.         super(RUN_COUNT, direction);
  679.         this.target = target;
  680.         setHelp(help);
  681.     }
  682.  
  683.     /**
  684.      * implement's abstract method in Bar
  685.      * @param g : graphics
  686.      * @param i : button index in Bar
  687.      * @param rc : drawing area
  688.      * @see Bar
  689.      */
  690.     void drawCell(Graphics g, int i, Rectangle rc)
  691.     {
  692.         if (i > RUN_COUNT || i < 0)
  693.             return;
  694.  
  695.         switch (i)
  696.         {
  697.         case SET_SIZE:
  698.             g.setColor(Color.blue);
  699.             g.drawLine(rc.x, rc.y, rc.x + rc.width, rc.y);
  700.             g.drawLine(rc.x, rc.y + rc.height - 2, rc.x + rc.width, rc.y + rc.height - 2);
  701.             g.drawLine(rc.x, rc.y, rc.x, rc.y + rc.height);
  702.             g.drawLine(rc.x + rc.width - 2, rc.y, rc.x + rc.width - 2, rc.y + rc.height);
  703.             break;
  704.         case GRID:
  705.             int wid = rc.width/4;
  706.             int hgt = rc.height/4;
  707.             g.setColor(Color.blue);
  708.             for (int k = 0; k < 5; k++)
  709.             {
  710.                 g.drawLine(rc.x, rc.y + hgt*k, rc.x + rc.width, rc.y + hgt*k);
  711.                 g.drawLine(rc.x + wid*k, rc.y, rc.x + wid*k, rc.y + rc.height);
  712.             }
  713.             break;
  714.         case DELETE:
  715.             g.setFont(new Font("TimesRoman", Font.BOLD, rc.height + 4));
  716.             g.setColor(Color.blue);
  717.             g.drawString("-", rc.x + 4, rc.y + rc.height);
  718.             break;
  719.         case COPY:
  720.             g.setFont(new Font("TimesRoman", Font.BOLD, rc.height + 4));
  721.             g.setColor(Color.blue);
  722.             g.drawString("+", rc.x + 2, rc.y + rc.height);
  723.             break;
  724.         case READ_DRAW:
  725.             g.setColor(Color.green);
  726.             g.fillRect(rc.x, rc.y + rc.height/3, rc.width*2/3, rc.height*2/3);
  727.             g.setColor(Color.black);
  728.             g.drawRect(rc.x, rc.y + rc.height/3, rc.width*2/3, rc.height*2/3);
  729.             g.setColor(Color.red);
  730.             g.drawLine(rc.x + rc.width, rc.y, rc.x + rc.width*2/3, rc.y);
  731.             g.drawLine(rc.x + rc.width, rc.y, rc.x + rc.width, rc.y + rc.height/3);
  732.             g.drawLine(rc.x + rc.width, rc.y, rc.x + rc.width/3, rc.y + rc.height*2/3);
  733.             break;
  734.         case WRITE_DRAW:
  735.             g.setColor(Color.green);
  736.             g.fillRect(rc.x, rc.y + rc.height/3, rc.width*2/3, rc.height*2/3);
  737.             g.setColor(Color.black);
  738.             g.drawRect(rc.x, rc.y + rc.height/3, rc.width*2/3, rc.height*2/3);
  739.             g.setColor(Color.red);
  740.             g.drawLine(rc.x + rc.width/3, rc.y + rc.height*2/3, rc.x + rc.width/3, rc.y + rc.height/3);
  741.             g.drawLine(rc.x + rc.width/3, rc.y + rc.height*2/3, rc.x + rc.width*2/3, rc.y + rc.height*2/3);
  742.             g.drawLine(rc.x + rc.width/3, rc.y + rc.height*2/3, rc.x + rc.width, rc.y);
  743.             break;
  744.         case ALIGN_TOP:
  745.             g.setColor(Color.blue);
  746.             g.drawLine(rc.x + rc.width/2, rc.y, rc.x, rc.y + rc.height/2);            
  747.             g.drawLine(rc.x + rc.width/2, rc.y, rc.x + rc.width/2, rc.y + rc.height);            
  748.             g.drawLine(rc.x + rc.width/2, rc.y, rc.x + rc.width, rc.y + rc.height/2);            
  749.             break;
  750.         case ALIGN_BOTTOM:
  751.             g.setColor(Color.blue);
  752.             g.drawLine(rc.x + rc.width/2, rc.y + rc.height, rc.x, rc.y + rc.height/2);            
  753.             g.drawLine(rc.x + rc.width/2, rc.y + rc.height, rc.x + rc.width/2, rc.y);            
  754.             g.drawLine(rc.x + rc.width/2, rc.y + rc.height, rc.x + rc.width, rc.y + rc.height/2);            
  755.             break;
  756.         case ALIGN_LEFT:
  757.             g.setColor(Color.blue);
  758.             g.drawLine(rc.x, rc.y + rc.height/2, rc.x + rc.width/2, rc.y);            
  759.             g.drawLine(rc.x, rc.y + rc.height/2, rc.x + rc.width, rc.y + rc.height/2);            
  760.             g.drawLine(rc.x, rc.y + rc.height/2, rc.x + rc.width/2, rc.y + rc.height);            
  761.             break;
  762.         case ALIGN_RIGHT:
  763.             g.setColor(Color.blue);
  764.             g.drawLine(rc.x + rc.width, rc.y + rc.height/2, rc.x + rc.width/2, rc.y);            
  765.             g.drawLine(rc.x + rc.width, rc.y + rc.height/2, rc.x, rc.y + rc.height/2);            
  766.             g.drawLine(rc.x + rc.width, rc.y + rc.height/2, rc.x + rc.width/2, rc.y + rc.width);            
  767.             break;
  768.         case BRING_FRONT:
  769.             g.setColor(Color.black);
  770.             g.drawRect(rc.x, rc.y, rc.width*2/3, rc.height*2/3);
  771.  
  772.             g.setColor(Color.blue);
  773.             g.fillRect(rc.x + rc.width/3, rc.y + rc.height/3, rc.width*2/3, rc.height*2/3);
  774.             g.setColor(Color.black);
  775.             g.drawRect(rc.x + rc.width/3, rc.y + rc.height/3, rc.width*2/3, rc.height*2/3);
  776.             break;
  777.         case BRING_BACK:
  778.             g.setColor(Color.blue);
  779.             g.fillRect(rc.x + rc.width/3, rc.y + rc.height/3, rc.width*2/3, rc.height*2/3);
  780.             g.setColor(Color.black);
  781.             g.drawRect(rc.x + rc.width/3, rc.y + rc.height/3, rc.width*2/3, rc.height*2/3);
  782.  
  783.             g.setColor(Color.lightGray);
  784.             g.fillRect(rc.x, rc.y, rc.width*2/3, rc.height*2/3);
  785.             g.setColor(Color.black);
  786.             g.drawRect(rc.x, rc.y, rc.width*2/3, rc.height*2/3);
  787.             break;
  788.         case REMOVE_ALL:
  789.             g.setColor(Color.red);
  790.             g.drawLine(rc.x, rc.y, rc.x + rc.width, rc.y + rc.height);
  791.             g.drawLine(rc.x, rc.y + rc.height, rc.x + rc.width, rc.y);
  792.             break;
  793.         case ABOUT_DRAW:
  794.             g.setColor(Color.blue);
  795.             g.drawArc(rc.x + rc.width/4, rc.y, rc.width/2, rc.height/2, 270, 270);
  796.             g.drawLine(rc.x + rc.width/2, rc.y + rc.height/2, rc.x + rc.width/2, rc.y + rc.height*3/4);
  797.             g.fillOval(rc.x + rc.width/2 - 1, rc.y + rc.height -  1, 2, 2);
  798.             break;
  799.         }
  800.     }
  801.         
  802.     /**
  803.      * implement's abstract method in Bar
  804.      * set target's content variable
  805.      * @param i : pressed button index in Bar
  806.      * @see Bar
  807.      */
  808.     void select(int i)
  809.     {
  810.         target.runCommand(i);        
  811.     }
  812. }
  813. //////////////////////////////////////////////////////////////////////////
  814. // Bars
  815. //////////////////////////////////////////////////////////////////////////
  816. /**
  817.  * root class of bar classes
  818.  *
  819.  * @version     1.0 03/24/96
  820.  * @author         Kang, Dae Woong (gothic@star.elim.net)
  821.  */
  822.  
  823. abstract class Bar extends Canvas
  824. {
  825.     /**
  826.      * button's drawing area size
  827.      */
  828.     final int sizeCell = 20;
  829.     /**
  830.      * bar's drawing area size
  831.      */
  832.     final int sizeBar = 30;
  833.     /**
  834.      * gab between bar border and button border
  835.      */
  836.     int gabBarCell = (sizeBar - sizeCell) / 2;
  837.  
  838.     /**
  839.      * help string array pointer
  840.      */
  841.      private String[] help = null; // showState
  842.     /**
  843.      * current displaying help pointer
  844.      */
  845.     private static String curHelp = null;
  846.  
  847.     /**
  848.      * bar's dispaying stylw
  849.      */
  850.     int direction;
  851.     static final int HORIZONTAL = 1;
  852.     static final int VERTICAL = 2;
  853.     
  854.     /**
  855.      * button count in bar
  856.      */
  857.     private int count;
  858.     /**
  859.      * getCellRect help variable
  860.      */
  861.     private int factorX, factorY;
  862.     
  863.     /**
  864.      * draw button method called when paint button
  865.      * @param g : graphics
  866.      * @param i : button index in Bar
  867.      * @param rc : drawing area
  868.      */
  869.     abstract void drawCell(Graphics g, int i, Rectangle rc);
  870.     /**
  871.      * called when mouse downd the button
  872.      * @param g : graphics
  873.      * @param i : button index in Bar
  874.      * @param rc : drawing area
  875.      */
  876.     abstract public void pressButton(int i);
  877.  
  878.     /**
  879.      * return the button state 
  880.      * @param i : button index in Bar
  881.      * @return : true if button is up state
  882.      */
  883.     abstract public boolean isButtonUp(int i);
  884.     /**
  885.      * called when button down
  886.      * set target's content variable
  887.      * @param i : pressed button index in Bar
  888.      */
  889.     abstract void select(int i); 
  890.  
  891.     /**
  892.      * Constructs a Bar with button count and direction
  893.      * @param count : button's count
  894.      * @param direction : bars direction (VERTICAL, HORIZONTAL)
  895.      */
  896.     public Bar(int count, int direction)
  897.     {
  898.         this.count = count;
  899.  
  900.         setBackground(Color.lightGray);
  901.         this.direction = direction;
  902.  
  903.         if (direction == HORIZONTAL)
  904.         { 
  905.             factorX = 1;
  906.             factorY = 0;
  907.             resize(sizeCell*count + gabBarCell*2, sizeBar);
  908.         }
  909.         else
  910.         {
  911.             factorX = 0;
  912.             factorY = 1;
  913.             resize(sizeBar, sizeCell*count + gabBarCell*2);
  914.         }
  915.     }
  916.  
  917.     /**
  918.      * set help string
  919.      * @param help : help String
  920.      */
  921.     public void setHelp(String[] help)
  922.     {
  923.         if (help.length >= count)
  924.             this.help = help;
  925.     }
  926.  
  927.     public void paint(Graphics g)
  928.     {
  929. System.out.println("bar print");
  930.         int i;
  931.         Dimension dm = size();
  932.                 
  933.         // draw border
  934.         g.setColor(Color.lightGray);
  935.         g.draw3DRect(0, 0, dm.width - 1, dm.height - 1, true);
  936.         
  937.         // draw each cell
  938.         for (i = 0; i < count; i++)
  939.         {
  940.             if (isButtonUp(i)) // child's method
  941.                 drawUpCell(g, i);
  942.             else
  943.                 drawDownCell(g, i);
  944.         }
  945.         g.dispose();
  946.     }
  947.  
  948.  
  949.  
  950.     /**
  951.      * draw up shape button
  952.      * it call child;s function drawCell
  953.      * @param g : graphics
  954.      * @param i : button index in Bar
  955.      */
  956.     void drawUpCell(Graphics g, int i)
  957.     {
  958.         Rectangle rc = getCellRect(i);
  959.     
  960.         g.setColor(Color.lightGray);
  961.         g.draw3DRect(rc.x, rc.y, rc.width, rc.height, true);
  962.         drawCell(g, i, getCellInterior(i, true));
  963.     }
  964.  
  965.     /**
  966.      * draw dwon shape button
  967.      * it call child;s function drawCell
  968.      * @param g : graphics
  969.      * @param i : button index in Bar
  970.      */
  971.     void drawDownCell(Graphics g, int i)
  972.     {
  973.         Rectangle rc = getCellRect(i);
  974.     
  975.         g.setColor(Color.lightGray);
  976.         g.draw3DRect(rc.x, rc.y, rc.width, rc.height, false);
  977.         drawCell(g, i, getCellInterior(i, false));
  978.     }
  979.  
  980.     /**
  981.      * clear button
  982.      * @param g : graphics
  983.      * @param i : button index in Bar
  984.      */
  985.     public void clearCell(Graphics g, int i)
  986.     {
  987.         Rectangle rc = getCellRect(i);
  988.  
  989.         g.clearRect(rc.x, rc.y, rc.width, rc.height);
  990.     }
  991.     
  992.     /**
  993.      * get area of a button
  994.      * @param i : button index in Bar
  995.      * @return button's area
  996.      */
  997.     public Rectangle  getCellRect(int i)
  998.     {
  999.         int  startPos = i*sizeCell;
  1000.         
  1001.         return new Rectangle(factorX*startPos + gabBarCell, factorY*startPos + gabBarCell,
  1002.                              sizeCell - 1, sizeCell - 1);
  1003.     }
  1004.  
  1005.     /**
  1006.      * get drawing area of a button
  1007.      * @param i : button index in Bar
  1008.      * @param isUp : state of button
  1009.      * @return button's area
  1010.      */
  1011.     public Rectangle  getCellInterior(int i, boolean  isUp)
  1012.     {
  1013.         int  startPos = i*sizeCell;
  1014.         int  factor = (isUp) ? 0 : 1;
  1015.         
  1016.         return new Rectangle(factorX*startPos + gabBarCell + 4 + factor,  factorY*startPos + gabBarCell + 4 + factor,
  1017.                              sizeCell - 8, sizeCell - 8);
  1018.     }
  1019.  
  1020.  
  1021.     /**
  1022.      * get left top point of button's area
  1023.      * @param i : button index in Bar
  1024.      * @return left top point of button's area
  1025.      */
  1026.     public Point  getCellPos(int i)
  1027.     {
  1028.         int  startPos;
  1029.         
  1030.         startPos = i*sizeCell;
  1031.         return new Point(factorX*startPos, factorY*startPos);
  1032.     }
  1033.  
  1034.     /**
  1035.      * get button's index by position
  1036.      * @param x: x position
  1037.      * @param y: y position
  1038.      * @return button's index
  1039.      */
  1040.     protected int getButtonIndex(int x, int y)
  1041.     {
  1042.         if (direction == HORIZONTAL && x < gabBarCell + sizeCell*count && 
  1043.             y > gabBarCell && y < sizeBar - gabBarCell)
  1044.         {
  1045.             return  (x - gabBarCell) / sizeCell;
  1046.         }
  1047.         else if (direction == VERTICAL && y < gabBarCell + sizeCell*count && 
  1048.                  x > gabBarCell && x < sizeBar - gabBarCell)
  1049.         {
  1050.             return  (y - gabBarCell) / sizeCell;
  1051.         }
  1052.         return -1;  
  1053.     }
  1054.  
  1055.     public boolean mouseDown(Event e, int x, int y)
  1056.     {
  1057.         int  i = getButtonIndex(x, y);
  1058.         if (i != -1)
  1059.             pressButton(i);
  1060.  
  1061.         return true;
  1062.     }
  1063.     /*
  1064.      * Show help in status line
  1065.      */
  1066.     public boolean mouseMove(Event e, int x, int y)
  1067.     {
  1068.         if (help == null)
  1069.         {
  1070.             if (curHelp != null)
  1071.                 WorkPanel.applet.showStatus("");
  1072.             return true;
  1073.         }
  1074.  
  1075.         int  i = getButtonIndex(x, y);
  1076.  
  1077.         if (i != -1)
  1078.         {
  1079.             if (curHelp != help[i]) 
  1080.                 WorkPanel.applet.showStatus(help[i]);
  1081.             curHelp = help[i];
  1082.         }
  1083.         else
  1084.         {
  1085.             if (curHelp != null)
  1086.                 WorkPanel.applet.showStatus("");
  1087.             curHelp = null;
  1088.         }
  1089.         return true;
  1090.     }
  1091. }
  1092.  
  1093.  
  1094. /**
  1095.  * A Bar class that select one button
  1096.  *
  1097.  * @version     1.0 03/24/96
  1098.  * @author         Kang, Dae Woong (gothic@star.elim.net)
  1099.  */
  1100.  
  1101. abstract class ChoiceBar extends Bar
  1102. {
  1103.     /** 
  1104.      * selected item index 
  1105.      */
  1106.     int curSelected = 0;
  1107.     
  1108.     /**
  1109.      * Constructs a Bar with button count and direction
  1110.      * @param count : button's count
  1111.      * @param direction : bars direction (VERTICAL, HORIZONTAL)
  1112.      */
  1113.     public ChoiceBar(int count, int direction)
  1114.     {
  1115.         super(count, direction);
  1116.     }
  1117.  
  1118.     /**
  1119.      * implement's abstract method in Bar
  1120.      * @param i : button index in Bar
  1121.      * @see Bar
  1122.      */
  1123.     public boolean isButtonUp(int i)
  1124.     {
  1125.         return i != curSelected;
  1126.     }
  1127.  
  1128.     /**
  1129.      * implement's abstract method in Bar
  1130.      * @param i : button index in Bar
  1131.      * @see Bar
  1132.      */
  1133.     public void pressButton(int i)
  1134.     {
  1135.         if (curSelected != i)
  1136.         {
  1137.             Graphics g = getGraphics();
  1138.             
  1139.             clearCell(g, curSelected);
  1140.             drawUpCell(g, curSelected);
  1141.             curSelected = i;
  1142.             clearCell(g, curSelected);
  1143.             drawDownCell(g, curSelected);
  1144.  
  1145.             g.dispose();
  1146.  
  1147.             select(i); //call child's function
  1148.         }
  1149.     }
  1150. }
  1151.  
  1152. /**
  1153.  * A Bar class that run command
  1154.  * it act like button
  1155.  *
  1156.  * @version     1.0 03/24/96
  1157.  * @author         Kang, Dae Woong (gothic@star.elim.net)
  1158.  */
  1159. abstract class CommandBar extends Bar
  1160. {
  1161.     /** 
  1162.      * selected item index 
  1163.      */
  1164.     int curSelected = -1;
  1165.     
  1166.     /**
  1167.      * Constructs a Bar with button count and direction
  1168.      * @param count : button's count
  1169.      * @param direction : bars direction (VERTICAL, HORIZONTAL)
  1170.      */
  1171.     public CommandBar(int count, int direction)
  1172.     {
  1173.         super(count, direction);
  1174.     }
  1175.  
  1176.     /**
  1177.      * implement's abstract method in Bar
  1178.      * @param i : button index in Bar
  1179.      * @see Bar
  1180.      */
  1181.     public boolean isButtonUp(int i)
  1182.     {
  1183.         return true;
  1184.     }
  1185.  
  1186.     /**
  1187.      * implement's abstract method in Bar
  1188.      * @param i : button index in Bar
  1189.      * @see Bar
  1190.      */
  1191.     public void pressButton(int i)
  1192.     {
  1193.         Graphics g = getGraphics();
  1194.  
  1195.         curSelected = i;
  1196.         clearCell(g, i);
  1197.         drawDownCell(g, i);
  1198.  
  1199.         g.dispose();
  1200.     }
  1201.     
  1202.     /**
  1203.      * run command 
  1204.      */
  1205.     public boolean mouseUp(Event e, int x, int y)
  1206.     {
  1207.         int  i = getButtonIndex(x, y);
  1208.         if (i == curSelected && i != -1)
  1209.         {
  1210.             select(i);
  1211.         }
  1212.         if (i != -1)
  1213.         {
  1214.             Graphics g = getGraphics();
  1215.  
  1216.             curSelected = i;
  1217.             clearCell(g, i);
  1218.             drawUpCell(g, i);
  1219.  
  1220.             g.dispose();
  1221.         }
  1222.         curSelected = -1;
  1223.         return true;
  1224.     }
  1225.  
  1226.     /**
  1227.      * cancel command if point is out of button
  1228.      */
  1229.     public boolean mouseDrag(Event e, int x, int y)
  1230.     {
  1231.         int  i = getButtonIndex(x, y);
  1232.  
  1233.         if (curSelected != -1 && i != curSelected)
  1234.         {
  1235.             Graphics g = getGraphics();
  1236.  
  1237.             clearCell(g, curSelected);
  1238.             drawUpCell(g, curSelected);
  1239.  
  1240.             g.dispose();
  1241.  
  1242.             curSelected = -1;
  1243.         }
  1244.         return true;
  1245.     }
  1246. }