Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using WingDings as a font in an applet

Status
Not open for further replies.

louisgnarf

Programmer
Jul 11, 2002
14
US
All I want to do is have a JLabel be in WingDings...what's the easiest way to do it? I tried creating a Font with "WingDings" as a parameter, but it doesn't work.

 
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.lang.*;
// We extend the basic JApplet, which itself extends
// the Java Applet class, which provides the basic
// init(), start(), stop(), and destroy() life-cycle
// management methods.
public class LabelTest extends JApplet {
// Don't panic! This is just a hack required because
// the real world hasn't caught up with Sun's Java2
// specs yet. It tweaks a "system" parameter so that
// a check that always fails and generates an error
// message won't happen.
public LabelTest() {
getRootPane().putClientProperty("defeatSystemEventQueueCheck",
Boolean.TRUE);
}

// Here's the real code. We are overriding Applet's
// init() class, which is inherited through JApplet.
// All we do is create a simple label, tell it to center
// the display text we provided through the constructor,
// and then add the label to the Applet's display area.
public void init() {
JLabel label = new JLabel("A Swing label");
label.setHorizontalAlignment(JLabel.CENTER);
getContentPane().add(label);
/* deprecated
String[] fontNames = Toolkit.getDefaultToolkit().getFontList();
System.out.println(fontNames.length);
for (int i=0;i<fontNames.length;i++)
System.out.println(fontNames);
*/
GraphicsEnvironment gE = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fontNames2 = gE.getAvailableFontFamilyNames();
int count = 0;
count = fontNames2.length;
for (int i=0;i<fontNames2.length;i++)
//System.out.println(fontNames2);
label.setFont(new Font(&quot;MS Gothic&quot;, Font.PLAIN, 12));
//label.setFont(new Font(&quot;Wingdings&quot;, Font.PLAIN, 12));
}
}

 
I can change to other font except wingdings.
The following example is from java.sun.com. Its applet alsp fails to show wingdings by drawing graphics.

/*
* @(#)FontSelection.java 1.2 98/07/31
*/


import java.lang.Integer;
import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Vector;

/*
* This applet displays a String with the user's selected
* fontname, style and size attributes.
*/

public class FontSelection extends JApplet implements ItemListener {
JLabel fontLabel, sizeLabel, styleLabel;
FontPanel fontC;
JComboBox fonts, sizes, styles;
int index = 0;
String fontchoice = &quot;fontchoice&quot;;
int stChoice = 0;
String siChoice = &quot;10&quot;;

public void init() {

getContentPane().setLayout( new BorderLayout() );

JPanel topPanel = new JPanel();
JPanel fontPanel = new JPanel();
JPanel sizePanel = new JPanel();
JPanel stylePanel = new JPanel();
JPanel sizeAndStylePanel = new JPanel();

topPanel.setLayout( new BorderLayout() );
fontPanel.setLayout( new GridLayout( 2, 1 ) );
sizePanel.setLayout( new GridLayout( 2, 1 ) );
stylePanel.setLayout( new GridLayout( 2, 1 ) );
sizeAndStylePanel.setLayout( new BorderLayout() );

topPanel.add( BorderLayout.WEST, fontPanel );
sizeAndStylePanel.add( BorderLayout.WEST, sizePanel );
sizeAndStylePanel.add( BorderLayout.CENTER, stylePanel );
topPanel.add( BorderLayout.CENTER, sizeAndStylePanel );

getContentPane().add( BorderLayout.NORTH, topPanel );

fontLabel = new JLabel();
fontLabel.setText(&quot;Fonts&quot;);
Font newFont = getFont().deriveFont(1);
fontLabel.setFont(newFont);
fontLabel.setHorizontalAlignment(JLabel.CENTER);
fontPanel.add(fontLabel);

sizeLabel = new JLabel();
sizeLabel.setText(&quot;Sizes&quot;);
sizeLabel.setFont(newFont);
sizeLabel.setHorizontalAlignment(JLabel.CENTER);
sizePanel.add(sizeLabel);

styleLabel = new JLabel();
styleLabel.setText(&quot;Styles&quot;);
styleLabel.setFont(newFont);
styleLabel.setHorizontalAlignment(JLabel.CENTER);
stylePanel.add(styleLabel);

GraphicsEnvironment gEnv = GraphicsEnvironment.getLocalGraphicsEnvironment();
String envfonts[] = gEnv.getAvailableFontFamilyNames();
Vector vector = new Vector();
for ( int i = 1; i < envfonts.length; i++ ) {
vector.addElement(envfonts);
}
fonts = new JComboBox( vector );
fonts.setMaximumRowCount( 9 );
fonts.addItemListener(this);
fontchoice = envfonts[0];
fontPanel.add(fonts);

sizes = new JComboBox( new Object[]{ &quot;10&quot;, &quot;12&quot;, &quot;14&quot;, &quot;16&quot;, &quot;18&quot;} );
sizes.setMaximumRowCount( 9 );
sizes.addItemListener(this);
sizePanel.add(sizes);

styles = new JComboBox( new Object[]{
&quot;PLAIN&quot;,
&quot;BOLD&quot;,
&quot;ITALIC&quot;,
&quot;BOLD & ITALIC&quot;} );
styles.setMaximumRowCount( 9 );
styles.addItemListener(this);
sizes.setMaximumRowCount( 9 );
stylePanel.add(styles);

fontC = new FontPanel();
fontC.setBackground(Color.white);
getContentPane().add( BorderLayout.CENTER, fontC);
}

/*
* Detects a state change in any of the Lists. Resets the variable corresponding
* to the selected item in a particular List. Invokes changeFont with the currently
* selected fontname, style and size attributes.
*/
public void itemStateChanged(ItemEvent e) {
if ( e.getStateChange() != ItemEvent.SELECTED ) {
return;
}

Object list = e.getSource();

if ( list == fonts ) {
fontchoice = (String)fonts.getSelectedItem();
} else if ( list == styles ) {
index = styles.getSelectedIndex();
stChoice = index;
} else {
siChoice = (String)sizes.getSelectedItem();
}
fontC.changeFont(fontchoice, stChoice, siChoice);
}

public static void main(String s[]) {
JFrame f = new JFrame(&quot;FontSelection&quot;);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
JApplet fontSelection = new FontSelection();
f.getContentPane().add(fontSelection, BorderLayout.CENTER);
fontSelection.init();
f.setSize(new Dimension(550,250));
f.setVisible(true);
}

}


class FontPanel extends JPanel {

Font thisFont;

public FontPanel(){
thisFont = new Font(&quot;Arial&quot;, Font.PLAIN, 10);
}

// Resets thisFont to the currently selected fontname, size and style attributes.
public void changeFont(String f, int st, String si){
Integer newSize = new Integer(si);
int size = newSize.intValue();
thisFont = new Font(f, st, size);
repaint();
}

public void paintComponent (Graphics g) {
super.paintComponent( g );
Graphics2D g2 = (Graphics2D) g;
int w = getWidth();
int h = getHeight();

g2.setColor(Color.darkGray);
g2.setFont(thisFont);
String change = &quot;Pick a font, size, and style to change me&quot;;
FontMetrics metrics = g2.getFontMetrics();
int width = metrics.stringWidth( change );
int height = metrics.getHeight();
g2.drawString( change, w/2-width/2, h/2-height/2 );
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top