home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / in4wjcxu / src / como / commlet / draw / draw.java.org < prev    next >
Encoding:
Text File  |  1996-08-14  |  11.2 KB  |  469 lines

  1. package como.commlet.draw;
  2.  
  3. import java.awt.*;
  4. import java.util.*;
  5. import java.io.IOException;
  6. import como.sys.*;
  7. import como.util.*;
  8. import como.awt.*;
  9. import como.io.*;
  10. import como.commlet.*;
  11.  
  12. /*
  13.  
  14. Scheoene menues
  15. fontauswahl
  16. text akzeptieren -- semantik????
  17. */
  18.  
  19. public class Draw extends WindowCommlet implements DrawObserver {
  20.  
  21.     DrawCanvas draw;            // a draw canvas that knows nothing about networking, but informs <this>
  22.     DrawControls controls;
  23.     // whenever its objects are changed
  24.  
  25.     static final int MSG_DRAW_NEW_OBJECT     = 1001; // arg = new GO
  26.     static final int MSG_DRAW_DEL_OBJECT     = 1002; // arg = key
  27.  
  28.     public String getCommletName() {
  29.         return("Draw");
  30.     }
  31.  
  32.     public String getCommletInfo() {
  33.         return("Draw Comlet, V 1.0, Jan Kautz & Ulrich Gall");
  34.     }
  35.  
  36.     public void init()     {
  37.         super.init();
  38.         draw = new DrawCanvas(this);
  39.         add("Center",draw);
  40.         controls = new DrawControls(draw); // needs to tell draw what happens
  41.         add("West",controls);
  42.         draw.setControls(controls);
  43.         resize(400,300);
  44.     }
  45.  
  46.     public void addUser(int who) { 
  47.         Debug.msg(1,"Draw.addUser() who = "+who);
  48.         User u = com.getUser(who);
  49.         String name = u.get(u.NAME).toString();
  50.         if (!u.containsKey(u.COLOR))
  51.         {
  52.             Color c = new Color(u.hashCode());
  53.             if (c.getRed() + c.getBlue() + c.getGreen() < 150)
  54.             c = new Color(255-c.getRed(),
  55.             255-c.getGreen(),
  56.             255-c.getBlue());
  57.             u.put(u.COLOR,c);
  58.         }
  59.         Color color  = (Color)u.get(u.COLOR);
  60.         draw.addObject(new Integer(who),new GOGroup(name,color));
  61.         if (com.getMyID() == who) 
  62.         draw.setCurrentGroupKey( new Integer(com.getMyID()) );
  63.         else {
  64.             // Somebody other than myself joined us, so I will send him the contents of my GOGroup
  65.             Enumeration e = ((GOGroup)draw.getGroup( new Integer(com.getMyID()) ) ).elements();
  66.             while (e.hasMoreElements()) {
  67.                 Msg m = new Msg(MSG_DRAW_NEW_OBJECT,(GraphicsObject)e.nextElement());
  68.                 m.to = who;
  69.                 com.sendTo(m);                
  70.             }
  71.             if (draw.colormode == draw.COLORMODE_OWNER) {
  72.                  controls.updateColormodepanel();
  73.             }
  74.         }
  75.     }
  76.  
  77.     public void userLeft(int who) { 
  78.     int i ;
  79.     // TODO 
  80. }
  81.  
  82.     public void newObject(GraphicsObject g) {
  83.         com.sendToOthers(new Msg(MSG_DRAW_NEW_OBJECT,g));
  84.     }
  85.  
  86.     public void delObject(Object key) {
  87.         com.sendToOthers(new Msg(MSG_DRAW_DEL_OBJECT,key));
  88.     }
  89.  
  90.     public boolean handleMsg(Msg msg)    {
  91.         if (super.handleMsg(msg)) return true;
  92.         String name = com.getUserName(msg.from);
  93.         Debug.msg(1,"Draw.handleMsg() : msg from " + name + " = "+msg);
  94.         if (msg.type == MSG_DRAW_NEW_OBJECT)
  95.         {
  96.             Debug.msg(10,"Draw.handleMsg() appending " + msg.arg + "  to " + msg.from);
  97.             draw.appendToGroup(new Integer(msg.from),(GraphicsObject)msg.arg);
  98.             //            draw.paintObject((GraphicsObject)msg.arg);
  99.             draw.repaint();
  100.             return true;
  101.         }
  102.         else if (msg.type == MSG_DRAW_DEL_OBJECT)
  103.         {
  104.             draw.delFromGroup(new Integer(msg.from),msg.arg); // msg.arg = toDelete.hashCode()
  105.             draw.repaint();
  106.             return true;
  107.         }
  108.         else if (msg.type == Msg.NEW_USER_INFO)
  109.         {
  110.             User u = (User)msg.arg;
  111.             draw.getGroup(u.get(u.ID)).name = u.get(u.NAME).toString();
  112.             draw.getGroup(u.get(u.ID)).setColor((Color)u.get(u.COLOR));
  113.             controls.updateColormodepanel();
  114.             draw.repaint();
  115.             return true;
  116.         }
  117.         return false;
  118.     }
  119. }
  120.  
  121. /**********************
  122. * DrawCanvas
  123. *
  124. *
  125. * The following methods are called by the Commlet
  126. *
  127. * addObject
  128. * appendToGroup
  129. * delFromGroup
  130. * getGroup
  131. * setCurrentGroupKey
  132. * setControls
  133. *
  134. * observer is informed about changes in objects. 
  135. * User actions may only alter currentgroup
  136. *
  137. */
  138.  
  139. class DrawCanvas extends Canvas
  140. {
  141.     public static final int EVENT_ACCEPT = 150173;
  142.  
  143.     GOGroup         objects;
  144.     DrawObserver     observer;
  145.     DrawControls     controls;
  146.  
  147.     GraphicsObject currentGO;
  148.     Object             currentGroupKey;
  149.  
  150.     String             colormode;
  151.  
  152.     // Color Modes
  153.     static String COLORMODE_OWNER = "Owner│s";
  154.     static String COLORMODE_OBJECT = "Object│s";
  155.  
  156.     // keys
  157.     static final int KEY_PASTE         = 112;
  158.     static final int KEY_CUT             = 127;
  159.     static final int KEY_COPY         = 99;
  160.     static final int KEY_DUMP         = 68;
  161.  
  162.     public DrawCanvas(DrawObserver d)
  163.     {
  164.         setBackground(Color.black);
  165.         colormode = COLORMODE_OBJECT;
  166.         observer = d;
  167.         objects = new GOGroup();
  168.         currentGO = null;
  169.         currentGroupKey = null;
  170.     }
  171.  
  172.     public void setControls(DrawControls c) {
  173.         controls = c;
  174.     }
  175.  
  176.     public void setCurrentGroupKey(Object group)
  177.     {
  178.         currentGroupKey = group;
  179.     }
  180.  
  181.     public GraphicsObject getCurrentGroup()
  182.     {
  183.         return (GraphicsObject)objects.get(currentGroupKey);
  184.     }
  185.  
  186.     public void setCurrentGO(GraphicsObject go)
  187.     {
  188.         currentGO = go;
  189.     }
  190.  
  191.     public void setColormode(String m)
  192.     {
  193.         colormode = m;    
  194.         repaint();
  195.     }
  196.  
  197.     public void paint(Graphics g)
  198.     {
  199.         Enumeration e = objects.keys();
  200.         while (e.hasMoreElements())
  201.         {
  202.             Object o = e.nextElement();
  203.             GraphicsObject go = (GraphicsObject)objects.get(o);
  204.             if (colormode.compareTo(COLORMODE_OBJECT) == 0) go.draw(g); 
  205.             else go.draw(g,go.getColor());
  206.         }
  207.         if (currentGO != null) {
  208.             if (colormode.compareTo(COLORMODE_OBJECT) == 0) currentGO.draw(g); 
  209.             else currentGO.draw(g,getCurrentGroup().getColor());
  210.         }
  211.     }
  212.  
  213.     public void paintObject(GraphicsObject g)
  214.     {
  215.         if (getGraphics() != null) {
  216.             if (g != null) {
  217.                 if (colormode.compareTo(COLORMODE_OBJECT) == 0) currentGO.draw(getGraphics()); 
  218.                 else currentGO.draw(getGraphics(),getCurrentGroup().getColor());
  219.             }
  220.         }
  221.     }
  222.  
  223.     /*************************************************************
  224.     * Adding and removing objects
  225.     */
  226.     public void addObject(Object key,GraphicsObject g)
  227.     {
  228.         objects.put(key,g);
  229.         paintObject(g);
  230.     }
  231.  
  232.     public GraphicsObject getGroup(Object key)
  233.     {
  234.         return (GraphicsObject)objects.get(key);
  235.     }
  236.  
  237.     public void appendToGroup(Object key,GraphicsObject g) 
  238.     {
  239.         GraphicsObject o = objects.get(key);
  240.         if (o instanceof GOGroup) {
  241.             GOGroup group = (GOGroup)o;
  242.             group.put(new Integer(g.hashCode()),g);
  243.             paintObject(g);
  244.         }
  245.         else {
  246.             Debug.msg(90,"DrawPanel.appendToGroup: Not a group: "    + o.toString());
  247.         }
  248.     }
  249.  
  250.     public void delFromGroup(Object gk,Object key)
  251.     {
  252.         GOGroup g = (GOGroup) objects.get(gk);
  253.         g.remove(key);
  254.         repaint();
  255.     }
  256.  
  257.     /******************************************************************************************
  258.     * Event handling
  259.     */
  260.     public boolean handleEvent(Event evt) {
  261.         Debug.msg(0,"DrawCanvas.handleEvent(): evt = "+evt);
  262.         /*        if (evt.id == Event.KEY_PRESS) {
  263.             if (evt.key == KEY_DUMP)
  264.             {
  265.                 Debug.msg(100,"Draw: objects = "+objects);
  266.                 Debug.msg(100,"Draw: Colormode =  "+colormode);
  267.                 if (currentGO == null) 
  268.                 Debug.msg(100,"No currentGO");
  269.                 else 
  270.                 Debug.msg(100,"currentGO = "+currentGO.toString());
  271.                 return true;
  272.             }
  273.         }        */
  274.         if (currentGO != null) {
  275.             {
  276.                 if (currentGO.handleEvent(evt))
  277.                 {
  278.                     if (evt.id == EVENT_ACCEPT)
  279.                     {
  280.                         // This means we should accept the currentGO and start a new one
  281.                         appendToGroup(currentGroupKey    ,currentGO);
  282.                         observer.newObject(currentGO);
  283.                         paintObject(currentGO);
  284.                         currentGO = currentGO.getNew();
  285.                         currentGO.color = controls.currentcolor;
  286.                         currentGO.filled = controls.filled;
  287.                         currentGO.moveTo(evt.x,evt.y);
  288.                     }
  289.                     paintObject(currentGO);
  290.                     return true;
  291.                 }
  292.             }
  293.         }
  294.         return false;                
  295.     }
  296. }
  297.  
  298. /*******************************************************************************************
  299. * DrawControls
  300. */
  301.  
  302. class DrawControls extends Panel {
  303.  
  304.     // modes
  305.     static String MODE_POLYGON     = "Polygon";
  306.     static String MODE_TEXT        = "Text";
  307.  
  308.     DrawCanvas drawcanvas;
  309.  
  310.     Choice     drawmodeChoice;
  311.     Choice    colormodeChoice;
  312.  
  313.     Panel colormodepanel;
  314.     Panel drawmodepanel;
  315.         
  316.     Checkbox filledCB;
  317.     ColorSelector colorsel;
  318.         
  319.     boolean filled;
  320.     Color currentcolor;
  321.     Color fillColor;
  322.  
  323.     public DrawControls(DrawCanvas dc) {
  324.         drawcanvas = dc;
  325.             
  326.         setLayout(new VertLayout(VertLayout.STRETCH));
  327.         add(new Label("Draw Mode:"));
  328.         drawmodeChoice = new Choice();
  329.         drawmodeChoice.addItem(MODE_POLYGON);
  330.         drawmodeChoice.addItem(MODE_TEXT);
  331.         add(drawmodeChoice);
  332.  
  333.         drawmodepanel = new Panel();
  334.         add(drawmodepanel);
  335.  
  336.         currentcolor = Color.white;
  337.         add(new Label("Current Color"));
  338.         colorsel = new ColorSelector(currentcolor);
  339.         add(colorsel);
  340.             
  341.         filled =    false;
  342.         filledCB = new Checkbox("Filled");
  343.         filledCB.setState(filled);
  344.         add(filledCB);
  345.  
  346.         add(new Label("Color Mode"));
  347.         colormodeChoice = new Choice();
  348.         colormodeChoice.addItem(dc.COLORMODE_OBJECT);
  349.         colormodeChoice.addItem(dc.COLORMODE_OWNER);
  350.         add(colormodeChoice);
  351.         colormodepanel = new Panel();
  352.         updateColormodepanel();
  353.         add(colormodepanel);
  354.          
  355.         GraphicsObject cur = new GOPolygon();
  356.         cur.setColor(currentcolor);
  357.         cur.setFilled(filled);
  358.         setCurrentGO(cur);
  359.  
  360.     }
  361.  
  362.     public void setCurrentGO(GraphicsObject cur)
  363.     {
  364.         cur.layoutPropPanel(drawmodepanel);            
  365.         layout();
  366.         paintAll(getGraphics());
  367.         drawcanvas.setCurrentGO(cur);
  368.     }
  369.  
  370.     public void setCurrentColor(Color c) {
  371.         currentcolor = c;
  372.         if (drawcanvas.currentGO != null) drawcanvas.currentGO.setColor(c);
  373.     }
  374.  
  375.     public void updateColormodepanel() {
  376.         colormodepanel.removeAll();
  377.         if (drawcanvas.colormode.compareTo(DrawCanvas.COLORMODE_OWNER) == 0)
  378.         {
  379.             colormodepanel.setLayout(new VertLayout(VertLayout.STRETCH));
  380.             Enumeration e = drawcanvas.objects.elements();
  381.             while (e.hasMoreElements()) {
  382.                 GraphicsObject g  = (GraphicsObject)e.nextElement();
  383.                 ColorView c = new ColorView(g.getColor(),g.name);
  384.                 colormodepanel.add(c);
  385.             }
  386.         colormodepanel.paintAll(colormodepanel.getGraphics());
  387.         }
  388.         layout();
  389.     }
  390.  
  391.     public boolean action(Event evt,Object arg) {
  392.         
  393.         if (evt.target == drawmodeChoice) {
  394.             String mode= drawmodeChoice.getSelectedItem();
  395.             GraphicsObject cur = null;
  396.             if (mode == MODE_POLYGON) cur = new GOPolygon();
  397.             if (mode == MODE_TEXT) cur = new GOText();
  398.             cur.setColor(currentcolor);
  399.             cur.setFilled(filled);
  400.             setCurrentGO(cur);
  401.             return true;
  402.         }
  403.         if (evt.target == colormodeChoice)  {
  404.             drawcanvas.setColormode(colormodeChoice.getSelectedItem());
  405.             if (colormodeChoice.getSelectedItem().compareTo(DrawCanvas.COLORMODE_OWNER) == 0)
  406.             currentcolor = drawcanvas.getCurrentGroup().getColor();
  407.             updateColormodepanel();
  408.             return true;
  409.         }
  410.         if (evt.target == filledCB) {
  411.             filled = filledCB.getState();
  412.             if (drawcanvas.currentGO != null) {
  413.                 drawcanvas.currentGO.setFilled(filled);
  414.                 drawcanvas.paintObject(drawcanvas.currentGO);
  415.             }
  416.         }
  417.         if (evt.target == colorsel) {
  418.             setCurrentColor(colorsel.getColor());
  419.         }
  420.         return false;
  421.     }
  422.  
  423.     public boolean handleEvent(Event evt) {
  424.         boolean sup =  super.handleEvent(evt);
  425.         // TODO: This is not necessarily for currentGO
  426.         GraphicsObject cur = drawcanvas.currentGO;
  427.         boolean b = false;
  428.         if (cur != null) {
  429.             b = cur.handlePropPanelEvent(evt,drawcanvas);
  430.         }
  431.         return (sup || b);
  432.     }
  433. }
  434.  
  435. class ColorView extends Canvas {
  436.     String name;
  437.     static final int SPACE_BOTTOM = 6;
  438.     static final int SPACE_LEFT = 10;
  439.  
  440.     public ColorView(Color c, String n) {
  441.         name = n;
  442.         setBackground(c);
  443.         if ((c.getRed()+c.getGreen()+c.getBlue()) < 384) setForeground(Color.white);
  444.             else setForeground(Color.black);
  445.     }
  446.  
  447.     public void paint(Graphics g) {
  448.         g.drawString(name,SPACE_LEFT,size().height-SPACE_BOTTOM);
  449.     }
  450.  
  451.     public Dimension preferredSize() {
  452.         FontMetrics fm = getFontMetrics(getFont());    
  453.         return new Dimension(2*SPACE_LEFT+fm.stringWidth(name),
  454.                 fm.getHeight()+2*SPACE_BOTTOM);
  455.     }
  456.  
  457.     public Dimension minimumSize() {
  458.         return preferredSize();
  459.     }
  460. }
  461.  
  462. /******************************************************************************************/
  463. interface DrawObserver {
  464.  
  465.     void newObject(GraphicsObject g);
  466.  
  467.     void delObject(Object key);
  468. }
  469.