In: Computer Science
1. How are different fonts set with Java GUI programs? What are font statistics?
2. What is meant by drawing a string with Java and how is this done?
1. We use a class as java.awt.Font to mark a particular Font Face for required texts. A Constructor can also be created for creating an instance of Font. We may also proceed for setFont() method that is used to render the text with the current font. A font can be plain, bold or italic. The font face represents the name that is specified for a certain font face like Arial Italic, Arial etc. The Font Family describes the family structure for a particular font face. We have two other classifications for Fonts: Logical and Physical. Logical Fonts are assisted by JDK whereas Physical Fonts are font libraries itself. We can make use of a getAvailableFontFamilyNames() to get to know about all Font Family Names. We may use getAllFonts() for creating the Font Instance. For deriving a new instance of a Font, we can prefer deriveFont() which helps in the obtaining a font instance from the current font which offers difference in size, style and properties. We can use different methods or routines to get to know of current font such as getName(), getSize(), getStyle(), getFontName().
For acquiring the descriptive information about spacing and size of the text with a certain font, we use a class java.awt.font.LineMetrics. We can specify getLineMetrics() method in our code to ask for a metrics that have been used in a text for a particular font. Our code can also provide getStringBounds() method in our Font class that provides bounding box for particular text. FontShow is used to represent characterstics of a particular font been used in a text. LineMetrics class can allow us to use getAscent () and getDescent() methods that ask for height and depth of baseline been used in text. getLeading() method is used to get to know details of vertical spacing between the lines of text. getHeight() method is used to represent the total height of text with space.
2. While drawing different primitive shapes , we can also draw strings or text with the help of Graphics class. There are certain methods been used for drawing strings in Java. There are three methods to be used for drawing strings: drawString(), drawChars() and drawBytes(). In drawString() method, we use String class instance and two values of integers. The String class instance represents the string to be drawn such as void drawString(String name, int a, int b). A drawChars() method contains character array, array offset, integer value for number of drawing characters and two coordinates enabling the text starting points such as drawChars(Char [] name, int offset, int l, int a , int b ). A drawBytes() method holds a byte array, an offset integer value, an integer value for number of bytes to be drawn and two coordinates representing the text starting points such as void drawBytes(byte [] name, int offset, int l, int a, int b).