⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mappanel.java

📁 Contiki是一个开源
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
          x = n.node.getX();          y = n.node.getY();        } else {          x = lx;          y = 10;          lx += 30;        }        if (!hideNetwork) {          FontMetrics fm = g.getFontMetrics();          int fnHeight = fm.getHeight();          int fnDescent = fm.getDescent();          for (int j = 0, mu = n.node.getLinkCount(); j < mu; j++) {            Link link = n.node.getLink(j);            if (link.node.hasLocation()) {              long age = (time - link.getLastActive()) / 100;              int x2 = link.node.getX();              int y2 = link.node.getY();              if (n.isSelected) {                if (age > LINK_COLOR.length) {                  age = 100;                } else {                  age -= 50;                }              }              line.setLine(x, y, x2, y2);              if (age < LINK_COLOR.length) {                g.setColor(age < 0 ? LINK_COLOR[0] : LINK_COLOR[(int) age]);              } else {                g.setColor(LINK_COLOR[LINK_COLOR.length - 1]);              }              g2d.draw(line);//              g.setColor(Color.lightGray);              int xn1, xn2, yn1, yn2;              if (x <= x2) {                xn1 = x; xn2 = x2;                yn1 = y; yn2 = y2;              } else {                xn1 = x2; xn2 = x;                yn1 = y2; yn2 = y;              }              int dx = xn1 + (xn2 - xn1) / 2 + 4;              int dy = yn1 + (yn2 - yn1) / 2 - fnDescent;              if (yn2 < yn1) {                dy += fnHeight - fnDescent;              }              g.drawString("ETX:" + (((int)(link.getETX() * 100 + 0.5)) / 100.0), dx, dy);            }          }        }        n.paint(g, x, y);        g.setColor(Color.black);        if (n.isSelected) {          BasicGraphicsUtils.drawDashedRect(g, x - delta, y - delta, 2 * delta, 2 * delta);        }        if (selectedNode != null && selectedNode.message != null) {          g.drawString(selectedNode.message, 10, 10);        }      }    }  }  // -------------------------------------------------------------------  // ActionListener  // -------------------------------------------------------------------  public void actionPerformed(ActionEvent e) {    Object source = e.getSource();    if (source == timer) {      ticker++;      if (hasPendingEvents) {        boolean repaint = false;        hasPendingEvents = false;        MapNode[] nodes = nodeList;        if (nodes != null) {          long time = System.currentTimeMillis();          for (int i = 0, n = nodes.length; i < n; i++) {            if (nodes[i].tick(time)) {              repaint = true;            }          }        }        if (repaint) {          hasPendingEvents = true;          repaint();        }      } else if ((ticker % 10) == 0) {        repaint();      }    } else if (source == hideItem) {      hideNetwork = !hideNetwork;      if (!hideNetwork) hideItem.setText("Hide Network");      else hideItem.setText("Show Network");      repaint();    } else if (source == resetNetworkItem) {      MapNode[] nodes = nodeList;      if (nodes != null) {        for (int i = 0, m = nodes.length; i < m; i++) {          MapNode n = nodes[i];          n.node.clearLinks();        }        repaint();      }    }  }  // -------------------------------------------------------------------  // Mouselistener  // -------------------------------------------------------------------  private MapNode getNodeAt(int mx, int my) {    int lx = 10;    MapNode[] nodes = nodeList;    if (nodes != null) {      for (int i = 0, m = nodes.length; i < m; i++) {        MapNode n = nodes[i];        int x, y;        if (n.node.hasLocation()) {          x = n.node.getX();          y = n.node.getY();        } else {          x = lx;          y = 10;          lx += 30;        }        if (mx >= (x - delta)            && mx <= (x + delta)            && my >= (y - delta)            && my <= (y + delta)) {          return n;        }      }    }    return null;  }  public void mouseClicked(MouseEvent e) {    int mx = e.getX();    int my = e.getY();    if (e.getButton() == MouseEvent.BUTTON1) {      MapNode node = getNodeAt(mx, my);      if (node != selectedNode) {        server.selectNodes(node == null ? null : new Node[] { node.node });      }    }    showPopup(e);  }  public void mousePressed(MouseEvent e) {    if (e.getButton() == MouseEvent.BUTTON1) {      MapNode aNode = getNodeAt(e.getX(), e.getY());      if (aNode != selectedNode) {        server.selectNodes(aNode != null ? new Node[] { aNode.node } : null);      }      draggedNode = aNode;      draggedTime = System.currentTimeMillis();    } else if (selectedNode != null) {      selectedNode = draggedNode = null;      server.selectNodes(null);    }    showPopup(e);  }  public void mouseReleased(MouseEvent e) {    if (draggedNode != null && e.getButton() == MouseEvent.BUTTON1) {      if ((!VISUAL_DRAG || (draggedTime > 0)) &&          (System.currentTimeMillis() - draggedTime) < 300) {        // Do not drag if mouse is only moved during click      } else {        draggedNode.node.setLocation(e.getX(), e.getY());        server.updateNodeLocation(draggedNode.node);        draggedNode = null;        repaint();      }    }    showPopup(e);  }  private void showPopup(MouseEvent e) {    if (e.isPopupTrigger()        && (e.getModifiers() & (MouseEvent.SHIFT_MASK|MouseEvent.CTRL_MASK)) == 0) {//      popupNode = getNodeAt(e.getX(), e.getY());//      nodeItem.setEnabled(popupNode != null);      popupMenu.show(this, e.getX(), e.getY());    }  }  public void mouseEntered(MouseEvent e) {  }  public void mouseExited(MouseEvent e) {  }  // -------------------------------------------------------------------  // MouseMotion  // -------------------------------------------------------------------  public void mouseDragged(MouseEvent e) {    if (!VISUAL_DRAG || draggedNode == null) {      // Do nothing    } else if (draggedTime > 0) {      if ((System.currentTimeMillis() - draggedTime) > 300) {        // No mouse click, time to drag the node        draggedTime = -1;      }    } else {      draggedNode.node.setLocation(e.getX(), e.getY());      repaint();    }  }  public void mouseMoved(MouseEvent e) {  }  // -------------------------------------------------------------------  // MapNode  // -------------------------------------------------------------------  private static class MapNode {    public final Node node;    public boolean isSelected;    public String message = null;    private int tick = 0;    MapNode(MapPanel panel, Node node) {      this.node = node;    }    boolean tick(long time) {      boolean r = false;      if (tick > 0) {        tick--;        r = true;      }      for (int i = 0, n = node.getLinkCount(); i < n; i++) {        Link link = node.getLink(i);        long age = (time - link.getLastActive()) / 100;        if (age < 200) {          r = true;          break;        }      }      return r;    }    public void paint(Graphics g, int x, int y) {      if (tick > (TOTAL_SHOW - SHOW_BLINK)) {        if ((tick & 4) == 0) {          // Hide circle        } else {          int index = FADE_COUNT - tick - 1;          if (index < 0) {            index = 0;          }          final int d = 8;          g.setColor(OTHER_COLOR[index]);          g.fillOval(x - d, y - d, d * 2 + 1, d * 2 + 1);        }      }      if (tick < (TOTAL_SHOW - SHOW_BLINK) && tick > 0) {        g.setColor(Color.red);        int height = 13 * tick / TOTAL_SHOW;        g.fillRect(x - 6, 5 + y - height, 2, height);      }      g.setColor(Color.black);      final int od = 3;      g.drawString(node.getID(), x + od * 2 + 3, y + 4);      g.fillOval(x - od, y - od, od * 2 + 1, od * 2 + 1);    }  } // end of inner class MapNode}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -