Java Applet:浏览器中没有抗锯齿字体(但在AppletViewer中)

时间:2011-03-18 15:09:05

标签: java fonts applet antialiasing appletviewer

在AppletViewer中,我的Applet看起来像这样: screenshot AppletViewer

在浏览器中,我的Applet如下所示: screenshot browser

如您所见,字体不是抗锯齿的。背景颜色也不同。并且所有文本都在右侧切割。

那可能是什么?

您也可以自己尝试here


here我尝试使用此代码:

System.setProperty("awt.useSystemAAFontSettings","on");
System.setProperty("swing.aatext", "true");

但这只会导致此异常:

java.security.AccessControlException: access denied (java.util.PropertyPermission awt.useSystemAAFontSettings write)
    at java.security.AccessControlContext.checkPermission(AccessControlContext.java:374)
    at java.security.AccessController.checkPermission(AccessController.java:546)
    at java.lang.SecurityManager.checkPermission(SecurityManager.java:532)
    at java.lang.System.setProperty(System.java:742)
    at applets.Termumformungen$in$der$Technik_08_Ethanolloesungen.Applet.init(Applet.java:51)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Plugin2Manager.java:1640)
    at java.lang.Thread.run(Thread.java:680)
Exception: java.security.AccessControlException: access denied (java.util.PropertyPermission awt.useSystemAAFontSettings write)

1 个答案:

答案 0 :(得分:2)

它应该通过覆盖你想要消除锯齿的每个组件的paint方法来工作:

static void activateAntiAliasing(Graphics g) {
    try {
        Graphics2D g2d = (Graphics2D)g;

        // for antialiasing geometric shapes
        g2d.setRenderingHint( RenderingHints.KEY_ANTIALIASING,
                              RenderingHints.VALUE_ANTIALIAS_ON );

        // for antialiasing text
        g2d.setRenderingHint( RenderingHints.KEY_TEXT_ANTIALIASING,
                              RenderingHints.VALUE_TEXT_ANTIALIAS_ON );

        // to go for quality over speed
        g2d.setRenderingHint( RenderingHints.KEY_RENDERING,
                              RenderingHints.VALUE_RENDER_QUALITY );
    }
    catch(ClassCastException ignored) {}
}

@Override public void paint(final Graphics g) {
    activateAntiAliasing(g);
    super.paint(g);
}