Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4210697

BasicStroke doesn't offset by (0.5, 0.5) when printing PostScript

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P4
    • Resolution: Won't Fix
    • Affects Version/s: 1.2.0
    • Fix Version/s: None
    • Component/s: client-libs
    • Labels:
    • Subcomponent:
      2d
    • CPU:
      x86
    • OS:
      windows_nt

      Description



      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)
      ======================================================================

        Attachments

          Activity

            People

            Assignee:
            prr Philip Race
            Reporter:
            gstone Greg Stone
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: