Buongiorno,
da java sto provando a stampare il contenuto di una JFrame quindi con le immagini, le etichette, i campi di testo etc. in essa contenuti.
Per fare ciò sto usando un esempio preso dal web con il seguente codice:
codice:
public class ProvaFrm extends javax.swing.JFrame implements Printable
...
PrinterJob job = PrinterJob.getPrinterJob();
PageFormat format = job.defaultPage();
format.setOrientation(PageFormat.PORTRAIT);
job.setPrintable(this, format);
try{
if(job.printDialog()) job.print();
} catch(Exception e){e.printStackTrace(); }
Ho dovuto quindi fare l'Override del metodo print
codice:
@Override
public int print(Graphics g, PageFormat format, int pagenum) {
if (pagenum > 0) return Printable.NO_SUCH_PAGE;
g.translate((int)format.getImageableX(), (int)format.getImageableY());
float pageWidth = (float)format.getImageableWidth();
float pageHeight = (float)format.getImageableHeight();
float imageHeight = (float)this.getHeight();
float imageWidth = (float)this.getWidth();
float scaleFactor = Math.min((float)pageWidth/(float)imageWidth, (float)pageHeight/(float)imageHeight);
int scaledWidth = (int)(((float)imageWidth)*scaleFactor);
int scaledHeight = (int)(((float)imageHeight)*scaleFactor);
BufferedImage canvas = new BufferedImage( this.getWidth(), this.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D gg = canvas.createGraphics();
this.paint( gg );
Image img = canvas ;
g.drawImage(img, 0, 0, scaledWidth, scaledHeight, null );
return Printable.PAGE_EXISTS;
}
Ora il problema è che la stampa la fa, sia su carta che su file, ma con un bordo nero tutto intorno (come da esempio del file allegato), e non riesco a capire come eliminarlo.
Qualcuno gentilmente riuscirebbe ad aiutarmi?