Rinat Tainov
Rinat Tainov

Reputation: 1489

How to get Java g.drawString() high resolution?

I have background templates where java program must write some dynamic texts,

BufferedImage image = ImageIO.read(new File("background.jpg"));
Graphics g = image.createGraphics();

g.setFont(new Font("DejaVu Sans",Font.PLAIN,18));
g.drawString("Hello,World!",10,10);

When writing in such manner, I have some resolution problems around text that Java wrote.

How to write high resolution text on image by Java?

UPDATE: Here example with anti-aliasing. enter image description here

g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

Upvotes: 3

Views: 3197

Answers (1)

Paŭlo Ebermann
Paŭlo Ebermann

Reputation: 74810

The problem is not the drawing of the text, but the subsequent saving of the file: If you are using JPEG-compression, you will get compression artifacts, specially around sharp corners like your text.

There is no real way around this, apart from not using JPG.

Upvotes: 3

Related Questions