I have a small, working Java GUI app that allows a user to place SVG images on the screen. Each SVG image is exactly 9.0 pixels tall and wide. This is verified in the code by outputting GraphicsNode.getBounds().getWidth() and getHeight(). I draw a grid framework with lines every 10 pixels so users can click to place any SVG symbol into the empty space between gridlines. (Like placing X's and O's in a tic-tac-toe game). On screen it looks perfect, even scaled to 800%. However printing has a small problem. The SVG symbols print a little too far to the right and too far down to properly fit within their grid "cell". They print over the right and bottom gridline. I use the exact same code for screen display and printing. I use GraphicsNode.paint() to get a nice smooth image even scaled to 800%. I have "fixed" this printing problem with this line before painting any SVG symbols when printing: g2d.translate(-0.5, -0.5); I have created my 9 pixel symbols in Illustrator and a program I found on sourceforge called sodipodi. So I think the problem is not in the SVG data but in the Batik renderer (or my use of it). Any idea why this is required to print at an exact location?
This is rather complex but basically has to do with what does a line from 0,10 to 10,10 draw? If you have an 'integer' coordinate system (JDK 1.1) you want one row of pixels lit from 0 to 10 on row 10 - this implies that '10' is in the 'middle' of the 10th row of pixles. This is fine for integer coordinate systems but when you introduce floating point coordinate systems this is really shifting everything by .5 pixel. Without the shift it would half lite the 9 and 10 rows. What you are seeing is that when you switch to printing it still shifts by 1/2 a pixel but that pixel is now 5 or 6 times smaller than the screen pixel.
I think your solution is the suggested one.
Thanks, Chris
Following is an excerpt of my display/print code:
public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D)g; drawPage(g2d, false); } public int print(Graphics g, PageFormat pf, int page) throws PrinterException { if(page >= 1) return Printable.NO_SUCH_PAGE; Graphics2D g2d = (Graphics2D)g; g2d.translate((int)pf.getImageableX(), (int)pf.getImageableY()); drawPage(g2d, true); return Printable.PAGE_EXISTS; } public void drawPage(Graphics2D g2d, boolean printFlag) { RenderingHints rh = new RenderingHints(null); rh.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.setRenderingHints(rh); g2d.setStroke(new BasicStroke(0.01f)); int gridWidth = 99; // 10x10 grid of 9 pixel symbols int gridHeight = 99; // with 1-pixel separating lines for(int i = 1; i < 10; i++) { int width = (9 * i) + i - 1; g2d.draw(new Line2D.Float(width, 0, width, gridHeight)); } for(int j = 1; j < 10; j++) { int height = (9 * j) + j - 1; g2d.draw(new Line2D.Float(0, height, gridWidth, height)); } int iconLineWidth = 10; // svg image plus line (9+1) int iconLineHeight = 10; if(printFlag == true) { g2d.translate(-0.5f, -0.5f); // KLUDGE }
for(int x = 0; x < 10; x++) { for(int y = 0; y < 10; y++) { if(grid[x][y] != -1) { svgSymbol(grid[x][y]).paint(g2d); } g2d.translate(0, iconLineHeight); } g2d.translate(iconLineWidth, -10 * iconLineHeight); } }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
