@Override
  public void mouseWheelMoved(MouseWheelEvent evt) {
    Point current = evt.getPoint();
    Rectangle bound = viewer.getViewportBounds();

    double dx = current.x - bound.width / 2;
    double dy = current.y - bound.height / 2;

    Dimension oldMapSize = viewer.getTileFactory().getMapSize(viewer.getZoom());

    viewer.setZoom(viewer.getZoom() + evt.getWheelRotation());

    Dimension mapSize = viewer.getTileFactory().getMapSize(viewer.getZoom());

    Point2D center = viewer.getCenter();

    double dzw = (mapSize.getWidth() / oldMapSize.getWidth());
    double dzh = (mapSize.getHeight() / oldMapSize.getHeight());

    double x = center.getX() + dx * (dzw - 1);
    double y = center.getY() + dy * (dzh - 1);

    viewer.setCenter(new Point2D.Double(x, y));
  }
Esempio n. 2
0
  /**
   * Get line-drawing (red line) with arrow
   *
   * @param map JXMapViewer
   * @param mousePos Point2D
   * @return ArrayList<Line2D.Double>
   */
  public ArrayList<Line2D.Double> getLines(JXMapViewer map, Point2D mousePos) {
    ArrayList<Line2D.Double> lines = new ArrayList<Line2D.Double>();
    if (p1 != null) {

      // Initialize variables
      Point2D pt1 = null;
      Point2D pt2 = null;
      Point2D middle = null;
      double length = 10;

      // Set pt1 and pt2
      if (p2 == null && p1 != null) {
        pt1 = map.getTileFactory().geoToPixel(p1.getGeoposition(), map.getZoom());
        pt2 =
            map.getTileFactory().geoToPixel(map.convertPointToGeoPosition(mousePos), map.getZoom());
      } else {
        pt1 = map.getTileFactory().geoToPixel(p1.getGeoposition(), map.getZoom());
        pt2 = map.getTileFactory().geoToPixel(p2.getGeoposition(), map.getZoom());
      }

      // Get red line
      Line2D.Double line =
          new Line2D.Double((int) pt1.getX(), (int) pt1.getY(), (int) pt2.getX(), (int) pt2.getY());
      lines.add(line);

      // Set middle point
      if (pt1.getX() == pt2.getX()) {
        middle = new Point2D.Double(pt1.getX(), (pt1.getY() + pt2.getY()) / 2);
      } else if (pt1.getY() == pt2.getY()) {
        middle = new Point2D.Double((pt1.getX() + pt2.getX()) / 2, pt1.getY());
      } else {
        middle = new Point2D.Double((pt1.getX() + pt2.getX()) / 2, (pt1.getY() + pt2.getY()) / 2);
      }

      // Get length of Line
      double difX = (double) pt1.getX() - (double) pt2.getX();
      double difY = (double) pt1.getY() - (double) pt2.getY();

      // Initialize variables
      Point2D arrowLineX = null;
      Point2D arrowLineY = null;

      // Calculate angle for drawing red arrow
      if (difY == 0) {
        double deltaX = Math.sqrt((length * length) / 2);
        if (difX < 0) {
          deltaX = deltaX * -1;
        }
        double deltaY = deltaX;
        arrowLineX = new Point2D.Double(middle.getX() + deltaX, middle.getY() + deltaY);
        arrowLineY = new Point2D.Double(middle.getX() + deltaY, middle.getY() - deltaX);
      } else {
        double angle = Math.atan(difX / difY);
        double arrowAngle = angle - 95;
        double deltaX = length * Math.sin(arrowAngle);
        double deltaY = length * Math.cos(arrowAngle);

        if (difY > 0) {
          arrowLineX = new Point2D.Double(middle.getX() + deltaX, middle.getY() + deltaY);
          arrowLineY = new Point2D.Double(middle.getX() + deltaY, middle.getY() - deltaX);
        } else {
          arrowLineX = new Point2D.Double(middle.getX() - deltaY, middle.getY() + deltaX);
          arrowLineY = new Point2D.Double(middle.getX() - deltaX, middle.getY() - deltaY);
        }
      }

      // Get red arrow
      Line2D.Double pijltje1 =
          new Line2D.Double(
              (int) middle.getX(),
              (int) middle.getY(),
              (int) arrowLineY.getX(),
              (int) arrowLineY.getY());
      Line2D.Double pijltje2 =
          new Line2D.Double(
              (int) middle.getX(),
              (int) middle.getY(),
              (int) arrowLineX.getX(),
              (int) arrowLineX.getY());
      lines.add(pijltje1);
      lines.add(pijltje2);
    }

    // Return red line + arrow
    return lines;
  }