Fonts and colour Assignment Help

Assignment Help: >> Graphics >> Fonts and colour

Fonts and colour:

The AWT also provides support for fonts and colours. The model adopted by Java to communicate colours to drawing methods and fonts to methods that display strings and characters is to set a current colour and a current font. This colour and font are used by all the Graphics methods until such time as they are reset to other values.

In Java, there are two classes that relate directly to font support: the Font class and the abstract FontMetrics class. The Font class is usually the most frequently used, while FontMetrics is required when information about an existing instance of Font is needed.

The Font class de?nes the font itself, including the font family, size and style. For example, to create a font in Serif, at 12 point and in bold you would use:

someFont = new Font("Serif", Font.BOLD, 12);

The ?rst parameter to the constructor is a string that de?nes the font family. The next parameter is a static int variable from the Font class that de?nes the font as being bold. Finally, the last integer is the size of the type.

Once an instance of Font has been created, it can be used to de?ne the appearance of text in a display. For example, in a JPanel, the following would be possible:

public void paintComponent(Graphics g)
{

Font someFont = new Font("Serif", Font.BOLD, 12);
g.setFont(someFont);
g.drawString("Some Text!", 50, 50);
}

In this example, the paintComponent method de?nes a new font and then, by using the setFont method, instructs the Graphics context to set this as the font for further graphics operations. The next operation uses text and displays a string in the context. Because the font for the context was set prior to the call to drawString the text will be drawn using the font that was created.

The FontMetrics class can be used to obtain information about a particular font. This information is most useful when the display of an application requires the accurate positioning of components or when text needs to be formatted in a particular manner. The FontMetrics constructor requires an instance of Font as a parameter. For example:

Font aFont = new Font("Serif", Font.BOLD, 12);

Graphics g = this.getGraphics( );
FontMetrics someFontMetric = g.getFontMetrics(aFont);

First of all, the getGraphics method obtains the graphics context for the application and having obtained this, the getFontMetrics method can be invoked. Once we have access to an instance of FontMetrics, it is possible to use the methods of that class to return information about the font. Some of the methods in the class include stringWidth(String) and charWidth(char), which return the pixel widths of strings and characters in the font that was used to create the instance. For example, the code:

int wordLength = someFontmetric.stringWidth("Hello");
int charLength = someFontmetric.charWidth('h');

places in wordLength the width of the string "Hello" and in charLength the width of the character 'h'.

As an example of using these methods, imagine that you require the title of a ?le to be centred in a window. If the ?lename is obtained from a text ?eld, for example, there is no way of knowing the ?lename in advance and therefore it is impossible to know what the required offset is to ensure that the name of the ?le is centred as expected. However, by creating an instance of FontMetric, it is a simple matter to determine the width of the ?lename using stringWidth and then centre the string based on the width of the parent window. Other related methods return the height, ascent and descent of a font.

 

Java Assignment Help - Java Homework Help

Struggling with java programming language? Are you not finding solution for your Fonts and colour homework and assignments? Live Fonts and colour experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer Fonts and colour homework help, java assignment help and Fonts and colour projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.

Why Expertsmind for assignment help

  1. Higher degree holder and experienced experts network
  2. Punctuality and responsibility of work
  3. Quality solution with 100% plagiarism free answers
  4. Time on Delivery
  5. Privacy of information and details
  6. Excellence in solving java programming language queries in excels and word format.
  7. Best tutoring assistance 24x7 hours

 

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd