home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / WDESAMPL.BIN / Graph.java < prev    next >
Text File  |  1997-04-04  |  10KB  |  371 lines

  1. /*
  2.  * @(#)Graph.java    1.3 96/12/06
  3.  *
  4.  * Copyright (c) 1994-1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
  7.  * modify and redistribute this software in source and binary code form,
  8.  * provided that i) this copyright notice and license appear on all copies of
  9.  * the software; and ii) Licensee does not utilize the software in a manner
  10.  * which is disparaging to Sun.
  11.  *
  12.  * This software is provided "AS IS," without a warranty of any kind. ALL
  13.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
  14.  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
  15.  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
  16.  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  17.  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
  18.  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
  19.  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
  20.  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
  21.  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
  22.  * POSSIBILITY OF SUCH DAMAGES.
  23.  *
  24.  * This software is not designed or intended for use in on-line control of
  25.  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
  26.  * the design, construction, operation or maintenance of any nuclear
  27.  * facility. Licensee represents and warrants that it will not use or
  28.  * redistribute the Software for such purposes.
  29.  */
  30.  
  31. import java.util.*;
  32. import java.awt.*;
  33. import java.applet.Applet;
  34.  
  35. class Node {
  36.     double x;
  37.     double y;
  38.  
  39.     double dx;
  40.     double dy;
  41.  
  42.     boolean fixed;
  43.  
  44.     String lbl;
  45. }
  46.  
  47. class Edge {
  48.     int from;
  49.     int to;
  50.  
  51.     double len;
  52. }
  53.  
  54. class GraphPanel extends Panel implements Runnable {
  55.     Graph graph;
  56.     int nnodes;
  57.     Node nodes[] = new Node[100];
  58.  
  59.     int nedges;
  60.     Edge edges[] = new Edge[200];
  61.  
  62.     Thread relaxer;
  63.     boolean stress;
  64.     boolean random;
  65.  
  66.     GraphPanel(Graph graph) {
  67.     this.graph = graph;
  68.     }
  69.  
  70.     int findNode(String lbl) {
  71.     for (int i = 0 ; i < nnodes ; i++) {
  72.         if (nodes[i].lbl.equals(lbl)) {
  73.         return i;
  74.         }
  75.     }
  76.     return addNode(lbl);
  77.     }
  78.     int addNode(String lbl) {
  79.     Node n = new Node();
  80.     n.x = 10 + 380*Math.random();
  81.     n.y = 10 + 380*Math.random();
  82.     n.lbl = lbl;
  83.     nodes[nnodes] = n;
  84.     return nnodes++;
  85.     }
  86.     void addEdge(String from, String to, int len) {
  87.     Edge e = new Edge();
  88.     e.from = findNode(from);
  89.     e.to = findNode(to);
  90.     e.len = len;
  91.     edges[nedges++] = e;
  92.     }
  93.  
  94.     public void run() {
  95.     while (true) {
  96.         relax();
  97.         if (random && (Math.random() < 0.03)) {
  98.         Node n = nodes[(int)(Math.random() * nnodes)];
  99.         if (!n.fixed) {
  100.             n.x += 100*Math.random() - 50;
  101.             n.y += 100*Math.random() - 50;
  102.         }
  103.         graph.play(graph.getCodeBase(), "audio/drip.au");
  104.         }
  105.         try {
  106.         Thread.sleep(100);
  107.         } catch (InterruptedException e) {
  108.         break;
  109.         }
  110.     }
  111.     }
  112.  
  113.     synchronized void relax() {
  114.     for (int i = 0 ; i < nedges ; i++) {
  115.         Edge e = edges[i];
  116.         double vx = nodes[e.to].x - nodes[e.from].x;
  117.         double vy = nodes[e.to].y - nodes[e.from].y;
  118.         double len = Math.sqrt(vx * vx + vy * vy);
  119.         double f = (edges[i].len - len) / (len * 3) ;
  120.         double dx = f * vx;
  121.         double dy = f * vy;
  122.  
  123.         nodes[e.to].dx += dx;
  124.         nodes[e.to].dy += dy;
  125.         nodes[e.from].dx += -dx;
  126.         nodes[e.from].dy += -dy;
  127.     }
  128.  
  129.     for (int i = 0 ; i < nnodes ; i++) {
  130.         Node n1 = nodes[i];
  131.         double dx = 0;
  132.         double dy = 0;
  133.  
  134.         for (int j = 0 ; j < nnodes ; j++) {
  135.         if (i == j) {
  136.             continue;
  137.         }
  138.         Node n2 = nodes[j];
  139.         double vx = n1.x - n2.x;
  140.         double vy = n1.y - n2.y;
  141.         double len = vx * vx + vy * vy;
  142.         if (len == 0) {
  143.             dx += Math.random();
  144.             dy += Math.random();
  145.         } else if (len < 100*100) {
  146.             dx += vx / len;
  147.             dy += vy / len;
  148.         }
  149.         }
  150.         double dlen = dx * dx + dy * dy;
  151.         if (dlen > 0) {
  152.         dlen = Math.sqrt(dlen) / 2;
  153.         n1.dx += dx / dlen;
  154.         n1.dy += dy / dlen;
  155.         }
  156.     }
  157.  
  158.     Dimension d = size();
  159.     for (int i = 0 ; i < nnodes ; i++) {
  160.         Node n = nodes[i];
  161.         if (!n.fixed) {
  162.         n.x += Math.max(-5, Math.min(5, n.dx));
  163.         n.y += Math.max(-5, Math.min(5, n.dy));
  164.         //System.out.println("v= " + n.dx + "," + n.dy);
  165.         if (n.x < 0) {
  166.             n.x = 0;
  167.         } else if (n.x > d.width) {
  168.             n.x = d.width;
  169.         }
  170.         if (n.y < 0) {
  171.             n.y = 0;
  172.         } else if (n.y > d.height) {
  173.             n.y = d.height;
  174.         }
  175.         }
  176.         n.dx /= 2;
  177.         n.dy /= 2;
  178.     }
  179.     repaint();
  180.     }
  181.  
  182.     Node pick;
  183.     boolean pickfixed;
  184.     Image offscreen;
  185.     Dimension offscreensize;
  186.     Graphics offgraphics;
  187.     
  188.  
  189.     final Color fixedColor = Color.red;
  190.     final Color selectColor = Color.pink;
  191.     final Color edgeColor = Color.black;
  192.     final Color nodeColor = new Color(250, 220, 100);
  193.     final Color stressColor = Color.darkGray;
  194.     final Color arcColor1 = Color.black;
  195.     final Color arcColor2 = Color.pink;
  196.     final Color arcColor3 = Color.red;
  197.  
  198.     public void paintNode(Graphics g, Node n, FontMetrics fm) {
  199.     int x = (int)n.x;
  200.     int y = (int)n.y;
  201.     g.setColor((n == pick) ? selectColor : (n.fixed ? fixedColor : nodeColor));
  202.     int w = fm.stringWidth(n.lbl) + 10;
  203.     int h = fm.getHeight() + 4;
  204.     g.fillRect(x - w/2, y - h / 2, w, h);
  205.     g.setColor(Color.black);
  206.     g.drawRect(x - w/2, y - h / 2, w-1, h-1);
  207.     g.drawString(n.lbl, x - (w-10)/2, (y - (h-4)/2) + fm.getAscent());
  208.     }
  209.  
  210.     public synchronized void update(Graphics g) {
  211.     Dimension d = size();
  212.     if ((offscreen == null) || (d.width != offscreensize.width) || (d.height != offscreensize.height)) {
  213.         offscreen = createImage(d.width, d.height);
  214.         offscreensize = d;
  215.         offgraphics = offscreen.getGraphics();
  216.         offgraphics.setFont(getFont());
  217.     }
  218.  
  219.     offgraphics.setColor(getBackground());
  220.     offgraphics.fillRect(0, 0, d.width, d.height);
  221.     for (int i = 0 ; i < nedges ; i++) {
  222.         Edge e = edges[i];
  223.         int x1 = (int)nodes[e.from].x;
  224.         int y1 = (int)nodes[e.from].y;
  225.         int x2 = (int)nodes[e.to].x;
  226.         int y2 = (int)nodes[e.to].y;
  227.         int len = (int)Math.abs(Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)) - e.len);
  228.         offgraphics.setColor((len < 10) ? arcColor1 : (len < 20 ? arcColor2 : arcColor3)) ;
  229.         offgraphics.drawLine(x1, y1, x2, y2);
  230.         if (stress) {
  231.         String lbl = String.valueOf(len);
  232.         offgraphics.setColor(stressColor);
  233.         offgraphics.drawString(lbl, x1 + (x2-x1)/2, y1 + (y2-y1)/2);
  234.         offgraphics.setColor(edgeColor);
  235.         }
  236.     }
  237.  
  238.     FontMetrics fm = offgraphics.getFontMetrics();
  239.     for (int i = 0 ; i < nnodes ; i++) {
  240.         paintNode(offgraphics, nodes[i], fm);
  241.     }
  242.  
  243.     g.drawImage(offscreen, 0, 0, null);
  244.     }
  245.  
  246.     public synchronized boolean mouseDown(Event evt, int x, int y) {
  247.     double bestdist = Double.MAX_VALUE;
  248.     for (int i = 0 ; i < nnodes ; i++) {
  249.         Node n = nodes[i];
  250.         double dist = (n.x - x) * (n.x - x) + (n.y - y) * (n.y - y);
  251.         if (dist < bestdist) {
  252.         pick = n;
  253.         bestdist = dist;
  254.         }
  255.     }
  256.     pickfixed = pick.fixed;
  257.     pick.fixed = true;
  258.     pick.x = x;
  259.     pick.y = y;
  260.     repaint();
  261.     return true;
  262.     }
  263.  
  264.     public synchronized boolean mouseDrag(Event evt, int x, int y) {
  265.     pick.x = x;
  266.     pick.y = y;
  267.     repaint();
  268.     return true;
  269.     }
  270.  
  271.     public synchronized boolean mouseUp(Event evt, int x, int y) {
  272.     pick.x = x;
  273.     pick.y = y;
  274.     pick.fixed = pickfixed;
  275.     pick = null;
  276.     
  277.     repaint();
  278.     return true;
  279.     }
  280.  
  281.     public void start() {
  282.     relaxer = new Thread(this);
  283.     relaxer.start();
  284.     }
  285.     public void stop() {
  286.     relaxer.stop();
  287.     }
  288. }
  289.  
  290. public class Graph extends Applet {
  291.     GraphPanel panel;
  292.  
  293.     public void init() {
  294.     setLayout(new BorderLayout());
  295.  
  296.     panel = new GraphPanel(this);
  297.     add("Center", panel);
  298.     Panel p = new Panel();
  299.     add("South", p);
  300.     p.add(new Button("Scramble"));
  301.     p.add(new Button("Shake"));
  302.     p.add(new Checkbox("Stress"));
  303.     p.add(new Checkbox("Random"));
  304.  
  305.     String edges = getParameter("edges");
  306.     for (StringTokenizer t = new StringTokenizer(edges, ",") ; t.hasMoreTokens() ; ) {
  307.         String str = t.nextToken();
  308.         int i = str.indexOf('-');
  309.         if (i > 0) {
  310.         int len = 50;
  311.         int j = str.indexOf('/');
  312.         if (j > 0) {
  313.             len = Integer.valueOf(str.substring(j+1)).intValue();
  314.             str = str.substring(0, j);
  315.         }
  316.         panel.addEdge(str.substring(0,i), str.substring(i+1), len);
  317.         }
  318.     }
  319.     Dimension d = size();
  320.     String center = getParameter("center");
  321.     if (center != null){
  322.         Node n = panel.nodes[panel.findNode(center)];
  323.         n.x = d.width / 2;
  324.         n.y = d.height / 2;
  325.         n.fixed = true;
  326.     }
  327.     }
  328.  
  329.     public void start() {
  330.     panel.start();
  331.     }
  332.     public void stop() {
  333.     panel.stop();
  334.     }
  335.     public boolean action(Event evt, Object arg) {
  336.     if (arg instanceof Boolean) {
  337.         if (((Checkbox)evt.target).getLabel().equals("Stress")) {
  338.         panel.stress = ((Boolean)arg).booleanValue();
  339.         } else {
  340.         panel.random = ((Boolean)arg).booleanValue();
  341.         }
  342.         return true;
  343.     } 
  344.     if ("Scramble".equals(arg)) {
  345.         play(getCodeBase(), "audio/computer.au");
  346.         Dimension d = size();
  347.         for (int i = 0 ; i < panel.nnodes ; i++) {
  348.         Node n = panel.nodes[i];
  349.         if (!n.fixed) {
  350.             n.x = 10 + (d.width-20)*Math.random();
  351.             n.y = 10 + (d.height-20)*Math.random();
  352.         }
  353.         }
  354.         return true;
  355.     }
  356.     if ("Shake".equals(arg)) {
  357.         play(getCodeBase(), "audio/gong.au");
  358.         Dimension d = size();
  359.         for (int i = 0 ; i < panel.nnodes ; i++) {
  360.         Node n = panel.nodes[i];
  361.         if (!n.fixed) {
  362.             n.x += 80*Math.random() - 40;
  363.             n.y += 80*Math.random() - 40;
  364.         }
  365.         }
  366.         return true;
  367.     }
  368.     return false;
  369.     }
  370. }
  371.