@Override
  public int print(Graphics g, PageFormat pf, int page) throws PrinterException {
    if (page > 0) {
      return NO_SUCH_PAGE;
    }

    int i = pf.getOrientation();

    // get the size of the page
    double pageWidth = pf.getImageableWidth();
    double pageHeight = pf.getImageableHeight();
    double myWidth = this.getWidth(); // - borderWidth * 2;
    double myHeight = this.getHeight(); // - borderWidth * 2;
    double scaleX = pageWidth / myWidth;
    double scaleY = pageHeight / myHeight;
    double minScale = Math.min(scaleX, scaleY);

    Graphics2D g2d = (Graphics2D) g;
    g2d.translate(pf.getImageableX(), pf.getImageableY());
    g2d.scale(minScale, minScale);

    drawPlot(g);

    return PAGE_EXISTS;
  }
Пример #2
0
        public int print(Graphics g,PageFormat pf,int pageIndex) {

                if (pageIndex == 0) {
                        Graphics2D g2d= (Graphics2D)g;
                        g2d.translate(pf.getImageableX(), pf.getImageableY()); 
                        g2d.setColor(Color.black);
                        g2d.drawString("example string", 250, 250);
                        g2d.fillRect(0, 0, 200, 200);
                        return Printable.PAGE_EXISTS;                                   
                } else {
                        return Printable.NO_SUCH_PAGE;
                }
        }
Пример #3
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;
  }
  private void drawPlot(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    g2d.setRenderingHint(
        RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    g2d.setColor(Color.white);
    g2d.fillRect(0, 0, getWidth(), getHeight());

    double activeWidth = getWidth() - leftMargin - rightMargin;
    double activeHeight = getHeight() - topMargin - bottomMargin;
    int bottomY = getHeight() - bottomMargin;
    int rightX = getWidth() - rightMargin;

    // draw data line
    int x1, x2, y1, y2;
    g2d.setColor(Color.red);
    for (int i = 1; i < numComponents; i++) {
      x1 = (int) (leftMargin + ((double) (i - 1) / (numComponents - 1)) * activeWidth);
      y1 = (int) (bottomY - (plotData[i - 1][plotIndex] * 10.0) / 1000 * activeHeight);
      x2 = (int) (leftMargin + ((double) (i) / (numComponents - 1)) * activeWidth);
      y2 = (int) (bottomY - (plotData[i][plotIndex] * 10) / 1000 * activeHeight);
      g2d.drawLine(x1, y1, x2, y2);
    }

    // draw data points
    int radius = 2;
    for (int i = 0; i < numComponents; i++) {
      x1 = (int) (leftMargin + ((double) (i) / (numComponents - 1)) * activeWidth);
      y1 = (int) (bottomY - (plotData[i][plotIndex] * 10.0) / 1000 * activeHeight);
      g2d.drawOval(x1 - radius - 1, y1 - radius - 1, 2 * radius + 2, 2 * radius + 2);
    }

    // draw axes
    g2d.setColor(Color.black);
    g2d.drawLine(leftMargin, bottomY, rightX, bottomY);
    g2d.drawLine(leftMargin, bottomY, leftMargin, topMargin);
    g2d.drawLine(leftMargin, topMargin, rightX, topMargin);
    g2d.drawLine(rightX, bottomY, rightX, topMargin);

    // draw ticks
    int tickSize = 4;
    for (int i = 1; i <= numComponents; i++) {
      x1 = (int) (leftMargin + ((double) (i - 1) / (numComponents - 1)) * activeWidth);
      g2d.drawLine(x1, bottomY, x1, bottomY + tickSize);
    }

    for (int i = 0; i <= 1000; i += 100) {
      y1 = (int) (bottomY - i / 1000.0 * activeHeight);
      g2d.drawLine(leftMargin, y1, leftMargin - tickSize, y1);
    }

    // labels
    DecimalFormat df = new DecimalFormat("#,###,###.###");
    Font font = new Font("SanSerif", Font.PLAIN, 11);
    FontMetrics metrics = g.getFontMetrics(font);
    int hgt, adv;
    hgt = metrics.getHeight();

    // x-axis labels
    String label;
    for (int i = 1; i <= numComponents; i++) {
      label = String.valueOf(i);
      x1 = (int) (leftMargin + ((double) (i - 1) / (numComponents - 1)) * activeWidth);
      adv = metrics.stringWidth(label) / 2;
      g2d.drawString(label, x1 - adv, bottomY + hgt + 4);
    }
    label = "Component";
    adv = metrics.stringWidth(label);
    int xAxisMidPoint = (int) (leftMargin + activeWidth / 2);
    g2d.drawString(label, xAxisMidPoint - adv / 2, bottomY + 2 * hgt + 6);

    // y-axis labels

    // rotate the font
    Font oldFont = g.getFont();
    Font f = oldFont.deriveFont(AffineTransform.getRotateInstance(-Math.PI / 2.0));
    g2d.setFont(f);

    int yAxisMidPoint = (int) (topMargin + activeHeight / 2);
    int offset;
    label = "Explained Variance (%)";
    offset = metrics.stringWidth("100.0") + 12 + hgt;
    adv = metrics.stringWidth(label);
    g2d.drawString(label, leftMargin - offset, yAxisMidPoint + adv / 2);

    // replace the rotated font.
    g2d.setFont(oldFont);

    df = new DecimalFormat("0.0");
    for (int i = 0; i <= 1000; i += 100) {
      label = df.format(i / 10);
      y1 = (int) (bottomY - i / 1000.0 * activeHeight);
      adv = metrics.stringWidth(label);
      g2d.drawString(label, leftMargin - adv - 12, y1 + hgt / 2);
    }

    // title

    // bold font
    oldFont = g.getFont();
    font = font = new Font("SanSerif", Font.BOLD, 12);
    g2d.setFont(font);

    label = "PCA Scree Plot";
    adv = metrics.stringWidth(label);
    g2d.drawString(label, getWidth() / 2 - adv / 2, topMargin - hgt - 5);

    g2d.setFont(oldFont);
  }