Explain what is fontmetrics in java, JAVA Programming

Assignment Help:

Explain what is FontMetrics in Java?

No word wrapping is completed when you draw a string in an applet, even if you embed newlines in the string along with \n. If you expect that a string may not fit in the applet, you should possibly use a TextArea Component instead. You'll learn about text areas and other AWT Components next class. Therefore there are times when you will require concerning yourself along with how much space a particular string will occupy. You search this out with a FontMetrics object. FontMetrics permit you to determine the height, width or other useful characteristics of a particular string, character, or array of characters in a particular font.

As an example the following program expands on the DrawString applet. Previously text would run off the side of the page if the string was too long to fit in the applet. Presently the string will wrap around if essential.

In order to tell where and whether to wrap the String, you required to measure the string, not its length in characters which can be variable but rather its width and height in pixels. Measurements of this sort on strings clearly depend on the font that's used to draw the string. All other things being equal a 14 point string will be wider than the similar string in 12 or 10 point type.

To measure character and string sizes you need to look at the FontMetrics of the current font. To obtain a FontMetrics object for the current Graphics object you use the java.awt.Graphics.getFontMetrics() method. From java.awt.FontMetrics you'll need fm.stringWidth(String s) to return the width of a string within a particular font, and fm.getLeading() to obtain the appropriate line spacing for the font. There are several more methods in java.awt.FontMetrics that let you measure the heights and widths of exact characters as well as ascenders, descenders and more, but these three methods will be enough for this program.

At last you'll need the StringTokenizer class from java.util to split up the String into individual words. therefore you do required to be careful lest some annoying beta tester (or, worse yet, end user) tries to see what happens when they feed the word antidisestablishmentarianism or supercalifragilisticexpialidocious into an applet that's 50 pixels across.

import java.applet.*;
import java.awt.*;
import java.util.*;

public class WrapTextApplet extends Applet {

private String inputFromPage;

public void init() {
this.inputFromPage = this.getParameter("Text");
}

public void paint(Graphics g) {

int line = 1;
int linewidth = 0;
int margin = 5;
StringBuffer sb = new StringBuffer();
FontMetrics fm = g.getFontMetrics();
StringTokenizer st = new StringTokenizer(inputFromPage);

while (st.hasMoreTokens()) {
String nextword = st.nextToken();
if (fm.stringWidth(sb.toString() + nextword) + margin <
this.getSize().width) {
sb.append(nextword);
sb.append(' ');
}
else if (sb.length() == 0) {
g.drawString(nextword, margin, line*fm.getHeight());
line++;
}
else {
g.drawString(sb.toString(), margin, line*fm.getHeight());
sb = new StringBuffer(nextword + " ");
line++;
}

}
if (sb.length() > 0) {
g.drawString(sb.toString(), margin, line*fm.getHeight());
line++;
}

}

}


Related Discussions:- Explain what is fontmetrics in java

Online doctor, can you explain me the er diagram for the online doctor syst...

can you explain me the er diagram for the online doctor system

What is constructors and explain with an example, What is Constructors? Exp...

What is Constructors? Explain with an example? A constructor forms a new instance of the class. It initializes all the variables and does any work essential to prepare the clas

Create a program using constructors, Create a program Using Constructors? ...

Create a program Using Constructors? The further program uses the constructor to initialize a car rather than setting the areas directly. class CarTest7 { public static voi

What is javaserver faces validation model, A device for validating the data...

A device for validating the data a user inputs to a JavaServer Faces UI component.

Control graphical user interface elements, Introduction In this assign...

Introduction In this assignment you will use Processing to create some geometric objects and graphical user interface (GUI) elements that Processing itself lacks. Processing h

Explain the uses of JVM verifier, Explain the uses of JVM verifier The ...

Explain the uses of JVM verifier The JVM "verifier" checks the code when it's loaded to verify that it has the correct structure --that it does not use an uninitialized pointer

Opengts geozone violation alerts, Opengts Geozone violation alerts Proje...

Opengts Geozone violation alerts Project Description: I am seeking a very reliable, articulate and an experienced Java and MySQL developer to customize the Device Communicati

Java expert with tomcat tuning experience required, Java expert with tomcat...

Java expert with tomcat tuning experience needed Project Description: Want a Java expert to fix memory issues with tomcat. I need School java project - Threads Programm

Write Your Message!

Captcha
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