Author Topic:   getFontList()
Vikram Deshmukh
greenhorn
posted May 09, 2000 01:20 AM             
Hi friends,

I have some problem with getFontList() function.

It returns list of strings .

But the basic problem is, its an abstract method, in the abstract class
Toolkit.(java.awt.Toolkit)

Since Toolkit is an abstract class, i cannot instantiate its object.
If i decides to override this method, by extending Toolkit,
i'll have to override galaxy of methods, in Toolkit. Even if it is possible with spending long haul, its not an elegant way.

So is there any other way, so i can use getFontList() method, or its functionality ?

Thanx in advance !

from,
vikram.

edward man
unregistered
posted May 09, 2000 10:15 PM           
getFontList is deprecated. The following program list all the fonts on your environment.

If you really want to use getFontList, uncomment those three lines.


// import java.awt.Toolkit;
import java.awt.GraphicsEnvironment;

class TestFont02 {
public static void main (String [] args)
{
// String fonts[] = Toolkit.getDefaultToolkit().getFontList();

// for (int i = 0; i < fonts.length; i++)
// System.out.println(fonts[i]);

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
String allFonts[] = ge.getAvailableFontFamilyNames();

for (int i = 0; i < allFonts.length; i++)
System.out.println(allFonts[i]);

System.exit(0);
}
}

maha anna
bartender
posted May 09, 2000 10:57 PM             
vikram,
YOu can get the Fonts list available to the local system through GraphicsEnvironment class. In fact GraphicsEnvironment class is also an abstract class as Toolkit. I am avoiding Toolkit because its method getFontList() is deprecated. But you can ask , then how can I instantiate the GraphicsEnvironment class ?.

From the Java source
-------------------------------


public abstract class GraphicsEnvironment {
private static GraphicsEnvironment localEnv;

/**
* This is an abstract class and cannot be instantiated directly.
* Instances must be obtained from a suitable factory or query method.
*/

protected GraphicsEnvironment() {
}

/**
* Returns the local GraphicsEnvironment.
* @return this GraphicsEnvironment.
*/
public static GraphicsEnvironment getLocalGraphicsEnvironment() {
if (localEnv == null) {
String nm = (String) java.security.AccessController.doPrivileged
(new sun.security.action.GetPropertyAction
("java.awt.graphicsenv", null));

try {
localEnv =
(GraphicsEnvironment) Class.forName(nm).newInstance();
} catch (ClassNotFoundException e) {
throw new Error("Could not find class: "+nm);
} catch (InstantiationException e) {
throw new Error("Could not instantiate Graphics Environment: "
+ nm);
} catch (IllegalAccessException e) {
throw new Error ("Could not access Graphics Environment: "
+ nm);
}
}

return localEnv;
}



For Every platform the JVM creates a GraphicsEnvironment object when you call the static getLocalGraphicsEnvironment() method on GraphicsEnvironment class From this object you call all the methods like getAllFontFamilyNames()/getAllFonts() etc. This is how we get the Font list. Here is a sample program.


import java.awt.*;

class tst {
public static void main(String[] args) {
String[] fontlist = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
for(int i=0; i System.out.println(fontlist[i]);
System.out.println("-------------");
Font[] fontlist1 = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
for(int i=0; i System.out.println(fontlist1[i].getName());
}

}


Partial Output (The list is too long )
Abadi MT Condensed
Abadi MT Condensed Light
Arial
Arial Black
Arial Cursiva
Arial Narrow
Arial Narrow Italic
Arial Narrow fed
Arial Narrow fed kursiv
Arial Narrow kursiv
Arial Negreta
Arial Negreta cursiva
Book Antiqua
Bookman Old Style
Bookman Old Style fed
Bookman Old Style fed kursiv
Bookman Old Style kursiv
Bookshelf Symbol 1
Bookshelf Symbol 2
Bookshelf Symbol 3
---------------------------------------------------------

[This message has been edited by maha anna (edited May 09, 2000).]

Vikram Deshmukh
greenhorn
posted May 10, 2000 12:48 AM             
Thanx !

|