|
-
August 9th, 1999, 01:02 PM
#1
Printing
I am working with an application that will print and am trying to learn how Java handles printing graphics, which isn't easy since there isn't much documentation on printing around. I came across a program that I typed in as and example and it doens't work. I copied this out of a published book, and have tried to run it on a UNIX box and a PC. The program creates a JFrame with two buttons and a scroll pane. The pane is used to display a graphic(gif or jpeg) and the two buttons are used to print either the whole frame or just the picture. When I select the button to print the entire frame, I get a copy of what i see on the screen. Fine. However, when i hit the button to print only the pic it throws all kinds of exceptions. The following code is part of what I was using and comes from the ActionListener Adapter added to each button:
public void actionPerformed(ActionEvent evt) {
Properties props = new Properties();
button1.setEnabled(false);
button2.setEnabled(false);
PrintJob pj = Toolkit.getDefaultToolkit().getPrintJob(PrintingExample1.this, "Print Test", props);
if(pj != null) {
System.out.println("Returned properties: " + props);
JButton b = (JButton)evt.getSource();
Component c;
Graphics g = pj.getGraphics();
if(b == button1) {
c = PrintingExample1.this;
} else {
c = scrolledObject;
}
Dimension od = c.getSize();
Dimension pd = pj.getPageDimension();
g.translate((pd.width - od.width) / 2, (pd.height - od.height) / 2);
if(b == button1) {
c.printAll(g);
} else {
c.print(g);
}
g.dispose();
pj.end();
}
button1.setEnabled(true);
button2.setEnabled(true);
}
Basically, if button1 is pressed, the graphics are loaded from the main JFrame created by the main method; if button2 is pressed, the graphics from the scrolledObject should be printed. The scrolledObject is passed in as and icon, which is added to a label, inorder to be displayed in the scrollPane. I have isolated the line of code which is causing the problem, it is the line "c.print(g);" For some reason the print method of the label is causing the problem, but i can't figure out why, especially since this is code from a published book. I've tried tweaking the program and using a button and other things as the component, but can't get it to print. Any suggestions on this or any remarks on printing in general would be greatly appreciated.
thank you.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|