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

发布于 2024-10-24 08:11:54 字数 1563 浏览 1 评论 0原文

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

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

如您所见,字体未进行抗锯齿处理。而且背景颜色也不一样。所有文本都被剪切在右侧。

那会是什么?

您也可以自己尝试此处


这里我尝试过使用此代码:

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)

In the AppletViewer, my Applet looks like this:
screenshot AppletViewer

In the browser, my Applet looks like this:
screenshot browser

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.

What could that be?

You can also try it yourself here.


From here I tried to use this code:

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

But that results only in this exception:

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

世界如花海般美丽 2024-10-31 08:11:54

它应该通过重写 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);
}

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文