-
Bug
-
Resolution: Won't Fix
-
P4
-
None
-
1.2.0
-
x86
-
windows_nt
Name: gsC80088 Date: 02/10/99
According to the javadoc for Graphics2D, "the BasicStroke object always applies a (0.5, 0.5) user
space translation to its pen before stroking a path." However, this doesn't seem to happen when
printing to a PostScript printer.
The following source code illustrates the problem. It strokes an ellipse using a 2-pixel BasicStroke,
and then it fills an ellipse that's offset by (0.5, 0.5). The result should be a 1-pixel border around the
filled ellipse. It works correctly when drawn on the screen, but when it's printed the filled polygon
isn't centered within the border. (The canvas is printed when the window is closed.) Here's the code:
import java.awt.*;
import java.awt.event.*;
import java.awt.print.*;
import java.awt.geom.*;
public class PrintableCanvas extends Canvas implements Printable {
public static void main(String args[]) {
PrintableCanvas canvas = new PrintableCanvas();
Frame frame = new Frame();
frame.setSize(640, 480);
frame.add("Center", canvas);
frame.addWindowListener(new WindowEventHandler(canvas));
frame.show();
}
public void paint(Graphics g) {
draw((Graphics2D) g);
}
public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
if (pageIndex == 0) {
draw((Graphics2D) g);
return PAGE_EXISTS;
} else {
return NO_SUCH_PAGE;
}
}
void draw(Graphics2D g) {
Shape shape1 = new Ellipse2D.Double(99.5, 99.5, 300, 200);
Shape shape2 = new Ellipse2D.Double( 100, 100, 300, 200);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setStroke(new BasicStroke(2));
g.setColor(Color.black);
g.draw(shape1);
g.setColor(Color.yellow);
g.fill(shape2);
}
}
class WindowEventHandler extends WindowAdapter {
private Printable m_Printable;
WindowEventHandler(Printable printable) {
m_Printable = printable;
}
public void windowClosing(WindowEvent event) {
PrinterJob job = PrinterJob.getPrinterJob();
PageFormat format = job.pageDialog(job.defaultPage());
job.setPrintable(m_Printable, format);
if (job.printDialog()) {
try {
job.print();
} catch (Exception e) {
e.printStackTrace();
}
}
System.exit(0);
}
}
(Review ID: 54028)
======================================================================