1

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

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

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

那会是什么?

您也可以在这里自己尝试。


这里我尝试使用此代码:

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)
4

1 回答 1

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);
}
于 2011-03-21T20:37:49.907 回答