Java Applet:浏览器中没有抗锯齿字体(但在 AppletViewer 中) [英] Java Applet: no antialiased font in browser (but in AppletViewer)

查看:28
本文介绍了Java Applet:浏览器中没有抗锯齿字体(但在 AppletViewer 中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 AppletViewer 中,我的 Applet 如下所示:

In the AppletViewer, my Applet looks like this:

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

In the browser, my Applet looks like this:

如您所见,字体没有抗锯齿.背景颜色也不同.并且所有文本都在右侧被剪切.

As you can see, the font is not antialiased. Also the background color is different. And all the text is cutted on the right side.

那可能是什么?

你也可以自己试试 这里.

来自这里 我尝试使用此代码:

From here I tried to use this code:

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)

推荐答案

它应该通过为每个想要抗锯齿的组件重写 paint 方法来工作:

It should work by overriding the paint method like this for each component where you want to have anti-aliasing:

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);
}

这篇关于Java Applet:浏览器中没有抗锯齿字体(但在 AppletViewer 中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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