The Graphics2D object was not present in Java 1.1 so if you *have* to make an applet or AWT program support Java 1.1 (i.e. not import any 1.2+ classes) but you still want to use advanced featured such as anti-aliasing when the user has a 1.2+ JRE then here is how to detect and enable those features using reflection:
public static void enableAntialiasing(Graphics g, boolean shapeAntiAlias, boolean textAntiAlias)
{
try
{
String nameGraphics2D = "java.awt.Graphics2D";
String nameRenderingHints = "java.awt.RenderingHints";
String nameRenderingHintsKey = "java.awt.RenderingHints$Key";
Class classGraphics2D = null;
try
{
classGraphics2D = Class.forName(nameGraphics2D);
}
catch (ClassNotFoundException e)
{
}
catch (NoClassDefFoundError e)
{
}
if (classGraphics2D != null)
{
System.out.println(nameGraphics2D + " is available");
Class classRenderingHints = null;
try
{
classRenderingHints = Class.forName(nameRenderingHints);
}
catch (ClassNotFoundException e)
{
}
catch (NoClassDefFoundError e)
{
}
if (classRenderingHints != null)
{
System.out.println(nameRenderingHints + " is available");
Class classRenderingHintsKey = null;
try
{
classRenderingHintsKey = Class.forName(nameRenderingHintsKey);
}
catch (ClassNotFoundException e)
{
}
catch (NoClassDefFoundError e)
{
}
if (classRenderingHintsKey != null)
{
System.out.println(nameRenderingHintsKey + " is available");
if (shapeAntiAlias)
{
Field rhKeyAntiAliasing = classRenderingHints.getField("KEY_ANTIALIASING");
Field rhValueAntiAliasingOn = classRenderingHints.getField("VALUE_ANTIALIAS_ON");
Method method = classGraphics2D.getMethod("setRenderingHint", new Class[] { classRenderingHintsKey, Object.class });
method.invoke(g, new Object[] { rhKeyAntiAliasing.get(null), rhValueAntiAliasingOn.get(null) });
}
if (textAntiAlias)
{
Field rhKeyTextAntiAliasing = classRenderingHints.getField("KEY_TEXT_ANTIALIASING");
Field rhValueTextAntiAliasingOn = classRenderingHints.getField("VALUE_TEXT_ANTIALIAS_ON");
Method method = classGraphics2D.getMethod("setRenderingHint", new Class[] { classRenderingHintsKey, Object.class });
method.invoke(g, new Object[] { rhKeyTextAntiAliasing.get(null), rhValueTextAntiAliasingOn.get(null) });
}
}
}
}
}
catch (Exception e)
{
System.out.println("Could not set antialiasing");
e.printStackTrace();
}
}
{
try
{
String nameGraphics2D = "java.awt.Graphics2D";
String nameRenderingHints = "java.awt.RenderingHints";
String nameRenderingHintsKey = "java.awt.RenderingHints$Key";
Class classGraphics2D = null;
try
{
classGraphics2D = Class.forName(nameGraphics2D);
}
catch (ClassNotFoundException e)
{
}
catch (NoClassDefFoundError e)
{
}
if (classGraphics2D != null)
{
System.out.println(nameGraphics2D + " is available");
Class classRenderingHints = null;
try
{
classRenderingHints = Class.forName(nameRenderingHints);
}
catch (ClassNotFoundException e)
{
}
catch (NoClassDefFoundError e)
{
}
if (classRenderingHints != null)
{
System.out.println(nameRenderingHints + " is available");
Class classRenderingHintsKey = null;
try
{
classRenderingHintsKey = Class.forName(nameRenderingHintsKey);
}
catch (ClassNotFoundException e)
{
}
catch (NoClassDefFoundError e)
{
}
if (classRenderingHintsKey != null)
{
System.out.println(nameRenderingHintsKey + " is available");
if (shapeAntiAlias)
{
Field rhKeyAntiAliasing = classRenderingHints.getField("KEY_ANTIALIASING");
Field rhValueAntiAliasingOn = classRenderingHints.getField("VALUE_ANTIALIAS_ON");
Method method = classGraphics2D.getMethod("setRenderingHint", new Class[] { classRenderingHintsKey, Object.class });
method.invoke(g, new Object[] { rhKeyAntiAliasing.get(null), rhValueAntiAliasingOn.get(null) });
}
if (textAntiAlias)
{
Field rhKeyTextAntiAliasing = classRenderingHints.getField("KEY_TEXT_ANTIALIASING");
Field rhValueTextAntiAliasingOn = classRenderingHints.getField("VALUE_TEXT_ANTIALIAS_ON");
Method method = classGraphics2D.getMethod("setRenderingHint", new Class[] { classRenderingHintsKey, Object.class });
method.invoke(g, new Object[] { rhKeyTextAntiAliasing.get(null), rhValueTextAntiAliasingOn.get(null) });
}
}
}
}
}
catch (Exception e)
{
System.out.println("Could not set antialiasing");
e.printStackTrace();
}
}
Hi Chris, Thank you very much for the code to enable anti-aliasing in Java 1.1. Just what I needed and saved me a lot of time. Best regards, Malcolm