xmlgraphics-batik-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Chris Borchert <cborch...@jam.rr.com>
Subject Symbol printing offset issue
Date Thu, 12 Feb 2004 16:55:16 GMT
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 

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?


Following is an excerpt of my display/print code:

public void paintComponent(Graphics g)
    Graphics2D g2d = (Graphics2D)g;
    drawPage(g2d, false);

public int print(Graphics g, PageFormat pf, int page) throws 
    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);

    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)
            g2d.translate(0, iconLineHeight);
        g2d.translate(iconLineWidth, -10 * iconLineHeight);

To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org

View raw message