Skip to content

Instantly share code, notes, and snippets.

@dancefire
Created January 30, 2014 13:45
Show Gist options
  • Save dancefire/8708590 to your computer and use it in GitHub Desktop.
Save dancefire/8708590 to your computer and use it in GitHub Desktop.
// Modified From: http://www.leepoint.net/notes-java/GUI-appearance/fonts/22fontdemo.html
// File : gui-appearance/font/fontdemo/FontDemo.java
// Purpose: Demo of how to work with fonts. This class is primarily
// user interface. The accomanying FontPanel class does the drawing.
// Author : Fred Swartz - 28 Sep 2006 - Placed in public domain.
package fontdemo;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
/////////////////////////////////////////////////////////////////////// FontDemo
class FontDemo extends JFrame {
//=================================================================== fields
private FontPanel _displayArea;// Where the sample text is displayed.
//... The following controls are instance variables because they are
// referenced at "runtime" in the listeners. Alternate solutions:
// * Declare them as *final* local variables.
// * Obtain them from the Event object passed to the listeners, which
// is a good solution when a listener is shared (but not here).
private JSlider _sizeSlider; // Sets the font size.
private JComboBox _fontCombo; // For selecting one of the installed fonts.
private JCheckBox _antialiasedCB; // To draw with antiliasing on/off.
//================================================================ constants
private static final int INITIAL_SIZE = 18;
private static final int INITIAL_STYLE = Font.PLAIN;
private static final String INITIAL_STYLENAME = "Font.PLAIN";
private static final String INITIAL_FONTNAME = "Monospaced";
//============================================================== constructor
public FontDemo() {
//... Create a FontPanel to display the fonts.
_displayArea = new FontPanel(INITIAL_FONTNAME, INITIAL_STYLE,
INITIAL_SIZE, false);
//... Get all font families
String[] fontNames = GraphicsEnvironment
.getLocalGraphicsEnvironment()
.getAvailableFontFamilyNames();
//... Make vector of all fonts that can display basic chars.
// Vector (not newer ArrayList) is used by JComboBox.
Vector<String> visFonts = new Vector<String>(fontNames.length);
for (String fontName : fontNames) {
Font f = new Font(fontName, Font.PLAIN, 12);
if (f.canDisplay('a')) {
//... Display only fonts that have the alphabetic characters.
visFonts.add(fontName);
} else {
// On my machine there are almost 20 fonts (eg, Wingdings)
// that don't display text.
//System.out.println("No alphabetics in " + fontName);
}
}
_fontCombo = new JComboBox(visFonts); // JComboBox of fonts
_fontCombo.setSelectedItem(INITIAL_FONTNAME); // Select initial font
_fontCombo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
_displayArea.setFontName((String)_fontCombo.getSelectedItem());
}
});
//... Style: Create radio buttons for the 4 style options.
JRadioButton stylePlainRB = new JRadioButton("Font.PLAIN", true);
JRadioButton styleItalicRB = new JRadioButton("Font.ITALIC", false);
JRadioButton styleBoldRB = new JRadioButton("Font.BOLD", false);
JRadioButton styleItalicBoldRB = new JRadioButton("Font.ITALIC+Font.BOLD"
, false);
//... Add the buttons to a button group
ButtonGroup styleGroup = new ButtonGroup();
styleGroup.add(stylePlainRB);
styleGroup.add(styleItalicRB);
styleGroup.add(styleBoldRB);
styleGroup.add(styleItalicBoldRB);
//... Add listeners.
stylePlainRB.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
_displayArea.setFontStyle(Font.PLAIN);
}
});
styleItalicRB.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
_displayArea.setFontStyle(Font.ITALIC);
}
});
styleBoldRB.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
_displayArea.setFontStyle(Font.BOLD);
}
});
styleItalicBoldRB.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
_displayArea.setFontStyle(Font.ITALIC + Font.BOLD);
}
});
//... Antialiasing checkbox
_antialiasedCB = new JCheckBox("Antialiased");
_antialiasedCB.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
_displayArea.setAntialiasing(_antialiasedCB.isSelected());
}
});
//... Create a slider for setting the size value
_sizeSlider = new JSlider(JSlider.HORIZONTAL, 5, 60, INITIAL_SIZE);
_sizeSlider.setMajorTickSpacing(10); // sets numbers for big tick marks
_sizeSlider.setMinorTickSpacing(1); // smaller tick marks
_sizeSlider.setPaintTicks(true); // display the ticks
_sizeSlider.setPaintLabels(true); // show the numbers
_sizeSlider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
_displayArea.setFontSize(_sizeSlider.getValue());
}
});
//... Controls arranged in BoxLayout.
// This is pretty ugly, but I didn't feel like struggling with
// GridBagLayout, and didn't want to use any of the superior
// external layout managers in this program.
JPanel controls = new JPanel();
controls.setLayout(new BoxLayout(controls, BoxLayout.Y_AXIS));
//... Add components to controls panel.
addToBox(controls, Component.LEFT_ALIGNMENT
, new JLabel("Font:"), _fontCombo
, new JLabel("Size:"), _sizeSlider
, new JLabel("Style:"), stylePlainRB, styleItalicRB
, styleBoldRB, styleItalicBoldRB
, _antialiasedCB
);
//... Put display+controls in the content pane.
JPanel content = new JPanel();
content.setLayout(new BorderLayout(5, 5));
content.add(_displayArea, BorderLayout.CENTER);
content.add(controls, BorderLayout.WEST);
content.setBorder(new EmptyBorder(12, 12, 12, 12));
//... Set window characteristics
setContentPane(content);
setTitle("Font Demo");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
}
//================================================================= addToBox
// Utility method to add elements to a BoxLayout container.
private void addToBox(Container cont, float align, JComponent... comps ) {
for (JComponent comp : comps) {
comp.setAlignmentX(align);
cont.add(comp);
}
}
//===================================================================== main
public static void main(String[] args) {
JFrame myWindow = new FontDemo();
myWindow.setVisible(true);
}
//////////////////////////////////////////////////////////////// FontPanel class
static class FontPanel extends JPanel {
private String _fontName;
private int _fontStyle;
private int _fontSize;
private boolean _antialiased;
//============================================================== constructor
public FontPanel(String font, int style, int size, boolean antialiased) {
this.setPreferredSize(new Dimension(400, 100));
this.setBackground(Color.white);
this.setForeground(Color.black);
_fontName = font;
_fontStyle = style;
_fontSize = size;
_antialiased = antialiased;
}
//================================================= @Override paintComponent
@Override public void paintComponent(Graphics g) {
super.paintComponent(g); // Paint background.
Graphics2D g2 = (Graphics2D)g; // Graphics2 for antialiasing.
String text = "Font(\""
+ _fontName + "\", "
+ fontStyleCodeToFontStyleString(_fontStyle) + ", "
+ _fontSize + ");\n"
+ "Chinese: 中文汉字,\n"
+ "Japanese: 日本語のファイル名,\n"
+ "Korean: 한국어 파일 이름."
;
Font f = new Font(_fontName, _fontStyle, _fontSize);
g2.setFont(f);
if (_antialiased) {
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
}
//... Find the size of this text so we can center it.
//FontMetrics fm = g2.getFontMetrics(f); // metrics for this object
//Rectangle2D rect = fm.getStringBounds(text, g2); // size of string
//int textHeight = (int)(rect.getHeight());
//int textWidth = (int)(rect.getWidth());
//... Center text horizontally and vertically
int x = 20;//(this.getWidth() - textWidth) / 2;
int y = 20;//(this.getHeight() - textHeight) / 2 + fm.getAscent();
for (String line: text.split("\n")) {
g2.drawString(line, x, y += g.getFontMetrics().getHeight());
}
}
//================================================================== SETTERS
public void setFontName(String fn) { _fontName = fn; this.repaint();}
public void setFontSize(int size) { _fontSize = size; this.repaint();}
public void setFontStyle(int style) {_fontStyle = style; this.repaint();}
public void setAntialiasing(boolean antialiased) {
_antialiased = antialiased;
this.repaint();
}
//=========================================== fontStyleCodeToFontStyleString
// Utility method for converting font codes to name.
public static String fontStyleCodeToFontStyleString(int styleCode) {
String styleName;
switch (styleCode) {
case Font.PLAIN: styleName = "Font.PLAIN"; break;
case Font.ITALIC: styleName = "Font.ITALIC"; break;
case Font.BOLD: styleName = "Font.BOLD"; break;
case Font.ITALIC+Font.BOLD: styleName = "ITALIC+Font.BOLD"; break;
default: throw new IllegalArgumentException(
"fontStyleCodeToFontStyleString: Unknown font code: " +
styleCode);
}
return styleName;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment