宽占满整张纸

public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
    if (pageIndex != 0) {
        return NO_SUCH_PAGE;
    }
    Graphics2D g2d = (Graphics2D) g;

    double pageWidth = pageFormat.getWidth();
    double imageWidth = image.getWidth();
    double imageHeight = image.getHeight();
    double scaleFactor = pageWidth / imageWidth;

    // Computes the x and y coordinates where to draw the image on the page
    double x = 0;  // image will be drawn from the left edge of the paper
    double y = (pageFormat.getHeight() - imageHeight * scaleFactor) / 2;  // vertically centered

    g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
    g2d.drawImage(image, (int) x, (int) y, (int) (imageWidth * scaleFactor), (int) (imageHeight * scaleFactor), null);
    return PAGE_EXISTS;
}

你可能感兴趣的:(java)