Example #1
0
  /**
   * Gets the distance (in primitive's coordinates space) between a given point and the primitive.
   * When it is reasonable, the behaviour can be binary (polygons, ovals...). In other cases (lines,
   * points), it can be proportional.
   *
   * @param px the x coordinate of the given point.
   * @param py the y coordinate of the given point.
   * @return the distance in logical units.
   */
  public int getDistanceToPoint(int px, int py) {
    /* in the macro primitive, the the first virtual point represents
    the position of the reference point of the macro to be drawn. */

    int x1 = virtualPoint[0].x;
    int y1 = virtualPoint[0].y;
    int dt = Integer.MAX_VALUE;

    // Here we check if the given point lies inside the text areas

    if (checkText(px, py)) return 0;

    // If not, we need to see more throughly about the inners of the macro

    int vx = px - x1 + 100;
    int vy = py - y1 + 100;

    // This is a sort of inelegant code: we need to translate the position
    // given in the macro's coordinate system.

    if (m) {
      switch (o) {
        case 1:
          vx = py - y1 + 100;
          vy = px - x1 + 100;
          break;

        case 2:
          vx = px - x1 + 100;
          vy = -(py - y1) + 100;
          break;

        case 3:
          vx = -(py - y1) + 100;
          vy = -(px - x1) + 100;
          break;

        case 0:
          vx = -(px - x1) + 100;
          vy = py - y1 + 100;
          break;

        default:
          vx = 0;
          vy = 0;
          break;
      }
    } else {
      switch (o) {
        case 1:
          vx = py - y1 + 100;
          vy = -(px - x1) + 100;
          break;

        case 2:
          vx = -(px - x1) + 100;
          vy = -(py - y1) + 100;
          break;

        case 3:
          vx = -(py - y1) + 100;
          vy = px - x1 + 100;
          break;

        case 0:
          vx = px - x1 + 100;
          vy = py - y1 + 100;
          break;

        default:
          vx = 0;
          vy = 0;
          break;
      }
    }

    if (macroDesc == null)
      System.out.println("1-Unrecognized macro " + "WARNING this can be a programming problem...");
    else {
      SelectionActions sa = new SelectionActions(macro);
      EditorActions edt = new EditorActions(macro, sa, null);
      return Math.min(edt.distancePrimitive(vx, vy), dt);
    }
    return Integer.MAX_VALUE;
  }
Example #2
0
  /**
   * The printing interface.
   *
   * @param g the graphic context.
   * @param pf the page format.
   * @param page the page number.
   * @return PAGE_EXISTS if the page has to be printed.
   * @throws PrinterException if a printing error occurs.
   */
  public int print(Graphics g, PageFormat pf, int page) throws PrinterException {
    int npages = 0;

    // This might be explained as follows:
    // 1 - The Java printing system normally works with an internal
    // resolution which is 72 dpi (probably inspired by Postscript).
    // 2 - To have a sufficient resolution, this is increased by 16 times,
    // by using the scale method of the graphic object associated to the
    // printer. This gives a 72 dpi * 16=1152 dpi resolution.
    // 3 - The 0.127 mm pitch used in FidoCadJ corresponds to a 200 dpi
    // resolution. Calculating 1152 dpi / 200 dpi gives the 5.76 constant

    double xscale = 1.0 / 16; // Set 1152 logical units for an inch
    double yscale = 1.0 / 16; // as the standard resolution is 72
    double zoom = 5.76; // act in a 1152 dpi resolution as 1:1

    Graphics2D g2d = (Graphics2D) g;

    // User (0,0) is typically outside the imageable area, so we must
    // translate by the X and Y values in the PageFormat to avoid clipping

    if (printMirror) {
      g2d.translate(pf.getImageableX() + pf.getImageableWidth(), pf.getImageableY());
      g2d.scale(-xscale, yscale);

    } else {
      g2d.translate(pf.getImageableX(), pf.getImageableY());
      g2d.scale(xscale, yscale);
    }

    int printerWidth = (int) pf.getImageableWidth() * 16;

    // Perform an adjustement if we need to fit the drawing to the page.
    if (printFitToPage) {
      MapCoordinates zoomm =
          DrawingSize.calculateZoomToFit(
              cc.dmp, (int) pf.getImageableWidth() * 16, (int) pf.getImageableHeight() * 16, false);
      zoom = zoomm.getXMagnitude();
    }

    MapCoordinates m = new MapCoordinates();

    m.setMagnitudes(zoom, zoom);

    PointG o = new PointG(0, 0);

    int imageWidth = DrawingSize.getImageSize(cc.dmp, zoom, false, o).width;
    npages = (int) Math.floor((imageWidth - 1) / (double) printerWidth);

    // Check if we need more than one page
    if (printerWidth < imageWidth) {
      g2d.translate(-(printerWidth * page), 0);
    }

    // Check if printing is finished.
    if (page > npages) {
      return NO_SUCH_PAGE;
    }
    // Now we perform our rendering
    cc.drawingAgent.draw(new Graphics2DSwing(g2d), m);

    /* tell the caller that this page is part of the printed document */
    return PAGE_EXISTS;
  }