Mark Stephens

Mark Stephens has been working with Java and PDF since 1999 and has diversified into HTML5, SVG and JavaFX.

He also enjoys speaking at conferences and has been a Speaker at user groups, Business of Software, Seybold and JavaOne conferences. He has a very dry sense of humor and an MA in Medieval History for which he has not yet found a practical use.

Java and anti-aliasing

1 min read

Anti-aliasing is the processes of making the edges of lines on shapes or text less jagged. This is done by fooling the eye – intermediate pixels are added to smooth the color transition. So you might add some grey pixels between a black and a white point. It is very common in computer applications and games to make curves less jagged and text look crisper. Click here for some examples.

It is not something you generally need to worry about in Java because it is automatic. You draw a line and Java blends it into the display. What you can control in Java is whether it is used for drawing in Graphics2D. This is useful because while it is useful most of the time, on some occasions you do not want it enabled. If you have narrow sharp lines or you print low resolution images, it can make them look more blurry.

You control the Anti-aliasing by using Rendering Hints and applying them to the Graphics2D object you are drawing onto – this is nice because it means you can have different settings from Screen, image generation and printing. To create a RenderingHints, you use this code

hints =new RenderingHints(RenderingHints.KEY_RENDERING,
 RenderingHints.VALUE_RENDER_QUALITY);
hints.put(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
//hints.put(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_OFF);

You then enable it with

g2.setRenderingHints(hints);

If you would like to try it in our Java PDF library, we have added a call to allow users to control it in our printing routines. Here is how you can experiment with it. Can you see a difference?

/**
* print
*///before we do any print, allow user to alter hints
//customPrintHintingHandler implementes CustomPrintHintingHandler
//and passed in with PdfDecoder method
//addExternalHandler(MycustomPrintHintingHandlerImpementation,
Options.CustomPrintHintingHandler);

if (customPrintHintingHandler != null) {
   if(printRender != null) {
      printRender.stopG2HintSetting(true);
   }
   customPrintHintingHandler.preprint(g2,this);
}
Mark Stephens

Mark Stephens has been working with Java and PDF since 1999 and has diversified into HTML5, SVG and JavaFX.

He also enjoys speaking at conferences and has been a Speaker at user groups, Business of Software, Seybold and JavaOne conferences. He has a very dry sense of humor and an MA in Medieval History for which he has not yet found a practical use.

Leave a Reply

Your email address will not be published. Required fields are marked *