how do I change the Graphics Font

I want to use in Java one of the following MS XP fonts:

Tahoma, Verdana, or MS Reference Sans Serif

because they distinguish among the Il|170O.

When I use the line referencing Graphics instance g:

System.out.println(g.getFont().getFontName());

in a program, it prints 揇ialog.plain?

Then when I use the line:

g.setFont(Font.getFont("Tahoma"));//get new font

the Font stays the same.

How can I change to another font?

Alternatively, How can I have my program get a list of

fonts already associated with or available to Java?

Or even get a list without using a java program?

[708 byte] By [hillmia] at [2007-11-27 4:36:52]
# 1

> I want to use in Java one of the following MS XP

> fonts:

> Tahoma, Verdana, or MS Reference Sans Serif

> because they distinguish among the Il|170O.

> When I use the line referencing Graphics instance g:

> System.out.println(g.getFont().getFontName());[/

> code]

> in a program, it prints 揇ialog.plain?

> Then when I use the line:

> [code]g.setFont(Font.getFont("Tahoma")); //get new

> font

> the Font stays the same.

> How can I change to another font?

> Alternatively, How can I have my program get a list

> of

> fonts already associated with or available to Java?

String[] allFontNames = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();

Niceguy1a at 2007-7-12 9:47:03 > top of Java-index,Security,Cryptography...
# 2

I did the list you recommended and found all three Fonts in it.

Then the following code still says it is Dialog.plain.

Must be something wrong with my code.

g.setFont(Font.getFont("Tahoma")); //get new font

g.setFont(g.getFont().deriveFont(36f)); //change size of font in points

String grafStrg=g.getFont().getFontName()+" "+"Il|170O";

System.out.println(grafStrg);

g.drawString(grafStrg,xmx/2,ymx/30);

hillmia at 2007-7-12 9:47:03 > top of Java-index,Security,Cryptography...
# 3

> I did the list you recommended and found all three

> Fonts in it.

> Then the following code still says it is

> Dialog.plain.

> Must be something wrong with my code.

> g.setFont(Font.getFont("Tahoma")); //get new font

From the JavaDocs for getFont():

=================

Returns a Font object from the system properties list. nm is treated as the name of a system property to be obtained. The String value of this property is then interpreted as a Font object according to the specification of Font.decode(String) If the specified property is not found, or the executing code does not have permission to read the property, null is returned instead.

====================

The default system properties do not contain any font information. You can use the decode() method directly, or use the getFont() method that accepts a Map of attributes.Here's an example:

====================

import java.awt.*;

import java.awt.event.*;

import java.awt.font.*;

import javax.swing.*;

import java.util.*;

public class FontExample {

private final String[] allFontNames = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();

private final Map fontAttributes = new HashMap();

private JLabel fontLabel;

private int currentIndex = -1;

FontExample() {

fontAttributes.put(TextAttribute.SIZE, 36);

fontAttributes.put(TextAttribute.POSTURE, TextAttribute.POSTURE_REGULAR);

JFrame f = new JFrame();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JButton nextButton = new JButton("NextFont");

fontLabel = new JLabel("", JLabel.CENTER);

nextFont();

nextButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent ae) {

nextFont();

}

});

f.add(fontLabel,BorderLayout.CENTER);

f.add(nextButton,BorderLayout.SOUTH);

f.setSize(400,300);

f.setVisible(true);

}

public static void main (String[] args) {

new FontExample();

}

private void nextFont() {

currentIndex++;

if (currentIndex >= allFontNames.length) {

currentIndex = 0;

}

fontAttributes.put(TextAttribute.FAMILY, allFontNames[currentIndex]);

//Get font using attributes

Font nextFont = Font.getFont(fontAttributes);

//Or, get the font by calling decode() directly

//Font nextFont = Font.decode(allFontNames[currentIndex]+"-PLAIN-36");

fontLabel.setFont(nextFont);

fontLabel.setText(nextFont.getFontName());

}

}

Jim S.

Niceguy1a at 2007-7-12 9:47:03 > top of Java-index,Security,Cryptography...
# 4

Thanks, your program works. I will study it.

Seems a bit more complicated than I would have expected.

But that's java.

I discovered that the following clip also works in my program,

But it is not yet written well.

Font[] allFonts=GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();

for(int i=0;i<allFonts.length;i++)

if(allFonts[i].getFontName().compareTo("Verdana Italic")==0) { g.setFont(allFonts[i]); break; }

g.setFont(g.getFont().deriveFont(36f)); //change size of font in points

String grafStrg=g.getFont().getFontName()+" "+"Il|170O";

System.out.println(grafStrg); //doesn抰 use the new Font, of course

g.drawString(grafStrg,xmx/2,ymx/30); //uses the new Font OK

>

hillmia at 2007-7-12 9:47:03 > top of Java-index,Security,Cryptography...
# 5

Thanks again for FontExample. I changed the JLabel to JTextArea and formed a string of printable characters with 3 newlines and got a nice view of the Fonts. Too bad one has to form the big array of names. Is there a way to get just the one or very few Fonts I want for a program without this? It seems that graphics.getFont(String) should have done it but doesn't work.

hillmia at 2007-7-12 9:47:03 > top of Java-index,Security,Cryptography...
# 6

> Thanks again for FontExample. I changed the JLabel to

> JTextArea and formed a string of printable characters

> with 3 newlines and got a nice view of the Fonts. Too

> bad one has to form the big array of names. Is there

> a way to get just the one or very few Fonts I want

> for a program without this? It seems that

> graphics.getFont(String) should have done it but

> doesn't work.

Use Font.decode()

Font theFontIWant = Font.decode("Verdana"+"-PLAIN-36");

Niceguy1a at 2007-7-12 9:47:03 > top of Java-index,Security,Cryptography...
# 7
Thanks, looks like it's the answer, but I haven't tried it yet.
hillmia at 2007-7-12 9:47:03 > top of Java-index,Security,Cryptography...