Java Applet: no antialiased font in browser (but in AppletViewer)

空扰寡人 提交于 2019-12-14 03:42:51

问题


In the AppletViewer, my Applet looks like this:

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.

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)

回答1:


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


来源:https://stackoverflow.com/questions/5353904/java-applet-no-antialiased-font-in-browser-but-in-appletviewer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!