All Downloads are FREE. Search and download functionalities are using the official Maven repository.

studio.utils.PrintUtilities Maven / Gradle / Ivy

package studio.utils;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.awt.*;
import javax.swing.*;
import java.awt.print.*;

public class PrintUtilities implements Printable {

    private static final Logger log = LogManager.getLogger();
    private Component componentToBePrinted;
    
    public static void printComponent(Component c) {
        new PrintUtilities(c).print();
    }
    
    public PrintUtilities(Component componentToBePrinted) {
        this.componentToBePrinted = componentToBePrinted;
    }
    
    public void print() {
        PrinterJob printJob = PrinterJob.getPrinterJob();
        printJob.setPrintable(this);
        if(printJob.printDialog())
            try {
                printJob.print();
            } catch (PrinterException pe) {
                log.error("Printing error", pe);
        }
    }
    
    public int print(Graphics g, PageFormat pf, int pageIndex) {
        int response = NO_SUCH_PAGE;
        
        Graphics2D g2 = (Graphics2D) g;
        disableDoubleBuffering(componentToBePrinted);        
        Dimension d = componentToBePrinted.getSize();
        double panelWidth  = d.width; 
        double panelHeight = d.height;
        double pageHeight = pf.getImageableHeight();
        double pageWidth  = pf.getImageableWidth();
        double scale = pageWidth / panelWidth;
        int totalNumPages = (int) Math.ceil(scale * panelHeight / pageHeight);
        if (pageIndex >= totalNumPages) {
            response = NO_SUCH_PAGE;
        } else {            
            g2.translate(pf.getImageableX(), pf.getImageableY());
            g2.translate(0f, -pageIndex * pageHeight);
            g2.scale(scale, scale);
            componentToBePrinted.paint(g2);
            enableDoubleBuffering(componentToBePrinted);
            response = Printable.PAGE_EXISTS;
        }
        return response;
    }
    
    public static void disableDoubleBuffering(Component c) {
        RepaintManager currentManager = RepaintManager.currentManager(c);
        currentManager.setDoubleBufferingEnabled(false);
    }
    
    public static void enableDoubleBuffering(Component c) {
        RepaintManager currentManager = RepaintManager.currentManager(c);
        currentManager.setDoubleBufferingEnabled(true);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy