/* * [AntiAliasedJTextArea.java] * * Summary: Like JTextArea, but allows you to control whether fonts are anti-aliased. * * Copyright: (c) 2005-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 2.8 2009-01-23 add Kanji digits to font samples */ package com.mindprod.fontshower; import javax.swing.JTextArea; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import static java.lang.System.*; /** * Like JTextArea, but allows you to control whether fonts are anti-aliased. * * @author Roedy Green, Canadian Mind Products * @version 2.8 2009-01-23 add Kanji digits to font samples * @since 2005 */ public final class AntiAliasedJTextArea extends JTextArea { /** * true if want extra debugging output */ private static final boolean DEBUGGING = false; /** * true if want anti-aliased fonts */ private boolean antialias = true; /** * no arg constructor */ @SuppressWarnings( { "UnusedDeclaration" } ) public AntiAliasedJTextArea() { super(); } /** * constructor * * @param text what to display */ public AntiAliasedJTextArea( String text ) { super( text ); } /** * called whenever system has a slice to render * * @param g Graphics defining where and region to paint. */ protected void paintComponent( Graphics g ) { Graphics2D g2d = ( Graphics2D ) g; if ( antialias ) { g2d.setRenderingHint( RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON ); g2d.setRenderingHint( RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY ); // if wanted to smooth geometric shapes too // g2d.setRenderingHint( RenderingHints.KEY_ANTIALIASING, // RenderingHints.VALUE_ANTIALIAS_ON ); } if ( DEBUGGING ) { out.println( "AntiAliasedJTextArea.paintComponent called with antialias:" + antialias + " " + g2d.getClipBounds() ); } super.paintComponent( g2d ); } /** * Control whether fonts are antialiased. Takes extra time, but makes smoother edges. * * @param antialias true if want anti-aliasing. */ public void setAntiAlias( boolean antialias ) { this.antialias = antialias; this.repaint(); } }