Note that there are some explanatory texts on larger screens.

plurals
  1. POCan Java render translucent text using sub-pixel AA?
    primarykey
    data
    text
    <p>I have found that while rendering opaque text in Java (latest version 6u23) uses sub-pixel AA just fine, rendering translucent text does not.</p> <p>Sub-pixel AA:</p> <p><img src="https://i.stack.imgur.com/ed1l4.gif" alt="alt text"> <img src="https://i.stack.imgur.com/oEDgp.gif" alt="alt text"></p> <p>The same text for which only the color has been changed from 0xFFFFFFFF to 0xBFFFFFFF:</p> <p><img src="https://i.stack.imgur.com/mfQky.gif" alt="alt text"> <img src="https://i.stack.imgur.com/F3rlJ.gif" alt="alt text"></p> <p>As you can see, the translucent text is clearly standard AA and rather than a clean translucent rendering, it has that awful '90s "spidery" appearance.</p> <p>Is this due to a technical limitation for sub-pixel AA in general, or a bug in Java, or just because Java doesn't even try for translucent text, or have I missed something?</p> <hr> <p><strong>Graphics Initialization</strong></p> <pre><code>dbGraphics=(Graphics2D)dbImage.getGraphics(); if(dctRoot.properties.getBoolean("Antialias",true)) { try { Map hnts=(Map)(dctRoot.awtComponent.getToolkit().getDesktopProperty("awt.font.desktophints")); // SET AA ON OVERALL (NOTE: GENERAL AA MUST BE OFF FOR SUBPIXEL AA TO BE HONORED - TEXT WIDGETS MUST DO THIS THEMSELVES) dbGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON); if(hnts!=null) { // SET FONT RENDERING HINTS FROM DESKTOP dbGraphics.addRenderingHints(hnts); } else { try { // SET TEXT AA TO FONT-SPECIFIED GASP AA (JAVA 6+) dbGraphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.class.getField("VALUE_TEXT_ANTIALIAS_GASP").get(null)); } catch(Throwable thr3) { // SET TEXT AA TO DEFAULT dbGraphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON); } } } catch(Throwable thr) { dctRoot.log.println("Antialiasing not supported on this JVM ("+thr+")."); dctRoot.setProperty("Antialias","False"); // turn off AA for subsequent painting } } else { try { dbGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF); dbGraphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_OFF); } catch(Throwable thr) {;} // ignore exception } </code></pre> <p><strong>Text Rendering</strong></p> <pre><code>Object oaa=disableGeneralAA(gc); ... gc.drawString(tl,xx,(ty+(xa*met.getHeight()))); restoreGeneralAA(gc,oaa); ... static private volatile boolean hasRenderingHints=true; // ***************************************************************************** // STATIC INIT &amp; MAIN // ***************************************************************************** // ***************************************************************************** // STATIC METHODS // ***************************************************************************** /** * Disable the general anti-aliasing rendering hint, returning whether the old value was RenderingHints.VALUE_ANTIALIAS_ON. * &lt;p&gt; * This method is needed for text rendering due to a bug in AWT; as of Java 6_20 when general AA is on text is not rendered using subpixel * AA, so general AA has to be turned off before rendering text and turned back on when done. This method abstracts that work and deals * with the possibility that the JVM does not support rendering hints, such as is the case with JME JVMs. */ static public Object disableGeneralAA(Graphics2D gc) { Object old=null; if(hasRenderingHints) { try { old=gc.getRenderingHint(RenderingHints.KEY_ANTIALIASING); gc.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF); } catch(NoClassDefFoundError thr) { hasRenderingHints=false; } catch(NoSuchFieldError thr) { hasRenderingHints=false; } catch(NoSuchMethodError thr) { hasRenderingHints=false; } } return old; } /** * Disable the general anti-aliasing rendering hint, returning whether the old value was RenderingHints.VALUE_ANTIALIAS_ON. * &lt;p&gt; * This method is needed for text rendering due to a bug in AWT; as of Java 6_20 when general AA is on text is not rendered using subpixel * AA, so general AA has to be turned off before rendering text and turned back on when done. This method abstracts that work and deals * with the possibility that the JVM does not support rendering hints, such as is the case with JME JVMs. */ static public void restoreGeneralAA(Graphics2D gc, Object val) { Object old=null; if(hasRenderingHints &amp;&amp; val!=null) { try { gc.setRenderingHint(RenderingHints.KEY_ANTIALIASING,val); } catch(NoClassDefFoundError thr) { hasRenderingHints=false; } catch(NoSuchFieldError thr) { hasRenderingHints=false; } catch(NoSuchMethodError thr) { hasRenderingHints=false; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload