1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
/* Get Font Information Example This java example shows how to get Font information like font name, size, style using Java AWT Font class. */ import java.applet.Applet; import java.awt.Font; import java.awt.Graphics; /* <applet code="GetFontInformation" width=200 height=200> </applet> */ public class GetFontInformation extends Applet{ public void paint(Graphics g){ /* * To get current font, use * Font getFont() method. */ Font currentFont = g.getFont(); /* * To get a font name, use * String getName() * method of AWT Font class. */ String fontName = currentFont.getName(); /* * To get font size, use * int getSize() * method of Font class. */ int size = currentFont.getSize(); /* * To get style of font, use * int getStyle() * method of Font class. * * Style can be Font.BOLD, Font.ITALIC or Font.PLAIN. * * You can directly call isBold(), isItalic() or * isPlain() methods of AWT Font class. */ int style = currentFont.getStyle(); String fontStyle = ""; if( (style & Font.BOLD) == Font.BOLD) fontStyle = "Bold"; if( (style & Font.ITALIC) == Font.ITALIC) fontStyle = "Italic"; if( (style & Font.PLAIN) == Font.PLAIN) fontStyle = "Plain"; /* * To get font family, use * String getFamily() * method of Font class. */ String family = currentFont.getFamily(); g.drawString("Font Name: " + fontName, 10, 30); g.drawString("Font size: " + size, 10, 50); g.drawString("Font Family: " + family, 10, 70); g.drawString("Font Style: " + fontStyle, 10, 90); } } |
Example Output
Add Comment