1 cananian 1.3.2.4 // PartitionGraphViewer.java, created Mon Nov 16 23:33:21 1998 by mfoltz 2 cananian 1.3.2.4 // Copyright (C) 1998 Mark A. Foltz <mfoltz@ai.mit.edu> 3 cananian 1.3.2.4 // Licensed under the terms of the GNU GPL; see COPYING for details. 4 cananian 1.3.2.4 5 mfoltz 1.1 // -*-Mode: Java-*- 6 mfoltz 1.1 // PartitionGraphViewer.java -- 7 mfoltz 1.1 // Author: Mark Foltz <mfoltz@ai.mit.edu> 8 mfoltz 1.1 // Maintainer: Mark Foltz <mfoltz@ai.mit.edu> 9 mfoltz 1.1 // Version: 10 mfoltz 1.1 // Created: <Sun Oct 25 16:14:14 1998> 11 mfoltz 1.4 // Time-stamp: <1998-11-26 13:51:07 mfoltz> 12 mfoltz 1.1 // Keywords: 13 mfoltz 1.1 14 mfoltz 1.2 package harpoon.Analysis.Partition; 15 mfoltz 1.1 16 cananian 1.3.2.5 import java.awt.Button; 17 cananian 1.3.2.5 import java.awt.Color; 18 cananian 1.3.2.5 import java.awt.Dimension; 19 cananian 1.3.2.5 import java.awt.Font; 20 cananian 1.3.2.5 import java.awt.Frame; 21 cananian 1.3.2.5 import java.awt.Graphics; 22 cananian 1.3.2.5 import java.awt.Panel; 23 cananian 1.3.2.5 import java.awt.Point; 24 mfoltz 1.1 import java.awt.event.ActionListener; 25 mfoltz 1.1 import java.awt.event.ActionEvent; 26 mfoltz 1.1 import java.util.Observer; 27 mfoltz 1.1 import java.util.Observable; 28 mfoltz 1.1 import java.util.Vector; 29 mfoltz 1.1 import java.util.Enumeration; 30 mfoltz 1.1 import java.util.Hashtable; 31 mfoltz 1.1 32 mfoltz 1.1 /** 33 mfoltz 1.1 * 34 mfoltz 1.1 * @author Mark A. Foltz <mfoltz@ai.mit.edu> 35 cananian 1.5 * @version $Id: PartitionGraphViewer.java,v 1.5 2002/02/25 20:58:28 cananian Exp $ 36 mfoltz 1.1 */ 37 mfoltz 1.1 38 mfoltz 1.1 public class PartitionGraphViewer extends Observable implements ActionListener { 39 mfoltz 1.1 40 mfoltz 1.1 static public Color _nodecolors[] = { new Color(0, 255, 0), new Color(255, 0, 0), 41 mfoltz 1.1 new Color(255, 255, 0), new Color(255, 0, 255), 42 mfoltz 1.1 new Color(0, 255, 255) }; 43 mfoltz 1.4 static public Color _edgecolor = new Color(255, 255, 0); 44 mfoltz 1.1 static public Color _textcolor = new Color(255, 255, 255); 45 mfoltz 1.4 static public Color _bgcolor = new Color(0, 0, 128); 46 mfoltz 1.1 static public Font _textfont = new Font("HELVETICA", Font.PLAIN, 8); 47 mfoltz 1.1 static public final int MULTI_CIRCLE = 0; 48 mfoltz 1.1 static public final int ONE_CIRCLE = 1; 49 mfoltz 1.1 50 mfoltz 1.1 private Frame _frame; 51 mfoltz 1.1 private PartitionPanel _panel; 52 mfoltz 1.1 private Button _button; 53 mfoltz 1.1 private int _w, _h; 54 mfoltz 1.1 55 mfoltz 1.1 public PartitionGraphViewer(WeightedGraph[] partition, int w, int h, Observer callback, int vizstyle) { 56 mfoltz 1.1 addObserver(callback); 57 mfoltz 1.1 _button = new Button("do it"); 58 mfoltz 1.1 _button.addActionListener(this); 59 mfoltz 1.1 _frame = new Frame("Partition Graph Mojo Screen"); 60 mfoltz 1.1 61 mfoltz 1.1 _w = w; 62 mfoltz 1.1 _h = h - 20; 63 mfoltz 1.1 64 mfoltz 1.1 _panel = new PartitionPanel(partition, vizstyle); 65 mfoltz 1.1 _panel.setSize(_w, _h); 66 mfoltz 1.1 _panel.setBackground(_bgcolor); 67 mfoltz 1.1 68 mfoltz 1.1 _frame.add(_panel, "Center"); 69 mfoltz 1.1 _frame.add(_button, "South"); 70 mfoltz 1.1 _frame.setSize(_w, h); 71 mfoltz 1.1 _frame.show(); 72 mfoltz 1.1 _panel.repaint(); 73 mfoltz 1.1 } 74 mfoltz 1.1 75 mfoltz 1.1 public void actionPerformed(ActionEvent e) { 76 mfoltz 1.1 setChanged(); 77 mfoltz 1.1 notifyObservers(this); 78 mfoltz 1.1 _panel.repaint(); 79 mfoltz 1.1 } 80 mfoltz 1.1 81 mfoltz 1.1 class PartitionPanel extends Panel { 82 mfoltz 1.1 83 mfoltz 1.1 private WeightedGraph[] _partition; 84 mfoltz 1.1 85 mfoltz 1.1 private int _vizstyle; 86 mfoltz 1.1 87 mfoltz 1.1 public PartitionPanel(WeightedGraph[] partition, int vizstyle) { 88 mfoltz 1.1 _partition = partition; 89 mfoltz 1.1 _vizstyle = vizstyle; 90 mfoltz 1.1 } 91 mfoltz 1.1 92 mfoltz 1.3 public void paint(Graphics g) { 93 mfoltz 1.1 94 mfoltz 1.1 switch (_vizstyle) { 95 mfoltz 1.1 case PartitionGraphViewer.MULTI_CIRCLE: 96 mfoltz 1.1 vizMultiCircle(g); 97 mfoltz 1.1 break; 98 mfoltz 1.1 case PartitionGraphViewer.ONE_CIRCLE: 99 mfoltz 1.1 vizOneCircle(g); 100 mfoltz 1.1 break; 101 mfoltz 1.1 default: 102 mfoltz 1.1 vizMultiCircle(g); 103 mfoltz 1.1 break; 104 mfoltz 1.1 } 105 mfoltz 1.1 106 mfoltz 1.1 } 107 mfoltz 1.1 108 mfoltz 1.1 public void vizOneCircle(Graphics g) { 109 mfoltz 1.1 110 mfoltz 1.1 Dimension dim = getSize(); 111 mfoltz 1.1 int i, j, num_nodes = 0; 112 mfoltz 1.1 Enumeration e, adj, weights; 113 mfoltz 1.1 Point[] nodes; 114 mfoltz 1.1 Point from, to; 115 mfoltz 1.1 WGNode node, adjnode; 116 mfoltz 1.1 long weight; 117 mfoltz 1.1 double colorcoef; 118 mfoltz 1.1 119 mfoltz 1.3 // g.clearRect(0,0,dim.width,dim.height); 120 mfoltz 1.1 121 mfoltz 1.1 // first get total number of nodes 122 mfoltz 1.1 int k = _partition.length; 123 mfoltz 1.1 Hashtable nodemap = new Hashtable(2 * k * _partition[0].size()); 124 mfoltz 1.1 125 mfoltz 1.1 for (i = 0; i < k; i++) num_nodes += _partition[i].size(); 126 mfoltz 1.1 nodes = new Point[num_nodes]; 127 mfoltz 1.1 layoutCircle((int) (0.4*dim.width), num_nodes, new Point(dim.width/2, dim.height/2), nodes); 128 mfoltz 1.1 129 mfoltz 1.1 // now layout and draw each node and put it in a big hash table. yum. 130 mfoltz 1.1 g.setFont(PartitionGraphViewer._textfont); 131 mfoltz 1.1 // System.err.print("draw node: "); 132 mfoltz 1.1 j = 0; 133 mfoltz 1.1 for (i = 0; i < k; i++) { 134 mfoltz 1.1 e = _partition[i].getNodes(); 135 mfoltz 1.1 while (e.hasMoreElements()) { 136 mfoltz 1.1 node = (WGNode) e.nextElement(); 137 mfoltz 1.1 nodemap.put(node._name, nodes[j]); 138 mfoltz 1.1 g.setColor(PartitionGraphViewer._nodecolors[i % PartitionGraphViewer._nodecolors.length]); 139 mfoltz 1.1 g.fillOval(nodes[j].x-4,nodes[j].y-4,8,8); 140 mfoltz 1.1 // System.err.print("("+nodes[j].x+","+nodes[j].y+") "); 141 mfoltz 1.1 g.setColor(PartitionGraphViewer._textcolor); 142 mfoltz 1.1 g.drawString(node._name, nodes[j].x+6, nodes[j].y); 143 mfoltz 1.1 j++; 144 mfoltz 1.1 } 145 mfoltz 1.1 } 146 mfoltz 1.1 147 mfoltz 1.1 // System.err.print("\n"); 148 mfoltz 1.1 149 mfoltz 1.1 // now draw the edges 150 mfoltz 1.1 // System.err.print("draw edge: "); 151 mfoltz 1.1 for (i = 0; i < k; i++) { 152 mfoltz 1.1 e = _partition[i].getNodes(); 153 mfoltz 1.1 while (e.hasMoreElements()) { 154 mfoltz 1.1 node = (WGNode) e.nextElement(); 155 mfoltz 1.1 adj = node.getAdjacent(); 156 mfoltz 1.1 weights = node.getWeights(); 157 mfoltz 1.1 from = (Point) nodemap.get(node._name); 158 mfoltz 1.1 while (adj.hasMoreElements()) { 159 mfoltz 1.1 adjnode = (WGNode) adj.nextElement(); 160 mfoltz 1.1 weight = ((Long) weights.nextElement()).longValue(); 161 mfoltz 1.1 colorcoef = 1.0 - Math.exp(-weight / 20); 162 mfoltz 1.1 g.setColor(new Color((int) (colorcoef*PartitionGraphViewer._edgecolor.getRed()), 163 mfoltz 1.1 (int) (colorcoef*PartitionGraphViewer._edgecolor.getGreen()), 164 mfoltz 1.1 (int) (colorcoef*PartitionGraphViewer._edgecolor.getBlue()))); 165 mfoltz 1.1 to = (Point) nodemap.get(adjnode._name); 166 mfoltz 1.1 // System.err.print("("+from.x+","+from.y+")->("+to.x+","+to.y+") "); 167 mfoltz 1.1 g.drawLine(from.x, from.y, to.x, to.y); 168 mfoltz 1.1 } 169 mfoltz 1.1 } 170 mfoltz 1.1 } 171 mfoltz 1.1 // System.err.print("\n"); 172 mfoltz 1.1 173 mfoltz 1.1 } 174 mfoltz 1.1 175 mfoltz 1.1 public void vizMultiCircle(Graphics g) { 176 mfoltz 1.1 177 mfoltz 1.1 Dimension dim = getSize(); 178 mfoltz 1.1 int i, j; 179 mfoltz 1.1 Enumeration e, adj, weights; 180 mfoltz 1.1 Point[] partition_ctr, nodes; 181 mfoltz 1.1 Point from, to; 182 mfoltz 1.1 WGNode node, adjnode; 183 mfoltz 1.1 long weight; 184 mfoltz 1.1 double colorcoef; 185 mfoltz 1.1 186 mfoltz 1.1 g.clearRect(0,0,dim.width,dim.height); 187 mfoltz 1.1 188 mfoltz 1.1 // first get centers of partitions 189 mfoltz 1.1 int k = _partition.length; 190 mfoltz 1.1 Hashtable nodemap = new Hashtable(2 * k * _partition[0].size()); 191 mfoltz 1.1 partition_ctr = new Point[k]; 192 mfoltz 1.1 layoutCircle((int)(dim.height/3), k, new Point(dim.width/2,dim.height/2), partition_ctr); 193 mfoltz 1.1 194 mfoltz 1.1 // now layout and draw each node and put it in a big hash table. yum. 195 mfoltz 1.1 g.setFont(PartitionGraphViewer._textfont); 196 mfoltz 1.1 // System.err.print("draw node: "); 197 mfoltz 1.1 for (i = 0; i < k; i++) { 198 mfoltz 1.1 nodes = new Point[_partition[i].size()]; 199 mfoltz 1.1 layoutCircle((int) (dim.height/7 * Math.sin(Math.PI / nodes.length)), 200 mfoltz 1.1 nodes.length, partition_ctr[i], nodes); 201 mfoltz 1.1 e = _partition[i].getNodes(); 202 mfoltz 1.1 for (j = 0; j < nodes.length; j++) { 203 mfoltz 1.1 node = (WGNode) e.nextElement(); 204 mfoltz 1.1 nodemap.put(node._name, nodes[j]); 205 mfoltz 1.1 g.setColor(PartitionGraphViewer._nodecolors[i % PartitionGraphViewer._nodecolors.length]); 206 mfoltz 1.1 g.fillOval(nodes[j].x-4,nodes[j].y-4,8,8); 207 mfoltz 1.1 // System.err.print("("+nodes[j].x+","+nodes[j].y+") "); 208 mfoltz 1.1 g.setColor(PartitionGraphViewer._textcolor); 209 mfoltz 1.1 g.drawString(node._name, nodes[j].x+6, nodes[j].y); 210 mfoltz 1.1 } 211 mfoltz 1.1 } 212 mfoltz 1.1 // System.err.print("\n"); 213 mfoltz 1.1 214 mfoltz 1.1 // now draw the edges 215 mfoltz 1.1 // System.err.print("draw edge: "); 216 mfoltz 1.1 for (i = 0; i < k; i++) { 217 mfoltz 1.1 e = _partition[i].getNodes(); 218 mfoltz 1.1 while (e.hasMoreElements()) { 219 mfoltz 1.1 node = (WGNode) e.nextElement(); 220 mfoltz 1.1 adj = node.getAdjacent(); 221 mfoltz 1.1 weights = node.getWeights(); 222 mfoltz 1.1 from = (Point) nodemap.get(node._name); 223 mfoltz 1.1 while (adj.hasMoreElements()) { 224 mfoltz 1.1 adjnode = (WGNode) adj.nextElement(); 225 mfoltz 1.1 weight = ((Long) weights.nextElement()).longValue(); 226 mfoltz 1.1 colorcoef = 1.0 - Math.exp(-weight / 20); 227 mfoltz 1.1 g.setColor(new Color((int) (colorcoef*PartitionGraphViewer._edgecolor.getRed()), 228 mfoltz 1.1 (int) (colorcoef*PartitionGraphViewer._edgecolor.getGreen()), 229 mfoltz 1.1 (int) (colorcoef*PartitionGraphViewer._edgecolor.getBlue()))); 230 mfoltz 1.1 to = (Point) nodemap.get(adjnode._name); 231 mfoltz 1.1 // System.err.print("("+from.x+","+from.y+")->("+to.x+","+to.y+") "); 232 mfoltz 1.1 g.drawLine(from.x, from.y, to.x, to.y); 233 mfoltz 1.1 } 234 mfoltz 1.1 } 235 mfoltz 1.1 } 236 mfoltz 1.1 // System.err.print("\n"); 237 mfoltz 1.1 238 mfoltz 1.1 } 239 mfoltz 1.1 240 mfoltz 1.1 public void layoutCircle(int r, int n, Point center, Point[] coords) { 241 mfoltz 1.1 242 mfoltz 1.1 if (n == 1) { 243 mfoltz 1.1 coords[0] = new Point(center.x, center.y); 244 mfoltz 1.1 } else { 245 mfoltz 1.1 double dtheta = 2 * Math.PI / n; 246 mfoltz 1.1 for (int i = 0; i < n; i++) 247 mfoltz 1.1 coords[i] = new Point(((int) (r * Math.cos(i * dtheta))) + center.x, 248 mfoltz 1.1 ((int) (-r * Math.sin(i * dtheta))) + center.y); 249 mfoltz 1.1 } 250 mfoltz 1.1 251 mfoltz 1.1 } 252 mfoltz 1.1 253 mfoltz 1.1 } 254 mfoltz 1.1 255 mfoltz 1.1 } 256 mfoltz 1.1 257 mfoltz 1.1 258 mfoltz 1.1 259 mfoltz 1.1 260 mfoltz 1.1 261 mfoltz 1.1 262 mfoltz 1.1 263 mfoltz 1.1