Witam.

Problem wygląda nastepująca, że nie wiem jak ustawić wymiary obszaru drukowania.

Przejrzałem dokumentację Oracle i na jej podstawie tworzę sobie klasę:

 

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import java.awt.print.*;


 
public class PrintUIWindow implements Printable, ActionListener {
 
    JPanel panelToPrint;
 
    public int print(Graphics g, PageFormat pf, int page) throws
                                                        PrinterException {
 
        if (page > 0) { /* We have only one page, and 'page' is zero-based */
            return NO_SUCH_PAGE;
        }
 
        /* 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
         */
        Graphics2D g2d = (Graphics2D)g;
        g2d.translate(pf.getImageableX(), pf.getImageableY());

 
        /* Now print the window and its visible contents */
        panelToPrint.printAll(g);
 
        /* tell the caller that this page is part of the printed document */
        return PAGE_EXISTS;
    }
 
    public void actionPerformed(ActionEvent e) {
         PrinterJob job = PrinterJob.getPrinterJob();
         PageFormat pf = job.pageDialog(job.defaultPage());
         job.setPrintable(this);
         boolean ok = job.printDialog();                      //wywołanie dialogu zatwierdzającego drukowanie
         if (ok) {
             try {
                  job.print();
             } catch (PrinterException ex) {
              /* The job did not successfully complete */
             }
         }
    }
 
    public PrintUIWindow(JPanel f) {
        panelToPrint = f;
    }
 
    public static void main(String args[]) {
        UIManager.put("swing.boldMetal", Boolean.FALSE);
        JFrame f = new JFrame("Print UI Example");
 
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(600,500);
        

        ObrazPanel obrazPanel = new ObrazPanel();
        f.add(obrazPanel);
        
    
        JButton printButton = new JButton("Print This Window");
        
        printButton.addActionListener(new PrintUIWindow(obrazPanel));
        f.add("South", printButton);
        f.setVisible(true);
    }
}

Przekazuję sobie do niej obiekt klasy ObiektPanel który chce wydrukować:

 

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JPanel;


public class ObrazPanel extends JPanel {

	private BufferedImage image;
	
	public ObrazPanel() {
		
		super();
		
		File imageFile = new File("java.jpg");
		try {
			image = ImageIO.read(imageFile);
		} catch (IOException e) {
			System.err.println("Błąd odczytu obrazka");
			e.getStackTrace();
		}
		
/*		Dimension dimension = new Dimension(100, 100);
		setPreferredSize(dimension);*/
	}
	
	public void paintComponent(Graphics g) {
		
		Graphics2D g2d = (Graphics2D) g;
		g2d.drawImage(image, 0, 20, this);       //20 - przesunięcie na Y
	}

}

Klasa ta wyświetla obraz jpg, na którym dlaje będę robić operacje (mniejsza o to).

Pytanie brzmi: jak zarządzać wymiarami tego JPanelu przekazywanymi do drukarki (żeby na kartce obraz posiadał wymagane wymiary) ? 

Pozdrawiam