00001
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 import java.io.*;
00036 import java.awt.*;
00037 import java.awt.event.*;
00038 import java.awt.image.*;
00039 import java.awt.print.*;
00040 import java.util.*;
00041 import javax.swing.*;
00042 import javax.swing.event.*;
00043 import javax.swing.border.*;
00044 import java.awt.geom.*;
00045
00046
00047 public class TopologyPanel extends JPanel
00048 implements KeyListener, ActionListener, FocusListener
00049 {
00050 static final int XZ = 1010;
00051 static final int YZ = 110;
00052 static final int TY = 101;
00053
00054 protected boolean running;
00055 protected int divisor;
00056 protected int orientation;
00057 protected Image offscreen;
00058 protected Dimension offscreensize;
00059 protected Graphics2D offgraphics;
00060
00061 protected double zoom_factor;
00062 protected int pan_multiplier;
00063 protected int pan_factor_x;
00064 protected int pan_factor_y;
00065
00066 public Image last_image;
00067
00068 protected SenseGui gui;
00069 protected int nodeSize;
00070 protected Vector<Arc> arcs;
00071 protected Vector<TxSphere> spheres;
00072 protected Vector<Detection> detect_nodes;
00073
00074 protected int width, height;
00075 protected Node new_node;
00076 protected JPopupMenu fail_menu;
00077 protected JPopupMenu envir_menu;
00078
00079 private LegendPanel leg_panel;
00080
00081 public boolean status;
00082 public Network configd_net;
00083 public JPanel refresher;
00084 public boolean image_changed;
00085
00086 static final int dialog_width = 15;
00087 static final int dialog_height = 15;
00088
00089 static final int sphere_life = 1;
00090
00091 public static final Color focus_color = new Color(250, 250, 210);
00092 public static final Color bkgrnd_color = new Color(240, 240, 200);
00093 public static final Color arc_color = new Color(35, 195, 100);;
00094 public static final Color sphere_color = new Color(240, 60, 60);
00095
00096 public Color[] group_colors;
00097 public Color[] node_color;
00098 public Color[] node_text_color;
00099
00100
00101 public TopologyPanel(SenseGui g, int w, int h) {
00102 super();
00103
00104 running = true;
00105 zoom_factor = 1.0;
00106 pan_multiplier = 100;
00107 pan_factor_x = 0;
00108 pan_factor_y = 0;
00109 orientation = 0;
00110 divisor = 1;
00111
00112 nodeSize = 1;
00113 gui = g;
00114 arcs = new Vector<Arc>();
00115 spheres = new Vector<TxSphere>();
00116 detect_nodes = new Vector<Detection>();
00117 width = w;
00118 height = h;
00119 status = false;
00120 new_node = null;
00121 image_changed = true;
00122 setPreferredSize(new Dimension(width, height));
00123 setLayout(new BorderLayout());
00124
00125 leg_panel = new LegendPanel(gui, width/4, height/8);
00126 JPanel bottom_panel = new JPanel(new BorderLayout());
00127 bottom_panel.setOpaque(false);
00128 bottom_panel.add(leg_panel, BorderLayout.EAST);
00129 add(bottom_panel, BorderLayout.SOUTH);
00130
00131 setBackground(bkgrnd_color);
00132
00133 fail_menu = new JPopupMenu();
00134
00135 JMenuItem fc_item = new JMenuItem("Configure Failure");
00136 fc_item.addActionListener(this);
00137 JMenuItem fcancel_item = new JMenuItem("Cancel");
00138 fcancel_item.setMnemonic(KeyEvent.VK_C);
00139 fcancel_item.addActionListener(this);
00140
00141 fail_menu.add(fc_item);
00142 fail_menu.addSeparator();
00143 fail_menu.addSeparator();
00144 fail_menu.add(fcancel_item);
00145
00146
00147 envir_menu = new JPopupMenu();
00148
00149 JMenuItem ec_item = new JMenuItem("Configure an Event Source");
00150 ec_item.addActionListener(this);
00151 JMenuItem ecancel_item = new JMenuItem("Cancel");
00152 ecancel_item.setMnemonic(KeyEvent.VK_C);
00153 ecancel_item.addActionListener(this);
00154
00155 envir_menu.add(ec_item);
00156 envir_menu.addSeparator();
00157 envir_menu.add(ecancel_item);
00158
00159 setSize(w, h);
00160 setVisible(true);
00161 addFocusListener(this);
00162 addKeyListener(this);
00163 }
00164
00165
00166 public void start() {
00167 configd_net = gui.topology;
00168 group_colors = new Color[configd_net.getGroups().size()];
00169 node_color = new Color[configd_net.getNodes().size()];
00170 node_text_color = new Color[configd_net.getNodes().size()];
00171 Vector<NodeGroup> gv = configd_net.getGroups();
00172 for (int i=0; i<gv.size(); i++) {
00173 Vector<Node> gvnv = ((NodeGroup)gv.elementAt(i)).getNodes();
00174 Random rand = new Random(SenseGui.global_rand.nextLong());
00175 int selector = rand.nextInt(139);
00176 Color c = ((NodeGroup)gv.elementAt(i)).getColor();
00177 if (((NodeGroup)gv.elementAt(i)).isRandColor())
00178 c = XColors.selection[selector];
00179 group_colors[i] = c;
00180
00181 Color t = Color.black;
00182 float[] hsb = Color.RGBtoHSB(c.getRed(), c.getGreen(),
00183 c.getBlue(), null);
00184 if (hsb[2] > 0.67) {
00185 if ((hsb[0] > 0.56 || hsb[0] < 0.15) && hsb[1] > 0.39)
00186
00187 t = Color.white;
00188 else
00189 t = Color.black;
00190 } else
00191 t = Color.white;
00192
00193 for (int j=0; j<gvnv.size(); j++) {
00194 int node_id = ((Node)gvnv.elementAt(j)).getSeq_num();
00195 node_color[ node_id ] = c;
00196 node_text_color[ node_id ] = t;
00197 }
00198 }
00199 }
00200
00201 public void stop() {
00202 try {
00203
00204
00205 detect_nodes.removeAllElements();
00206 spheres.removeAllElements();
00207 arcs.removeAllElements();
00208 repaint();
00209 status = false;
00210 } catch(Exception e) {
00211 e.printStackTrace();
00212 }
00213 }
00214
00215 public Dimension getMinimumSize() {
00216 return getPreferredSize();
00217 }
00218
00219 public Dimension getMaximumSize() {
00220 return getPreferredSize();
00221 }
00222
00223 public Dimension getPreferredSize() {
00224 return new Dimension(this.width, this.height);
00225 }
00226
00227 public double scale(double coord) {
00228 double scaling_factor = (double)gui.width /
00229 (double)configd_net.getField_width();
00230 return coord * scaling_factor;
00231 }
00232
00233 public double descale(double coord) {
00234 double scaling_factor = (double)configd_net.getField_width()
00235 / (double)gui.width;
00236 return coord * scaling_factor;
00237 }
00238
00239
00240
00241 public void failNode(int id, Color c) {
00242
00243 node_color[ id ] = c;
00244 image_changed = true;
00245 }
00246
00247 public void setupPaint() {
00248 status = true;
00249 image_changed = true;
00250 repaint();
00251 }
00252
00253 public void focusGained(FocusEvent evt) {
00254 setBackground(focus_color);
00255 image_changed = true;
00256 repaint();
00257 }
00258
00259 public void focusLost(FocusEvent evt) {
00260 setBackground(bkgrnd_color);
00261 image_changed = true;
00262 repaint();
00263 }
00264
00265 public void keyTyped(KeyEvent e) {}
00266
00267 public void keyPressed(KeyEvent e) {
00268 if (e.getKeyCode() == KeyEvent.VK_LEFT)
00269 pan(-1, 0);
00270 else if (e.getKeyCode() == KeyEvent.VK_RIGHT)
00271 pan(1, 0);
00272 else if (e.getKeyCode() == KeyEvent.VK_UP)
00273 pan(0, -1);
00274 else if (e.getKeyCode() == KeyEvent.VK_DOWN)
00275 pan(0, 1);
00276 else if (e.getKeyCode() == KeyEvent.VK_PLUS)
00277 zoom(gui.menu_options.zup());
00278 else if (e.getKeyCode() == KeyEvent.VK_MINUS)
00279 zoom(gui.menu_options.zdn());
00280 e.consume();
00281 }
00282
00283 public void keyReleased(KeyEvent e) {}
00284
00285
00286 public synchronized void paintComponent(Graphics g) {
00287 super.paintComponent(g);
00288 drawTopology(g);
00289 }
00290
00291
00292 protected void createSpontEvt(Component c, int x, int y) {
00293 new_node = new Node();
00294 new_node.setX((double)descale(x));
00295 new_node.setY((double)descale(y));
00296 new_node.setId(configd_net.getNodes().size());
00297 envir_menu.show(c, x, y);
00298 }
00299
00300 public Node onNode(int x, int y) {
00301 Vector<Node> nv = configd_net.getNodes();
00302 for (int i=0; i<nv.size(); i++) {
00303 Node n = (Node)nv.elementAt(i);
00304
00305 if ((int)Math.abs((double)x - n.getX()) <= nodeSize/2 &&
00306 (int)Math.abs((double)y - n.getY()) <= nodeSize/2)
00307 return n;
00308 }
00309 return (Node)null;
00310 }
00311
00312 public void actionPerformed(ActionEvent ae) {
00313 String s = ae.getActionCommand();
00314 java.lang.Object arg = ae.getSource();
00315
00316 if (s.equalsIgnoreCase("Cancel")) {
00317 } else if (s.equalsIgnoreCase("Add Node")) {
00318
00319
00320
00321
00322
00323 } else if (s.equalsIgnoreCase("Configure Node")) {
00324
00325
00326
00327
00328
00329 } else if (s.equalsIgnoreCase("Configure Failure")) {
00330 } else if (s.equalsIgnoreCase("Configure an Event Source")) {
00331 }
00332 }
00333
00334
00335 public void displayDetect(int node, String type) {
00336 for (int i=0; i<detect_nodes.size(); i++) {
00337 Detection d = (Detection)detect_nodes.elementAt(i);
00338 d.ttl--;
00339 if (d.ttl <= 0) {
00340 detect_nodes.removeElementAt(i);
00341 i--;
00342 }
00343 }
00344 boolean has_one = false;
00345 Color d_color = Color.white;
00346 Node target = (Node)gui.topology.findNode(node);
00347 if (target != null) {
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359 }
00360 if (has_one) {
00361 detect_nodes.addElement(new Detection(node, target.getX(),
00362 target.getY(),
00363 target.getZ(), d_color));
00364 image_changed = true;
00365 }
00366 }
00367
00368
00369 public void displayEvent(String s) {
00370 if (s.length() == 0)
00371 return;
00372 int event = s.indexOf(" event");
00373 if (event >= 0) {
00374 String type = s.substring(0, event);
00375 if (type.equalsIgnoreCase("failure")) {
00376 int node = s.indexOf("node ");
00377 int at = s.indexOf(" at");
00378 int node_id = Integer.parseInt(s.substring(node+5, at));
00379 node_color[node_id] = Color.black;
00380 node_text_color[node_id] = Color.white;
00381 }
00382 } else {
00383
00384
00385
00386
00387
00388
00389
00390 int end = s.indexOf("\n");
00391 if (end > 0)
00392 s = s.substring(0, end);
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406 }
00407 image_changed = true;
00408 repaint();
00409 }
00410
00411 public void displayComm(String s) {
00412 int sending = s.indexOf("Send");
00413 int receiving = s.indexOf("Recv");
00414 int start = s.indexOf("NODE");
00415 int end = s.indexOf(":", start);
00416 int node_num = -1;
00417 if (start >= 0 && end > 0)
00418 node_num = Integer.parseInt(s.substring(start+4, end));
00419 if (sending >= 0 && node_num >= 0) {
00420 spheres.addElement(new TxSphere(configd_net, node_num,
00421 sphere_life));
00422 } else if (receiving >= 0 && node_num >= 0) {
00423 start = s.indexOf("from ");
00424 end = s.indexOf(":", start);
00425 int from_node = -1;
00426 if (start >= 0 && end > 0)
00427 from_node = Integer.parseInt(s.substring(start+5, end));
00428 if (from_node >= 0)
00429 arcs.addElement(new Arc(configd_net, node_num, from_node));
00430 }
00431 image_changed = true;
00432
00433 }
00434
00435 public void displayComm(byte[] b, int i) {
00436 displayComm(new String(b, 0, i));
00437 }
00438
00439 public void clear() {
00440 detect_nodes.removeAllElements();
00441 spheres.removeAllElements();
00442 arcs.removeAllElements();
00443 repaint();
00444 }
00445
00446 public void clearSpheres() {
00447 for (int i=0; i<spheres.size(); i++) {
00448 TxSphere ts = (TxSphere)spheres.elementAt(i);
00449 ts.time_to_live--;
00450 if (ts.time_to_live <= 0) {
00451 spheres.removeElementAt(i);
00452 image_changed = true;
00453 }
00454 }
00455 for (int i=0; i<detect_nodes.size(); i++) {
00456 Detection d = (Detection)detect_nodes.elementAt(i);
00457 d.ttl--;
00458 if (d.ttl <= 0) {
00459 detect_nodes.removeElementAt(i);
00460 i--;
00461 image_changed = true;
00462 }
00463 }
00464 repaint();
00465 }
00466
00467 public int setOrientation(long coordinate) {
00468 return 0;
00469 }
00470
00471 public int setOrientation(double coordinate, boolean flip) {
00472 int additive = gui.width / 60;
00473 if (orientation == 0)
00474 return (int)coordinate;
00475 else if (orientation == 1)
00476 return (int)(coordinate * 7.0/8.0);
00477 else if (orientation == TY)
00478 return (int)((coordinate / (double)height) * 7.0/8.0);
00479 else if (!flip)
00480 return (int)((coordinate / (double)divisor) * 7.0/8.0) + additive;
00481 else
00482 return (int)(((configd_net.getField_width() - coordinate) /
00483 (double)divisor) * 7.0/8.0) + additive;
00484 }
00485
00486
00487 public synchronized void paintTxSphere(Graphics2D g, TxSphere txs) {
00488 if (!txs.is_oriented) {
00489 if (orientation == XZ) {
00490 txs.center_x = (int)(scale(setOrientation(txs.x, false)) *
00491 zoom_factor) + pan_factor_x;
00492 txs.center_y = (int)(scale(setOrientation(txs.z, true)) *
00493 zoom_factor) + pan_factor_y;
00494 } else if (orientation == YZ) {
00495 txs.center_x = (int)(scale(setOrientation(txs.y, false)) *
00496 zoom_factor) + pan_factor_x;
00497 txs.center_y = (int)(scale(setOrientation(txs.z, true)) *
00498 zoom_factor) + pan_factor_y;
00499 } else {
00500 txs.center_x = (int)(scale(setOrientation(txs.x, false)) *
00501 zoom_factor) + pan_factor_x;
00502 txs.center_y = (int)(scale(setOrientation(txs.y, false)) *
00503 zoom_factor) + pan_factor_y;
00504 }
00505 txs.center_r = (int)(scale((int)txs.radius / divisor) *
00506 zoom_factor);
00507 if (txs.color == null)
00508 txs.color = sphere_color;
00509 txs.is_oriented = true;
00510 }
00511 g.setColor(txs.color);
00512 g.drawOval(txs.center_x - txs.center_r,
00513 txs.center_y - txs.center_r,
00514 txs.center_r * 2, txs.center_r * 2);
00515 }
00516
00517
00518 public synchronized void paintArc(Graphics2D g, Arc arc) {
00519 if (!arc.is_oriented) {
00520 if (orientation == XZ) {
00521 arc.line_x1 = (int)(scale(setOrientation(arc.f_x, false)) *
00522 zoom_factor) + pan_factor_x;
00523 arc.line_y1 = (int)(scale(setOrientation(arc.f_z, true)) *
00524 zoom_factor) + pan_factor_y;
00525 arc.line_x2 = (int)(scale(setOrientation(arc.t_x, false)) *
00526 zoom_factor) + pan_factor_x;
00527 arc.line_y2 = (int)(scale(setOrientation(arc.t_z, true)) *
00528 zoom_factor) + pan_factor_y;
00529 } else if (orientation == YZ) {
00530 arc.line_x1 = (int)(scale(setOrientation(arc.f_y, false)) *
00531 zoom_factor) + pan_factor_x;
00532 arc.line_y1 = (int)(scale(setOrientation(arc.f_z, true)) *
00533 zoom_factor) + pan_factor_y;
00534 arc.line_x2 = (int)(scale(setOrientation(arc.t_y, false)) *
00535 zoom_factor) + pan_factor_x;
00536 arc.line_y2 = (int)(scale(setOrientation(arc.t_z, true)) *
00537 zoom_factor) + pan_factor_y;
00538 } else {
00539 arc.line_x1 = (int)(scale(setOrientation(arc.f_x, false)) *
00540 zoom_factor) + pan_factor_x;
00541 arc.line_y1 = (int)(scale(setOrientation(arc.f_y, false)) *
00542 zoom_factor) + pan_factor_y;
00543 arc.line_x2 = (int)(scale(setOrientation(arc.t_x, false)) *
00544 zoom_factor) + pan_factor_x;
00545 arc.line_y2 = (int)(scale(setOrientation(arc.t_y, false)) *
00546 zoom_factor) + pan_factor_y;
00547
00548
00549 if (configd_net != null &&
00550 configd_net.getNodes().size() < 10) {
00551 int arrowLen = gui.width / 40;
00552 double theta = Math.toRadians(20);
00553 double slope = (arc.t_y - arc.f_y) / (arc.t_x - arc.f_x);
00554 double y_intcpt = arc.t_y - (slope * arc.t_x);
00555 double backtrack = 2.0 * (double)g.getFontMetrics().getAscent() / 3.0;
00556
00557 if (slope >= 0.0 && slope < 1.0) {
00558 if (arc.f_x < arc.t_x)
00559 arc.t_x -= backtrack;
00560 else
00561 arc.t_x += backtrack;
00562 arc.t_y = (slope * arc.t_x) + y_intcpt;
00563 } else if (slope >= 1.0) {
00564 if (arc.f_y < arc.t_y)
00565 arc.t_y -= backtrack;
00566 else
00567 arc.t_y += backtrack;
00568 arc.t_x = (arc.t_y - y_intcpt) / slope;
00569 } else if (slope > -1.0) {
00570 if (arc.f_x < arc.t_x)
00571 arc.t_x -= backtrack;
00572 else
00573 arc.t_x += backtrack;
00574 arc.t_y = (slope * arc.t_x) + y_intcpt;
00575 } else {
00576 if (arc.f_y < arc.t_y)
00577 arc.t_y -= backtrack;
00578 else
00579 arc.t_y += backtrack;
00580 arc.t_x = (arc.t_y - y_intcpt) / slope;
00581 }
00582
00583 double angle = Math.atan2(arc.t_y - arc.f_y,
00584 arc.t_x - arc.f_x) + Math.PI;
00585 arc.arrowhead_x_pts[0] = (int)
00586 (scale((int)arc.t_x) * zoom_factor) + pan_factor_x;
00587 arc.arrowhead_y_pts[0] = (int)
00588 (scale((int)arc.t_y) * zoom_factor) + pan_factor_y;
00589 arc.arrowhead_x_pts[1] = (int)
00590 (scale((int)(arc.t_x + Math.cos(angle - theta) *
00591 arrowLen)) * zoom_factor) + pan_factor_x;
00592 arc.arrowhead_y_pts[1] = (int)
00593 (scale((int)(arc.t_y + Math.sin(angle - theta) *
00594 arrowLen)) * zoom_factor) + pan_factor_y;
00595 arc.arrowhead_x_pts[2] = (int)
00596 (scale((int)(arc.t_x + Math.cos(angle + theta) *
00597 arrowLen)) * zoom_factor) + pan_factor_x;
00598 arc.arrowhead_y_pts[2] = (int)
00599 (scale((int)(arc.t_y + Math.sin(angle + theta) *
00600 arrowLen)) * zoom_factor) + pan_factor_y;
00601 }
00602 }
00603 arc.is_oriented = true;
00604 }
00605 g.setColor(arc.color);
00606 g.drawLine(arc.line_x1, arc.line_y1, arc.line_x2, arc.line_y2);
00607
00608 if (orientation == 0 && configd_net != null &&
00609 configd_net.getNodes().size() < 10) {
00610 g.setStroke(new BasicStroke(2.1f));
00611 g.fillPolygon(arc.arrowhead_x_pts, arc.arrowhead_y_pts, 3);
00612 }
00613 }
00614
00615
00616 public synchronized void paintNode(Graphics2D g, Node n, FontMetrics fm) {
00617 if (node_color == null || node_color.length <= n.getId())
00618 paintNode(g, n, fm, Color.white, Color.black);
00619 else
00620 paintNode(g, n, fm, node_color[n.getSeq_num()],
00621 node_text_color[n.getSeq_num()]);
00622 }
00623
00624 public synchronized void paintNode(Graphics2D g, Node n,
00625 FontMetrics fm, Color c, Color t) {
00626 int x = (int)(scale(setOrientation(n.getX(), false)) *
00627 zoom_factor) + pan_factor_x;
00628 int y = (int)(scale(setOrientation(n.getY(), false)) *
00629 zoom_factor) + pan_factor_y;
00630 int z = (int)(scale(setOrientation(n.getZ(), true)) *
00631 zoom_factor);
00632 g.setColor(c);
00633 double zf = zoom_factor;
00634 if (zf > 1.0)
00635 zf = 1.0;
00636 int w = fm.stringWidth(Integer.toString(n.getId())) + 10;
00637 if (w > nodeSize)
00638 nodeSize = w;
00639 int h = fm.getHeight() + 4;
00640 h = (int)(scale(h) * zf);
00641 w = (int)(scale(w) * zf);
00642 if (h < 2) h = 2;
00643 if (w < 2) w = 2;
00644 if (orientation == XZ)
00645 g.fillRect(x - w/2, z - h/2, w, h);
00646 else if (orientation == YZ)
00647 g.fillRect(y - w/2, z - h/2, w, h);
00648 else
00649 g.fillRect(x - w/2, y - h/2, w, h);
00650 g.setColor(t);
00651 if (orientation == XZ)
00652 g.drawString(Integer.toString(n.getId()),
00653 (x - (w-(int)(scale(10) * zf))/2) - 1,
00654 (z - (h-(int)(scale(4) * zf))/2) +
00655 (int)(scale(fm.getAscent()) * zf) + 1);
00656 else if (orientation == YZ)
00657 g.drawString(Integer.toString(n.getId()),
00658 (y - (w-(int)(scale(10) * zf))/2) - 1,
00659 (z - (h-(int)(scale(4) * zf))/2) +
00660 (int)(scale(fm.getAscent()) * zf) + 1);
00661 else
00662 g.drawString(Integer.toString(n.getId()),
00663 (x - (w-(int)(scale(10) * zf))/2) - 1,
00664 (y - (h-(int)(scale(4) * zf))/2) +
00665 (int)(scale(fm.getAscent()) * zf) + 1);
00666
00667 }
00668
00669
00670 public synchronized void paintDetect(Graphics2D g, int id,
00671 double x_in, double y_in, double z_in,
00672 FontMetrics fm, Color c) {
00673 int x = (int)(scale(setOrientation(x_in, false)) * zoom_factor) +
00674 pan_factor_x;
00675 int y = (int)(scale(setOrientation(y_in, false)) * zoom_factor) +
00676 pan_factor_y;
00677 int z = (int)(scale(setOrientation(z_in, true)) * zoom_factor);
00678 g.setColor(c);
00679 int w = fm.stringWidth(Integer.toString(id)) + 15;
00680 int border_w = fm.stringWidth(Integer.toString(id)) + 12;
00681 int h = fm.getHeight() + 9;
00682 int border_h = fm.getHeight() + 6;
00683 h = (int)(scale(h) * zoom_factor);
00684 w = (int)(scale(w) * zoom_factor);
00685 border_h = (int)(scale(border_h) * zoom_factor);
00686 border_w = (int)(scale(border_w) * zoom_factor);
00687 if (h < 4) h = 4;
00688 if (w < 4) w = 4;
00689 if (border_h < 3) border_h = 3;
00690 if (border_w < 3) border_w = 3;
00691 if (orientation != XZ && orientation != YZ) {
00692 g.fillRect(x - w/2, y - h/2, w, h);
00693 g.setColor(Color.white);
00694 g.fillRect(x - border_w/2, y - border_h/2, border_w, border_h);
00695 }
00696 }
00697
00698
00699 public synchronized void compileImage() {
00700 Dimension d = getSize();
00701 Insets insets = getInsets();
00702 d.width = getWidth() - insets.left - insets.right;
00703 d.height = getHeight() - insets.top - insets.bottom;
00704
00705
00706 if ((offscreen == null) || (d.width != offscreensize.width) ||
00707 (d.height != offscreensize.height)) {
00708 offscreen = createImage(d.width, d.height);
00709 offscreensize = d;
00710 if (offgraphics != null) {
00711 offgraphics.dispose();
00712 }
00713 offgraphics = (Graphics2D)offscreen.getGraphics();
00714 offgraphics.setFont(getFont());
00715 }
00716
00717
00718 offgraphics.setColor(getBackground());
00719 offgraphics.fillRect(0, 0, d.width, d.height);
00720 FontMetrics fm = offgraphics.getFontMetrics();
00721
00722
00723 offgraphics.setStroke(new BasicStroke(1.3f));
00724 for (int i=0; i<spheres.size(); i++) {
00725 paintTxSphere(offgraphics, (TxSphere)spheres.elementAt(i));
00726 }
00727
00728
00729 offgraphics.setStroke(new BasicStroke(1.3f));
00730 for (int i=0; i<arcs.size(); i++) {
00731 paintArc(offgraphics, (Arc)arcs.elementAt(i));
00732 }
00733
00734
00735 offgraphics.setStroke(new BasicStroke(1.3f));
00736 if (detect_nodes != null) {
00737 for (int i=0; i<detect_nodes.size(); i++) {
00738 Detection detect = (Detection)detect_nodes.elementAt(i);
00739 paintDetect(offgraphics, detect.id, detect.x, detect.y,
00740 detect.z, fm, detect.c);
00741 }
00742 }
00743
00744
00745
00746 offgraphics.setStroke(new BasicStroke(1.3f));
00747 if (configd_net != null) {
00748 Vector<Node> nv = configd_net.getNodes();
00749 for (int i=0; i<nv.size(); i++) {
00750 paintNode(offgraphics, (Node)nv.elementAt(i), fm);
00751 }
00752 }
00753
00754 last_image = offscreen;
00755 }
00756
00757 public synchronized void drawTopology(Graphics g) {
00758
00759 Graphics2D g2 = (Graphics2D)g;
00760 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
00761 RenderingHints.VALUE_ANTIALIAS_ON);
00762
00763 if (image_changed)
00764 compileImage();
00765 g2.drawImage(last_image, 0, 0, null);
00766 image_changed = false;
00767 }
00768
00769 public void zoom(int z) {
00770 double old_zoom = zoom_factor;
00771 if (z == 0) {
00772 zoom_factor = 1.0;
00773 pan_multiplier = 100;
00774 pan_factor_x = pan_factor_y = 0;
00775 } else {
00776 if (z > 0) {
00777 zoom_factor = (double)(z + 1);
00778 pan_multiplier = 100;
00779 } else {
00780 zoom_factor = 1.0 / (double)((0 - z) + 1);
00781 pan_multiplier = (int)(pan_multiplier * zoom_factor);
00782 }
00783 pan_factor_x = pan_factor_x +
00784 (int)((gui.width / 2) * (old_zoom - zoom_factor));
00785 pan_factor_y = pan_factor_y +
00786 (int)((gui.width / 2) * (old_zoom - zoom_factor));
00787 }
00788
00789 image_changed = true;
00790 arcs.removeAllElements();
00791 repaint();
00792 }
00793
00794 public void pan(int x, int y) {
00795 pan_factor_x = pan_factor_x + (x * pan_multiplier);
00796 pan_factor_y = pan_factor_y + (y * pan_multiplier);
00797 image_changed = true;
00798 arcs.removeAllElements();
00799 repaint();
00800 }
00801 }