/* * Copyright 2003 Smithvalley. All rights reserved. * * THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY * KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. */ package com.smithvalley.samples.demos; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; /** * This is a static class used for enabling anti-aliased text in a way * that works both in 1.1.4 Microsoft VM's and modern VM's. * * Make the call as follows: * AntiAliasEnabler.setAntiAlias(g, true); // to enable anti-aliasing * AntiAliasEnabler.setAntiAlias(g, false); // to disable anti-aliasing * * This call should be made somewhere in your paint() method, generally * the first line. * * If you make the call from more than one location in your code you * will need to bottleneck all your calls though one static method which * is in a class which is NOT this class. Here is an example: * * public MyMainClass { * * public static void setAntiAlias(Graphics g, boolean enable) { * AntiAliasEnabler.setAntiAlias(g, enable); * } * } * * Now you may use MyMainClass.setAntiAlias(g, true/false) from anywhere * in your applet or application to turn anti-aliasing on and off. If * you call AntiAliasEnabler.setAntiAlias() directly from more than one * location you will get a class not found exception after the first time * it is called. This is due to an anomaly in the JIT and how it handles * calls to alien classes. * * When you include this class you must have the Java2 libraries in your * classpath so this might not work for those of you using older IDE's. * I sugest using Eclipse becuase it makes it really easy to run your IDE * under a modern Java library and compile for 1.1.4. You simply go to * Preference -> Java -> Compiler and under "JDKCompilance" choose 1.3. * If the VM you are using is too new it may no longer support the * backwards compatibility feature. I use the IBM 1.3.1 JDK and it * compiles 1.1.4 code with no problems. * * @author Mike Smith (michael@smithvalley.com) */ public class AntiAliasEnabler { private static final String javaVersion = System.getProperty("java.version"); public static final float javaVersNum; static { float version = 0; try { version = Float.valueOf(javaVersion.substring(0, 3)).floatValue(); } catch (NumberFormatException e) { } javaVersNum = version; } private static boolean canAntiAlias = true; private AntiAliasEnabler() {} public static void setAntiAlias(Graphics g, boolean enable) { if (canAntiAlias) { if (javaVersNum < 1.2F) { canAntiAlias = false; } else { try { setRenderingHits(g, enable); } catch(Exception e) { canAntiAlias = false; } } } } private static void setRenderingHits(Graphics g, boolean enable) { Object o; if (enable) { o = RenderingHints.VALUE_ANTIALIAS_ON; } else { o = RenderingHints.VALUE_ANTIALIAS_OFF; } Graphics2D gg = (Graphics2D)g; gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING, o); } }