Java 7 - 在打印时默认情况下会覆盖Graphics2D.setFont()和Graphics2D.setColor()。

时间:2013-12-07 03:43:58

标签: java swing printing graphics2d

我有一个绘制到面板并打印的程序。绘制到画布时,渲染器会生成正确的输出。但是,在绘制到print方法时,字体会被默认字体覆盖。 请看下面显示输出的两个图像。 Canvas or Screen Output

Printed PDF output

特定区域的代码在我的Visualise2D.class中 Canvas和Print的渲染器与它们都称为Visualise2D.class相同。

Visualise2D的一些代码

    public void paintSurfaceTimes(Graphics2D gTimes, SurfaceConnector s, Dummy d, Color c, Rectangle2D bounds,double scale, double enhanceTie, double enhance ){
    gTimes.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    FontMetrics fm = gTimes.getFontMetrics();
    double hEdge = (UnitConvert.pixelsToMeters(LiteTieTRIAL.averageSize));
    double x1 =((d.getEasting() - bounds.getX()));
    double y1 = (bounds.getHeight() + bounds.getY() - d.getNorthing());

    if (d instanceof Hole) {
        hEdge = (UnitConvert.pixelsToMeters(((Hole) d).getDiameter()* enhance)/2);
    }
    else
        hEdge = (UnitConvert.pixelsToMeters(LiteTieTRIAL.averageSize * enhance)/2);

    x1 =((d.getEasting() - bounds.getX()));
    y1 = (bounds.getHeight() + bounds.getY() - d.getNorthing());

    gTimes.setColor(c);
    gTimes.setFont(DEFAULT_FONT);
    fm = gTimes.getFontMetrics();
    gTimes.drawString((dec0P.format(s.getTime())), 
            (int)((x1+hEdge*Math.cos(180))*scale - fm.getStringBounds(dec0P.format(s.getTime()), gTimes).getWidth()),
            (int)((y1 - hEdge*Math.sin(0))*scale+ fm.getStringBounds(dec0P.format(s.getTime()), gTimes).getHeight()));
}

我的getCanvas()方法中的paintComponent(Graphics g)中的一些代码

                    //Draws SurfaceTimes
                if(surfaceTimesOnOffButton.isSelected())    {
                    try {
                        getSurfaceTimes();
                    } catch (NegativeNumberException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    for(Pattern tempPat: world.getPatternList().values()){
                        for(SurfaceConnector sc: tempPat.getSurfaceList().values()){
                            //                          boolean isSelected = sc == selected || (selected instanceof Collection<?> && ((Collection<?>)selected).contains(sc));   

                            if(sc.getTo() instanceof Dummy ){
                                renderer.paintSurfaceTimes(g2, sc, sc.getTo(), colourTimes, bounds, Zoom.getScalingFactor()* UnitConvert.metersToPixels(1), enhanceTie, enhance);
                            }

                        }
                    }
                }

画布绘图代码和可打印代码之间的唯一区别是: 下面的getCanvas()代码......

Visualise2D renderer = new Visualise2D();
            @Override public void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2 = (Graphics2D) g;

LiteTiePrinter.class

public class LiteTiePrinter implements Printable {

public int print(Graphics g,PageFormat pf,int page)抛出PrinterException {

// We have only one page, and 'page'
// is zero-based
if (page > 0) {
     return NO_SUCH_PAGE;
}

Graphics2D g2d = (Graphics2D)g;

g2d.translate(pf.getImageableX(), pf.getImageableY());

// Now we perform our rendering
Visualise2D renderer = new Visualise2D();

对于为什么字体输出不同,我们不胜感激。提前谢谢。

1 个答案:

答案 0 :(得分:0)

欢迎阅读此内容的任何人。我以为我会分享我15小时的搜索互联网和堆栈溢出来找到我自己的问题的答案。我刚刚找到一个并测试了一个建议。它起作用了。

我选择使用 user456837 提供的答案。 如果我有足够的积分,我会投票给这个用户。

答案是用 drawGlyphVector()补充 drawString()。有效!

//GET THE FONTMETRICS
        FontMetrics fm = h2.getFontMetrics();
        //GET THE DIAMETER OF THE HOLE
        double hEdge = (UnitConvert.pixelsToMeters(hole.getDiameter()));
        // GET THE LOCATION OF THE HOLE
        double x1 =((hole.getEasting() - bounds.getX()));
        double y1 = (bounds.getHeight() + bounds.getY() - hole.getNorthing());
        //SET THE COLOUR OF THE TEXT
        h2.setColor(AMETHYST);
        //SET THE FONT TO USE
        h2.setFont(new Font("Monaco", Font.BOLD, 8));
        //USING - DRAWGLYPHVECTOR() TO OVERCOME THE PRINTING ISSUE RELATED TO THE OSX PORT OF JAVA7 - MAY NOT NEED THIS WITH FUTURE UPDATES.
        h2.drawGlyphVector(
                h2.getFont().createGlyphVector(h2.getFontRenderContext(), 
                dec1P.format(hole.getHoleLength())),
                (int)((x1+hEdge*Math.cos(300))*scale)- (fm.stringWidth(dec1P.format(hole.getHoleLength()))/2),  
                (int)((y1 + (hEdge *enhance*1.1)/2)*scale) +(fm.getHeight()/2) +pixelOffset
                );

        /*//USING - DRAWSTRING() DOESN'T WORK IN JAVA 7 ON MAC OSX
        h2.drawString(
                (dec1P.format(hole.getHoleLength())),   
                (int)((x1+hEdge*Math.cos(300))*scale)- (fm.stringWidth(dec1P.format(hole.getHoleLength()))/2),  
                (int)((y1 + (hEdge *enhance*1.1)/2)*scale) +(fm.getHeight()/2) +pixelOffset
                );
*/

我希望这可以帮助任何人搜索相同的问题。另外,我建议你阅读Stephane Grenier关于这个问题的博客。它在一篇博客评论中提到,这在Java 7u40中并不明显......所以也许我最好升级......?

请仔细阅读以下链接

Printing issue on osx