Phil already talked about why you are having resolution problems, but I
wanted to point out a mistake in your code that was causing "some of the
operations" to be lost.
[EMAIL PROTECTED] wrote:
Hi, I'm trying to build up a BufferedImage using its Graphics component, and
then print that image using a Printable. The problem is that some of my
operations on the graphics component seem to be lost, and when I print the
image, a lot of it is clipped off around the borders. The code below shows the
gist of what I'm doing.
// this method is in class A
foo(String text, Font font, B b, int x, int y) {
Graphics2D g2d = (Graphics2D) image.getGraphics();
g2d.setColor(Color.black); // this color gets lost when bar is called
g2d.setFont(font); // this font gets lost when bar is called
b.bar(text, x, y);
In this code you never use g2d to draw anything. You call getGraphics()
on the image, which constructs a brand new Graphics object that is not
shared with any other call to getGraphics() and then you set some
attributes (color and font) on that graphics object and then you drop
the reference on the floor so those attributes will never get used
anywhere to draw anything. The key things here are:
Image.getGraphics() always returns a new object that is never shared.
Graphics.setColor() sets the color for only that one graphics object, it
has no effect on any other graphics object. (The same is true with
setFont()).
}
// this method is in class B
bar(String text, int x, int y) {
Graphics2D g2d = (Graphics2D) image.getGraphics();
Here you obtain another brand new unshared graphics object from the
image which is set to default values for all attributes. In particular,
this object will not have the color or font that you set in foo()
because those attributes were set on a different graphics object.
g2d.drawString(text, x, y); // this does not get lost when the image is
printed
}
// this is the print method for the Printable, the image is passed to the
Printable
print(Graphics g, PageFormat pf, int pageIndex) {
if (pageIndex == 0) {
Graphics2D g2d = (Graphics2D)image.getGraphics();
g2d.translate(pf.getImageableX(), pf.getImageableY());
Here you obtain a 3rd graphics object from the image and you set its
translation. But, again, since you never draw anything with this g2d,
the translation you set never gets used for anything.
((Graphics2D)graphics).drawImage(image, null, 0, 0);
Finally here you use some other graphics object (it isn't the one that
was passed in to the print method - that one was called "g" - and it
isn't the one that was obtained from the image - that one was called
"g2d", so I'm not sure where you are drawing this image to - it's being
drawn to this mystery object called "graphics" that none of the code you
included traces the creation of...?
return Printable.PAGE_EXISTS;
} else {
return Printable.NO_SUCH_PAGE;
}
}
If I hardcode the color and font in the bar method, then the text actually comes out at the
printer, but if x < 80 or x > 500, it doesn't get printed; same if y < 80 or y
> 600 (these are approximate, I'm just estimating based on what I printed and where it
got cut off). This leaves about a 1 inch margin around the printed text on the paper from
the printer. Ultimately, I want to generate a document image using j2d and send that to
the printer. Any help is greatly appreciated!
My comments above should help you realize why putting the color and font
into the bar method is quite a different set of operations than trying
to do it from the foo() method. You cannot "leave some attributes in a
graphics object to be used by future graphics objects" because each call
to getGraphics() returns a brand new instance with preset defaults.
...jim
===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".