如何继承系统的反别名设置,以便将文本绘制为像swing一样的屏幕外图像? [英] How can I inherit the system's anti-alias setting for painting text to off-screen images like swing does?

查看:171
本文介绍了如何继承系统的反别名设置,以便将文本绘制为像swing一样的屏幕外图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在Java 6下运行swing GUI应用程序时,它们会自动为所有字体使用我配置的子像素抗锯齿设置。结果比标准AA选项大大改进。

When I run my swing GUI applications under Java 6, they automatically use my configured sub-pixel anti-alias settings for all fonts. The result is much improved over standard AA options.

但是当我绘制到图像时,我无法初始化图形上下文以使用系统的AA配置。尝试使用Java的不同AA提示是一个失败的原因,因为没有子像素方法适用于所有用户。

But when I paint to an image I can find no way to initialize the graphics context to use the system's AA configuration. Trying to play around with Java's different AA hints is a lost cause because no sub-pixel method will work for all users.

有没有办法继承系统AA设置给定的图形上下文而不是必须选择一个并明确设置提示?目前我必须使用GASP AA来避免标准AA用小字体给出的可怕结果。我试过没有为文本AA设置任何内容,也没有设置任何AA提示。

Is there any way to inherit system AA settings for a given graphics context instead of having to pick one and explicitly set the hint? At the moment I have to use GASP AA to avoid the horrible results that standard AA gives with small fonts. I have tried not setting anything for text AA, and not setting any AA hints at all.

更新2010-01-05

我想我已经把它钉死了;当直接绘制到AWT图形上下文时,子像素AA提示似乎只被尊重;当我画到双缓冲图像时,它只是标准AA;但当我绕过双缓冲区图像时,子像素AA就完成了。

I think I have pinned this down; the subpixel AA hints appear to only be respected when painting directly to the AWT graphics context; when I paint to a double-buffer image it just does standard AA; but when I bypass the double-buffer image the subpixel AA is done.

否则The_Fire的答案可以在有可用Swing的JVM中运行(但不是J2ME JVM);请注意,The_Fire的答案不能使用AWT组件(使用新的Label()而不是新的JLabel()失败),可能是因为在将组件实现到显示之前无法提取FontRenderContext。

Otherwise The_Fire's answer would work in JVMs which have Swing available (but not J2ME JVMs); Note that The_Fire's answer does not work using an AWT component (using new Label() instead of new JLabel() fails), presumably because the FontRenderContext cannot be extracted until the component is realized to the display.

我目前获取目标图片图形上下文的代码如下所示:

My current code to get the graphics context for my target image currently looks like this:

try {
    if((dbImage=dctRoot.createImage(wid,hgt,1))!=null) {            // if createImage returns null or throws an exception the component is not yet displayable
        dbGraphics=(Graphics2D)dbImage.getGraphics();
        if(dctRoot.properties.getBoolean("Antialias",true)) {
            try {
                // set AA on overall
                dbGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING     ,RenderingHints.VALUE_ANTIALIAS_ON);
                // set text AA to platform/impl default
                dbGraphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT);
                // try to override platform/impl AA with font-specified AA (Java 6+)
                try { dbGraphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.class.getField("VALUE_TEXT_ANTIALIAS_GASP").get(null)); } catch(Throwable thr) {;} // yes, ignore exception
                }
            catch(Throwable thr) {
                dctRoot.log.println("Antialiasing not supported on this JVM ("+thr+").");
                dctRoot.setProperty("Antialias","False");           // turn off AA for subsequent painting
                }
            }
        }
    }
catch(Throwable thr) {
    dbImage=null;
    dbGraphics=null;
    }

创建图像的代码使用底层AWT组件,形成背景在我做我的所有绘画 - 组件是一个Panel,因为我需要能够做一个setFocusCycleRoot,因此它可以很好地与其他AWT组件一起使用。创建图像代码如下:

The code to create the image uses an underlying AWT component, which forms the backdrop on which I do all my painting - the component is a Panel, because I need to be able to do a setFocusCycleRoot so it plays well with other AWT components. The create image code follows:

public DctImage createImage(int wid, int hgt, float accpty) {
    GraphicsConfiguration               cfg=awtComponent.getGraphicsConfiguration();
    Image                               img=null;

    if(transparentImages) {
        //y { img=new BufferedImage(wid,hgt,BufferedImage.TYPE_INT_ARGB); }     // NB: J2ME CDC/PP 1.1 does not have the BufferedImage constructors (one day I may discover a way to create a BufferedImage via another API!!)
        try { img=cfg.createCompatibleImage(wid,hgt,Transparency.TRANSLUCENT); }// NB: J2ME CDC/PP 1.1 does not have this API, but prefer to use GraphicsConfiguration over new BufferImage(...)
        catch(NoClassDefFoundError   thr) { transparentImages=false; createImage(wid,hgt,accpty); } // try again with transparency disabled
        catch(NoSuchMethodError      thr) { transparentImages=false; createImage(wid,hgt,accpty); } // try again with transparency disabled
        catch(NoSuchFieldError       thr) { transparentImages=false; createImage(wid,hgt,accpty); } // try again with transparency disabled
        }
    else {
        img=cfg.createCompatibleImage(wid,hgt);
        }

    if(accpty>0 && SET_ACCELERATION_PRIORITY!=null) {
        try { SET_ACCELERATION_PRIORITY.invoke(img,new Object[]{new Float(accpty)}); } catch(Throwable thr) {;}
        }

    return (img==null ? null : new DctImage(img));
    }


推荐答案

我发现有一些因素在这里发生。

I found there were a few factors going on here.

首先,需要从底层AWT组件创建图像,并且必须在没有透明度的情况下创建图像:

First, the image needs to be created from the underlying AWT component, and it must be created without transparency:

cfg.createCompatibleImage(wid,hgt);

而不是

cfg.createCompatibleImage(wid,hgt,Transparency.TRANSLUCENT);

其次,由于一些莫名其妙的原因,主AA设置KEY_ANTIALIASING必须关闭才能使用LCD子像素AA。

Second, for some inexplicable reason, the primary AA setting, KEY_ANTIALIASING, must be off to use LCD subpixel AA.

最后,最重要的是,桌面字体呈现提示可以使用以下方式轻松检索:

Lastly, and most importantly, the desktop font rendering hints are easily retrieved using:

java.awt.Toolkit.getDesktopProperty("awt.font.desktophints")



< hr>

更新2010-01-05

在Java 6.26中重新测试,它似乎是需要将常规AA设置为渲染文本AA的问题终于得到了解决(通过Oracle的方式......太阳只是<罢工>几年十年太晚了)。

Retesting in Java 6.26, it seems like the problem with needing to set general AA off to render text AA has finally been resolved (way to go Oracle... after Sun was just a few years decade too late).

这篇关于如何继承系统的反别名设置,以便将文本绘制为像swing一样的屏幕外图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆