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

📄 visualizer2d.java

📁 Contiki is an open source, highly portable, multi-tasking operating system for memory-constrained n
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            }          });          pickMoteMenu.add(menuItem);        }      }    }    // Show menu    Point pos = new Point(canvas.getLocationOnScreen().x + x, canvas        .getLocationOnScreen().y        + y);    pickMoteMenu.setLocation(pos.x, pos.y);    pickMoteMenu.setInvoker(canvas);    pickMoteMenu.setVisible(true);  }  private void beginMoveRequest(final int x, final int y) {    final Vector<Mote> foundMotes = findMotesAtPosition(x, y);    if (foundMotes == null || foundMotes.size() == 0)      return;    moteMoveBeginTime = System.currentTimeMillis();    beginMoveRequest(foundMotes.get(0));  }  private void beginMoveRequest(Mote moteToMove) {    moteIsBeingMoved = true;    this.moteToMove = moteToMove;    canvas.repaint();  }  private void handleMoveRequest(final int x, final int y,      boolean wasJustReleased) {        if (!moteIsBeingMoved) {      return;    }    if (!wasJustReleased) {      // Still moving mote      canvas.setCursor(moveCursor);      return;    }    // Stopped moving mote    canvas.setCursor(Cursor.getDefaultCursor());    moteIsBeingMoved = false;        Position newXYValues = transformPixelToPositon(new Point(x, y));    if (moteMoveBeginTime <= 0 || System.currentTimeMillis() - moteMoveBeginTime > 300) {      int returnValue = JOptionPane.showConfirmDialog(myPlugin, "Move mote to"          + "\nX=" + newXYValues.getXCoordinate() + "\nY="          + newXYValues.getYCoordinate() + "\nZ="          + moteToMove.getInterfaces().getPosition().getZCoordinate());            if (returnValue == JOptionPane.OK_OPTION) {        moteToMove.getInterfaces().getPosition().setCoordinates(            newXYValues.getXCoordinate(), newXYValues.getYCoordinate(),            moteToMove.getInterfaces().getPosition().getZCoordinate());      }    }    moteMoveBeginTime = -1;    moteToMove = null;    repaint();  }  /**   * Returns all motes at given position.   *    * @param clickedX   *          X coordinate   * @param clickedY   *          Y coordinate   * @return All motes at given position   */  protected Vector<Mote> findMotesAtPosition(int clickedX, int clickedY) {    double xCoord = factorXPixelToCoord(clickedX);    double yCoord = factorYPixelToCoord(clickedY);    Vector<Mote> motesFound = new Vector<Mote>();    // Calculate painted mote radius in coordinates    double paintedMoteWidth = factorXPixelToCoord(MOTE_RADIUS)        - factorXPixelToCoord(0);    double paintedMoteHeight = factorYPixelToCoord(MOTE_RADIUS)        - factorYPixelToCoord(0);    for (int i = 0; i < simulation.getMotesCount(); i++) {      Position pos = simulation.getMote(i).getInterfaces().getPosition();      // Transform to unit circle before checking if mouse hit this mote      double distanceX = Math.abs(xCoord - pos.getXCoordinate())          / paintedMoteWidth;      double distanceY = Math.abs(yCoord - pos.getYCoordinate())          / paintedMoteHeight;      if (distanceX * distanceX + distanceY * distanceY <= 1) {        motesFound.add(simulation.getMote(i));      }    }    if (motesFound.size() == 0)      return null;    return motesFound;  }  /**   * Get colors a certain mote should be painted with. May be overridden to get   * a different color scheme.   *    * Normally this method returns an array of two colors, one for the state   * (outer circle), the other for the type (inner circle).   *    * If this method only returns one color, the entire mote will be painted   * using that.   *    * @param mote   *          Mote to paint   * @return Color[] { Inner color, Outer color }   */  abstract public Color[] getColorOf(Mote mote);  protected void visualizeSimulation(Graphics g) {    for (int i = 0; i < simulation.getMotesCount(); i++) {      Mote mote = simulation.getMote(i);      Color moteColors[] = getColorOf(mote);      Position motePos = mote.getInterfaces().getPosition();      Point pixelCoord = transformPositionToPixel(motePos);      int x = pixelCoord.x;      int y = pixelCoord.y;      if (mote == moteToMove) {        // Don't fill mote      } else if (moteColors.length >= 2) {        g.setColor(moteColors[0]);        g.fillOval(x - MOTE_RADIUS, y - MOTE_RADIUS, 2 * MOTE_RADIUS,            2 * MOTE_RADIUS);        g.setColor(moteColors[1]);        g.fillOval(x - MOTE_RADIUS / 2, y - MOTE_RADIUS / 2, MOTE_RADIUS,            MOTE_RADIUS);      } else if (moteColors.length >= 1) {        g.setColor(moteColors[0]);        g.fillOval(x - MOTE_RADIUS, y - MOTE_RADIUS, 2 * MOTE_RADIUS,            2 * MOTE_RADIUS);      }       g.setColor(Color.BLACK);      g.drawOval(x - MOTE_RADIUS, y - MOTE_RADIUS, 2 * MOTE_RADIUS,          2 * MOTE_RADIUS);    }  }  /**   * Recalculate size of canvas and factors for transforming between real and   * pixel coordinates. This method is called every time this frame is resized   * or created.   */  protected void calculateTransformations() {    if (simulation.getMotesCount() == 0) {      smallestXCoord = 0;      smallestYCoord = 0;      factorXCoordToPixel = 1;      factorYCoordToPixel = 1;      return;    }    double biggestXCoord, biggestYCoord;    Position motePos = simulation.getMote(0).getInterfaces().getPosition();    smallestXCoord = biggestXCoord = motePos.getXCoordinate();    smallestYCoord = biggestYCoord = motePos.getYCoordinate();    // Get extreme coordinates    for (int i = 0; i < simulation.getMotesCount(); i++) {      motePos = simulation.getMote(i).getInterfaces().getPosition();      if (motePos.getXCoordinate() < smallestXCoord)        smallestXCoord = motePos.getXCoordinate();      if (motePos.getXCoordinate() > biggestXCoord)        biggestXCoord = motePos.getXCoordinate();      if (motePos.getYCoordinate() < smallestYCoord)        smallestYCoord = motePos.getYCoordinate();      if (motePos.getYCoordinate() > biggestYCoord)        biggestYCoord = motePos.getYCoordinate();    }    if ((biggestXCoord - smallestXCoord) == 0) {      factorXCoordToPixel = 1;    } else      factorXCoordToPixel = ((double) canvas.getPreferredSize().width - 2 * CANVAS_BORDER_WIDTH)          / (biggestXCoord - smallestXCoord);    if ((biggestYCoord - smallestYCoord) == 0) {      factorYCoordToPixel = 1;    } else      factorYCoordToPixel = ((double) canvas.getPreferredSize().height - 2 * CANVAS_BORDER_WIDTH)          / (biggestYCoord - smallestYCoord);  }  /**   * Transforms a real-world position to a pixel which can be painted onto the   * current sized canvas.   *    * @param pos   *          Real-world position   * @return Pixel coordinates   */  public Point transformPositionToPixel(Position pos) {    return new Point(factorXCoordToPixel(pos.getXCoordinate()),        factorYCoordToPixel(pos.getYCoordinate()));  }  /**   * Transforms real-world coordinates to a pixel which can be painted onto the   * current sized canvas.   *    * @param x Real world X   * @param y Real world Y   * @param z Real world Z (ignored)   * @return Pixel coordinates   */  public Point transformPositionToPixel(double x, double y, double z) {    return new Point(factorXCoordToPixel(x), factorYCoordToPixel(y));  }  /**   * Transforms a pixel coordinate to a real-world. Z-value will always be 0.   *    * @param pixelPos   *          On-screen pixel coordinate   * @return Real world coordinate (z=0).   */  public Position transformPixelToPositon(Point pixelPos) {    Position dummyPosition = new Position(null);    dummyPosition.setCoordinates(factorXPixelToCoord(pixelPos.x),        factorYPixelToCoord(pixelPos.y), 0.0);    return dummyPosition;  }  /**   * @return The current canvas to paint on   */  public JPanel getCurrentCanvas() {    return canvas;  }  private int factorXCoordToPixel(double xCoordinate) {    return (int) ((xCoordinate - smallestXCoord) * factorXCoordToPixel + CANVAS_BORDER_WIDTH);  }  private int factorYCoordToPixel(double yCoordinate) {    return (int) ((yCoordinate - smallestYCoord) * factorYCoordToPixel)        + CANVAS_BORDER_WIDTH;  }  private double factorXPixelToCoord(int xPixel) {    return ((double) (xPixel - CANVAS_BORDER_WIDTH) / factorXCoordToPixel)        + smallestXCoord;  }  private double factorYPixelToCoord(int yPixel) {    return ((double) (yPixel - CANVAS_BORDER_WIDTH) / factorYCoordToPixel)        + smallestYCoord;  }  public void closePlugin() {    if (simObserver != null) {      simulation.deleteObserver(simObserver);      for (int i = 0; i < simulation.getMotesCount(); i++) {        Position posIntf = simulation.getMote(i).getInterfaces().getPosition();        if (posIntf != null) {          posIntf.deleteObserver(posObserver);        }      }    }  }}

⌨️ 快捷键说明

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