Mark Stephens Mark has been working with Java and PDF since 1999 and is a big NetBeans fan. He enjoys speaking at conferences. He has an MA in Medieval History and a passion for reading.

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

Did you know...

IDRsolutions offers a whole range of online file converters to convert PDF and Microsoft Excel, Word and Office Documents to HTML5, SVG or image formats?

It is free to use for single file conversions and also includes Developer links if you want to use our commercial software for bulk conversions. Find out more on this page

Mark Stephens Mark has been working with Java and PDF since 1999 and is a big NetBeans fan. He enjoys speaking at conferences. He has an MA in Medieval History and a passion for reading.

How to convert WMF files to SVG in java…

This article will show you how to convert WMF files into SVG files using our JDeli Java Image library. What is WMF? WMF is...
Amy Pearson
1 min read

How to write WebP images in Java

In this article, I will walk you through how to write out images as WebP images in Java. ImageIO does not support WebP images...
Mark Stephens
1 min read

How to convert EMF files to SVG in Java…

This article shows you how to convert EMF files into SVG files using our JDeli Java Image library. What is EMF? EMF is a...
Mark Stephens
1 min read

Leave a Reply

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

IDRsolutions Ltd 2020. All rights reserved.