Working with Fonts
import java.awt.event.*;
import java.awt.*;
public class FontsDemo extends Frame{
String msg = "Font Types";
String msg1="",msg2="",msg3="";
GraphicsEnvironment ge;
Font f;
public FontsDemo(){
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fonts = ge.getAvailableFontFamilyNames();
for(int i=0;i<5 && i<fonts.length;i++){
msg += fonts[i]+"; ";
}
f = new Font("Dialog",Font.PLAIN,12);
//creatint new font, font-type,font-style,font-size
msg1 = "Changed Font to Dialog";
//setting font type
setFont(f);
f = new Font("SansSerif",Font.PLAIN,12);
msg2 = "Changed Font to SansSerif";
setFont(f);
f = new Font("Monospaced",Font.PLAIN,12);
msg3 = "Changed Font to Monospaced";
setFont(f);
}
public void paint(Graphics g){
//drawString method write text in the specified position
//it takes string, x-coord, y-coord
g.drawString(msg,10,60);
g.drawString(msg1,10,90);
g.drawString(msg2,10,120);
g.drawString(msg3,10,150);
Font f= g.getFont();
//getting graphics current context font
String fontName = f.getName();//getting fontname
String fontFamily = f.getFamily();//getting fontfamily
int fontSize = f.getSize(); //getting fontSize
int fontStyle = f.getStyle();//getting fontStyle
msg = fontName+", "+fontFamily+", "+fontSize+", "+fontStyle;
g.drawString(msg,10,180);
}
public static void main(String[] args){
FontsDemo app = new FontsDemo();
app.setSize(new Dimension(770,300));
app.setTitle("Fonts");
app.setVisible(true);
}
}