Ejemplo n.º 1
0
  @Override
  public void setVisible(boolean b) {
    // In case something changed while the dialog was closed...
    enforceButtonRules();

    if (b) {
      SwingUtil.centerOver(this, TabletopTool.getFrame());
    }
    super.setVisible(b);
  }
  /**
   * @see
   *     com.t3.client.tool.drawing.RadiusTemplateTool#handleMouseMovement(java.awt.event.MouseEvent)
   */
  @Override
  protected void handleMouseMovement(MouseEvent e) {
    // Set the anchor
    ZonePoint vertex = template.getVertex();
    if (!anchorSet) {
      setCellAtMouse(e, vertex);
      controlOffset = null;

      // Move the anchor if control pressed.
    } else if (SwingUtil.isControlDown(e)) {
      handleControlOffset(e, vertex);

      // Add or delete a new cell
    } else {

      // Get mouse point as an offset from the vertex
      LineTemplate lt = ((LineTemplate) template);
      ZonePoint mouse = new ScreenPoint(e.getX(), e.getY()).convertToZone(renderer);
      CellPoint mousePoint = renderer.getZone().getGrid().convert(mouse);
      CellPoint vertexPoint = renderer.getZone().getGrid().convert(lt.getVertex());
      mousePoint.x = mousePoint.x - vertexPoint.x;
      mousePoint.y = mousePoint.y - vertexPoint.y;

      // Compare to the second to last point, if == delete last point
      List<CellPoint> path = lt.getPath();
      CellPoint lastPoint = path.get(path.size() - 1);
      int dx = mousePoint.x - lastPoint.x;
      int dy = mousePoint.y - lastPoint.y;
      if (dx != 0 && dy == 0 || dy != 0 && dx == 0) {
        int count = Math.max(Math.abs(dy), Math.abs(dx));
        dx = dx == 0 ? 0 : dx / Math.abs(dx);
        dy = dy == 0 ? 0 : dy / Math.abs(dy);
        for (int i = 1; i <= count; i++) {
          CellPoint current = lt.getPointFromPool(lastPoint.x + dx * i, lastPoint.y + dy * i);
          if (path.size() > 1 && path.get(path.size() - 2).equals(current)) {
            lt.addPointToPool(path.remove(path.size() - 1));
            lt.addPointToPool(current);
          } else {
            path.add(current);
          } // endif
        } // endfor
      } // endif
      renderer.repaint();
      controlOffset = null;
    } // endif
  }
  @Override
  protected void paintComponent(Graphics g) {

    // Image
    Dimension size = getSize();
    if (img != null) {
      Dimension imgSize = new Dimension(img.getWidth(null), img.getHeight(null));
      SwingUtil.constrainTo(imgSize, size.width, size.height);

      // Border
      int x = (size.width - imgSize.width) / 2;
      int y = (size.height - imgSize.height) / 2;

      g.drawImage(img, x, y, imgSize.width, imgSize.height, null);
      g.setColor(Color.black);
      g.drawRect(x, y, imgSize.width - 1, imgSize.height - 1);
    }
  }