How can I make my JLabel text using a special font, smooth?

I am trying to create a SWING application using Java 1.6, and I have a JLabel that uses its own font from a .ttf file.

I thought 1.6 had default anti-aliasing, but my text is pretty pixelated.

Here is an example code and an image showing the result:

package aceprobowler.test; import java.awt.Color; import java.awt.Font; import java.io.InputStream; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.SwingUtilities; import aceprobowler.options.OptionsValues; public class TestAntialiasedText extends JFrame { private static final long serialVersionUID = 2411330284507353990L; public TestAntialiasedText(String title) { super(title); setSize(800,200); Font titleFont = null; try { InputStream is = OptionsValues.class.getResourceAsStream("fonts//KOMIKAX_.ttf"); titleFont = Font.createFont(Font.TRUETYPE_FONT, is).deriveFont(Font.PLAIN, 60); } catch (Exception ex) { ex.printStackTrace(); System.err.println("font not loaded. Using serif font."); titleFont = new Font("serif", Font.PLAIN, 24); } JPanel panelWithText = new JPanel(); JLabel labelWithText = new JLabel("This is a test"); labelWithText.setFont(titleFont); labelWithText.setBackground(Color.BLACK); labelWithText.setForeground(Color.WHITE); labelWithText.setOpaque(true); panelWithText.add(labelWithText); add(panelWithText); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { new TestAntialiasedText("Testing text anti-alias").setVisible(true); } }); } } 

Mostly visible on "T" and on "A", Not anti-aliased text

I tried to create an inner class overriding paintComponent (Graphics g) and using

 Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON) 

But that will not work. Can anyone help me with this? I can not find any information on the Internet about this, since Javaj 1.6 has to do by default everything related to SWING Anti-aliased.

Thanks in advance!

+4
source share
3 answers

When you override the paintComponent method of a paintComponent instance, I believe that you will need to use the following:

Example:

+3
source

I believe that the default for font smoothing is platform dependent. You do not say what platform you are on. In any case, setting the text smoothing rendering tooltip should fix it.

+3
source

Another approach is to use TextLayout and put the FontRenderContext that isAntiAliased , as shown here .

+3
source

All Articles