Printing graphics object in Landscape or portrait mode
Hello,
I have a graphics 2d object (a Tree Diagram) which is inside a JScrollPane,and the JSCrollPane is on a JPanel. it's length and width is more than one page. I am using a printable object to print the object. The task I need to do is,
(1). compress the width and length of the diagram, so that if can fit into one page, else split the diagram into pages, so that each part of it can be printed on different pages(page size A4) and then later those pages can be merged accordingly.
I am not able to find, how to split the graphic object(the tree diagram into pages), shoul d I try doing it using x and y co ordinate of the graphic object.
[669 byte] By [
serjitha] at [2007-11-26 15:21:49]

# 1
Draw onto a buffered image, then scale it down with an affine transform. Then draw the image onto the graphics object that the print(Graphics g, PageFormat pageFormat, int page) sends you.Post a SSCCE of your current setup if you want me to modify it to work correctly.-Js
# 2
Just use my StandardPrint class
All you should need to do is do
StandardPrint sp = new StandardPrint(your tree);
http://forum.java.sun.com/thread.jspa?forumID=57&threadID=5122492
you are welcome to use and modify the code, but please do not change the package or take credit for it as your own work
# 3
> Just use my StandardPrint class
>
> All you should need to do is do
>
> StandardPrint sp = new StandardPrint(your tree);
>
> http://forum.java.sun.com/thread.jspa?forumID=57&threa
> dID=5122492
>
> you are welcome to use and modify the code, but
> please do not change the package or take credit for
> it as your own work
Hello,
Thanks for the class, I tried using the StandardPrint class, I am passing a JPanel to the StandardPrint class like this,
StandardPrint print = new StandardPrint(actWorkSpace);
try {
print.start();
} catch (PrinterException ex) {
ex.printStackTrace();
}
here actWorkSpace is a JPanel on which has graphics.
But It is printing blank pages. The diagram on jpanel is somewhat like this....
| -|
| |#-||
| |--#--|| |
| 0--| --#-||-0|
| |--#|_||
| |--#| |
|__|
I need to print this diagram, The diagram is added to a scrollpane which is added to the JPanel.
After trying some available packages on net too, I am not able to figure out what to do...
Please help, it is a urgent.
# 4
/*
* PrintUtilities.java
*
* Created on January 17, 2007, 7:25 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package action.vector.bam;
/**
*
* @author serjith
*/
import java.awt.*;
import javax.swing.*;
import java.awt.print.*;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
/** A simple utility class that lets you very simply print
* an arbitrary component. Just pass the component to the
* PrintUtilities.printComponent. The component you want to
* print doesn't need a print method and doesn't have to
* implement any interface or do anything special at all.
* <P>
* If you are going to be printing many times, it is marginally more
* efficient to first do the following:
* <PRE>
*PrintUtilities printHelper = new PrintUtilities(theComponent);
* </PRE>
* then later do printHelper.print(). But this is a very tiny
* difference, so in most cases just do the simpler
* PrintUtilities.printComponent(componentToBePrinted).
*
* 7/99 Marty Hall, http://www.apl.jhu.edu/~hall/java/
* May be freely used or adapted.
*/
public class PrintUtilities implements Printable {
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();
PrintRequestAttributeSet set = new HashPrintRequestAttributeSet();
PageFormat pgFormat = printJob.pageDialog(set);
printJob.setPrintable(this,pgFormat);
//printJob.setPrintable(this);
if (printJob.printDialog())
try {
printJob.print();
} catch(PrinterException pe) {
System.out.println("Error printing: " + pe);
}
}
public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
if (pageIndex > 10) {
return(NO_SUCH_PAGE);
} else {
Graphics2D g2d = (Graphics2D)g;
g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
disableDoubleBuffering(componentToBePrinted);
int W = componentToBePrinted.getWidth();
int H = componentToBePrinted.getHeight();
//Component newComp = componentToBePrinted.getGraphics().clipRect();
System.out.println("WorkSpace Height "+H+" Width "+W);
pageFormat.getHeight();
pageFormat.getWidth();
pageFormat.setOrientation(PageFormat.LANDSCAPE);
//componentToBePrinted.paint(g2d);
componentToBePrinted.printAll(g2d);//.paintAll(g2d);
enableDoubleBuffering(componentToBePrinted);
return(PAGE_EXISTS);
}
}
/** The speed and quality of printing suffers dramatically if
* any of the containers have double buffering turned on.
* So this turns if off globally.
* @see enableDoubleBuffering
*/
public static void disableDoubleBuffering(Component c) {
RepaintManager currentManager = RepaintManager.currentManager(c);
currentManager.setDoubleBufferingEnabled(false);
}
/** Re-enables double buffering globally. */
public static void enableDoubleBuffering(Component c) {
RepaintManager currentManager = RepaintManager.currentManager(c);
currentManager.setDoubleBufferingEnabled(true);
}
}
=============================================================
Please find this code, which I am trying to use, here I am passing the JPaneltry{
PrintUtilities.printComponent(actWorkSpace);
}catch(Exception ex){
ex.printStackTrace();
}
the actWorkSpace is a JPanel with graphics as mentioned above, which is being passes to PrintUtilities.printComponent();
the output is only just a single page with just a portion of the graphics on the JPanel, pls help...... it is urgent....
# 5
/*
* PrintUtilities.java
*
* Created on January 17, 2007, 7:25 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package action.vector.bam;
/**
*
* @author serjith
*/
import java.awt.*;
import javax.swing.*;
import java.awt.print.*;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
/** A simple utility class that lets you very simply print
* an arbitrary component. Just pass the component to the
* PrintUtilities.printComponent. The component you want to
* print doesn't need a print method and doesn't have to
* implement any interface or do anything special at all.
* <P>
* If you are going to be printing many times, it is marginally more
* efficient to first do the following:
* <PRE>
*PrintUtilities printHelper = new PrintUtilities(theComponent);
* </PRE>
* then later do printHelper.print(). But this is a very tiny
* difference, so in most cases just do the simpler
* PrintUtilities.printComponent(componentToBePrinted).
*
* 7/99 Marty Hall, http://www.apl.jhu.edu/~hall/java/
* May be freely used or adapted.
*/
public class PrintUtilities implements Printable {
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();
PrintRequestAttributeSet set = new HashPrintRequestAttributeSet();
PageFormat pgFormat = printJob.pageDialog(set);
printJob.setPrintable(this,pgFormat);
//printJob.setPrintable(this);
if (printJob.printDialog())
try {
printJob.print();
} catch(PrinterException pe) {
System.out.println("Error printing: " + pe);
}
}
public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
if (pageIndex > 10) {
return(NO_SUCH_PAGE);
} else {
Graphics2D g2d = (Graphics2D)g;
g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
disableDoubleBuffering(componentToBePrinted);
int W = componentToBePrinted.getWidth();
int H = componentToBePrinted.getHeight();
//Component newComp = componentToBePrinted.getGraphics().clipRect();
System.out.println("WorkSpace Height "+H+" Width "+W);
pageFormat.getHeight();
pageFormat.getWidth();
pageFormat.setOrientation(PageFormat.LANDSCAPE);
//componentToBePrinted.paint(g2d);
componentToBePrinted.printAll(g2d);//.paintAll(g2d);
enableDoubleBuffering(componentToBePrinted);
return(PAGE_EXISTS);
}
}
/** The speed and quality of printing suffers dramatically if
* any of the containers have double buffering turned on.
* So this turns if off globally.
* @see enableDoubleBuffering
*/
public static void disableDoubleBuffering(Component c) {
RepaintManager currentManager = RepaintManager.currentManager(c);
currentManager.setDoubleBufferingEnabled(false);
}
/** Re-enables double buffering globally. */
public static void enableDoubleBuffering(Component c) {
RepaintManager currentManager = RepaintManager.currentManager(c);
currentManager.setDoubleBufferingEnabled(true);
}
}
=============================================================
Please find this code, which I am trying to use, here I am passing the JPanel
try{
PrintUtilities.printComponent(actWorkSpace);
}catch(Exception ex){
ex.printStackTrace();
}
the actWorkSpace is a JPanel with graphics as mentioned above, which is being passes to PrintUtilities.printComponent();
the output is only just a single page with just a portion of the graphics on the JPanel, pls help...... it is urgent....
# 6
/*
* PrintUtilities.java
*
* Created on January 17, 2007, 7:25 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package action.vector.bam;
/**
*
* @author serjith
*/
import java.awt.*;
import javax.swing.*;
import java.awt.print.*;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
/** A simple utility class that lets you very simply print
* an arbitrary component. Just pass the component to the
* PrintUtilities.printComponent. The component you want to
* print doesn't need a print method and doesn't have to
* implement any interface or do anything special at all.
* <P>
* If you are going to be printing many times, it is marginally more
* efficient to first do the following:
* <PRE>
*PrintUtilities printHelper = new PrintUtilities(theComponent);
* </PRE>
* then later do printHelper.print(). But this is a very tiny
* difference, so in most cases just do the simpler
* PrintUtilities.printComponent(componentToBePrinted).
*
* 7/99 Marty Hall, http://www.apl.jhu.edu/~hall/java/
* May be freely used or adapted.
*/
public class PrintUtilities implements Printable {
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();
PrintRequestAttributeSet set = new HashPrintRequestAttributeSet();
PageFormat pgFormat = printJob.pageDialog(set);
printJob.setPrintable(this,pgFormat);
//printJob.setPrintable(this);
if (printJob.printDialog())
try {
printJob.print();
} catch(PrinterException pe) {
System.out.println("Error printing: " + pe);
}
}
public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
if (pageIndex > 0) { // requires change since it is bigger than a single page
return(NO_SUCH_PAGE);
} else {
Graphics2D g2d = (Graphics2D)g;
g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
disableDoubleBuffering(componentToBePrinted);
int W = componentToBePrinted.getWidth();
int H = componentToBePrinted.getHeight();
//Component newComp = componentToBePrinted.getGraphics().clipRect();
System.out.println("WorkSpace Height "+H+" Width "+W);
pageFormat.getHeight();
pageFormat.getWidth();
pageFormat.setOrientation(PageFormat.LANDSCAPE);
//componentToBePrinted.paint(g2d);
componentToBePrinted.printAll(g2d);//.paintAll(g2d);
enableDoubleBuffering(componentToBePrinted);
return(PAGE_EXISTS);
}
}
/** The speed and quality of printing suffers dramatically if
* any of the containers have double buffering turned on.
* So this turns if off globally.
* @see enableDoubleBuffering
*/
public static void disableDoubleBuffering(Component c) {
RepaintManager currentManager = RepaintManager.currentManager(c);
currentManager.setDoubleBufferingEnabled(false);
}
/** Re-enables double buffering globally. */
public static void enableDoubleBuffering(Component c) {
RepaintManager currentManager = RepaintManager.currentManager(c);
currentManager.setDoubleBufferingEnabled(true);
}
}
=============================================================
Please find this code, which I am trying to use, here I am passing the JPanel
try{
PrintUtilities.printComponent(actWorkSpace);
}catch(Exception ex){
ex.printStackTrace();
}
the actWorkSpace is a JPanel with graphics as mentioned above, which is being passes to PrintUtilities.printComponent();
the output is only just a single page with just a portion of the graphics on the JPanel, pls help...... it is urgent....
