Java Reference
In-Depth Information
Example 7−1: UnicodeDisplay.java (continued)
else if (cmd.equals("Monospaced")) fontfamily = "Monospaced";
else if (cmd.equals("Plain")) fontstyle = Font.PLAIN;
else if (cmd.equals("Italic")) fontstyle = Font.ITALIC;
else if (cmd.equals("Bold")) fontstyle = Font.BOLD;
else if (cmd.equals("BoldItalic")) fontstyle = Font.BOLD + Font.ITALIC;
p.setFont(fontfamily, fontstyle);
}
/** A convenience method to create a Menu from an array of items */
private JMenu makemenu(String name, String[] itemnames,
ActionListener listener)
{
JMenu m = new JMenu(name);
for(int i = 0; i < itemnames.length; i++) {
JMenuItem item = new JMenuItem(itemnames[i]);
item.addActionListener(listener);
item.setActionCommand(itemnames[i]); // okay here, though
m.add(item);
}
return m;
}
/** The main() program just create a window, packs it, and shows it */
public static void main(String[] args) {
UnicodeDisplay f = new UnicodeDisplay("Unicode Displayer");
f.pack();
f.show();
}
/**
* This nested class is the one that displays one "page" of Unicode
* glyphs at a time. Each "page" is 256 characters, arranged into 16
* rows of 16 columns each.
**/
public static class UnicodePanel extends JComponent {
protected char base; // What character we start the display at
protected Font font = new Font("serif", Font.PLAIN, 18);
protected Font headingfont = new Font("monospaced", Font.BOLD, 18);
static final int lineheight = 25;
static final int charspacing = 20;
static final int x0 = 65;
static final int y0 = 40;
/** Specify where to begin displaying, and re-display */
public void setBase(char base) { this.base = base; repaint(); }
/** Set a new font name or style, and redisplay */
public void setFont(String family, int style) {
this.font = new Font(family, style, 18);
repaint();
}
/**
* The paintComponent() method actually draws the page of glyphs
**/
public void paintComponent(Graphics g) {
int start = (int)base & 0xFFF0; // Start on a 16-character boundary
Search WWH ::




Custom Search