F
fredjeboy
Hi,
I've made a class My_panel which extends JPanel and implements the
Printable Interface. I do some drawings in the paintComponent(Graphics
g)... But this is quite width, so later I put my object of My_panel in
a JScrollPane and add it to the content pane of my JApplet.
No the only problem in My_panel is that it does't succees in printing
my "drawing" over muliple pages... Here's my code
public class My_Panel extends JPanel implements Printable{
public void paintComponent(Graphics g){
//doing some large drawings
}
int teller=0; //initialisation counter
public int print(Graphics g, PageFormat pageFormat, int
pageIndex)throws PrinterException{
Graphics2D g2 = (Graphics2D) g;
pageFormat.setOrientation(PageFormat.LANDSCAPE);
double pageHeight = pageFormat.getImageableHeight();
double pageWidth = pageFormat.getImageableWidth();
double chartWidth = (double) this.getWidth();
double chartHeight = (double) this.getHeight();
int totalNumPages = (int) Math.round(chartWidth/pageWidth);
if(pageIndex >= totalNumPages)
{
return Printable.NO_SUCH_PAGE;
}
g2.translate(pageFormat.getImageableX()+teller*pageWidth,pageFormat.getImageableY());//translate
to printable area
this.printAll(g2);
teller++;
return Printable.PAGE_EXISTS;
}
public void printPanel() {
PrinterJob printJob = PrinterJob.getPrinterJob();
PageFormat pageFormat =
printJob.pageDialog(printJob.defaultPage());
printJob.setPrintable(this,pageFormat);
if (printJob.printDialog()){
try {
printJob.print();
System.out.println("Starting print...");
} catch(PrinterException pe) {
System.out.println("Error printing: " + pe);
}
}
}
....
}
I've also tried come clipping on g2 but this also did'tn work. What I
get now is that my printer prints some blank pages...
Anyone?
I've made a class My_panel which extends JPanel and implements the
Printable Interface. I do some drawings in the paintComponent(Graphics
g)... But this is quite width, so later I put my object of My_panel in
a JScrollPane and add it to the content pane of my JApplet.
No the only problem in My_panel is that it does't succees in printing
my "drawing" over muliple pages... Here's my code
public class My_Panel extends JPanel implements Printable{
public void paintComponent(Graphics g){
//doing some large drawings
}
int teller=0; //initialisation counter
public int print(Graphics g, PageFormat pageFormat, int
pageIndex)throws PrinterException{
Graphics2D g2 = (Graphics2D) g;
pageFormat.setOrientation(PageFormat.LANDSCAPE);
double pageHeight = pageFormat.getImageableHeight();
double pageWidth = pageFormat.getImageableWidth();
double chartWidth = (double) this.getWidth();
double chartHeight = (double) this.getHeight();
int totalNumPages = (int) Math.round(chartWidth/pageWidth);
if(pageIndex >= totalNumPages)
{
return Printable.NO_SUCH_PAGE;
}
g2.translate(pageFormat.getImageableX()+teller*pageWidth,pageFormat.getImageableY());//translate
to printable area
this.printAll(g2);
teller++;
return Printable.PAGE_EXISTS;
}
public void printPanel() {
PrinterJob printJob = PrinterJob.getPrinterJob();
PageFormat pageFormat =
printJob.pageDialog(printJob.defaultPage());
printJob.setPrintable(this,pageFormat);
if (printJob.printDialog()){
try {
printJob.print();
System.out.println("Starting print...");
} catch(PrinterException pe) {
System.out.println("Error printing: " + pe);
}
}
}
....
}
I've also tried come clipping on g2 but this also did'tn work. What I
get now is that my printer prints some blank pages...
Anyone?