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

Give an examples of java.lang.math methods, Give an Examples of java.lang.M...

Give an Examples of java.lang.Math Methods Here is an instance program in which exercises most of the routines in java.lang.Math. If your high school math is a little rusty, do

Array prints random numbers into string, Prompt the user for an int between...

Prompt the user for an int between an upper and lower boundary. Reuse the validateInput() method from project 2 to validate if the input is in bounds. If it is not, print an error

Write html and javascript code which displays a textbox, Write HTML and Jav...

Write HTML and JavaScript code which displays a textbox and button on a web page? While user enters text in the text box and clicks the button it displays in that text in the m

Program, Luminous Jewels - The Polishing Game Byteland county is very famo...

Luminous Jewels - The Polishing Game Byteland county is very famous for luminous jewels. Luminous jewels are used in making beautiful necklaces. A necklace consists of various lum

Page replacement algorithms code in nachos, what is the code for page repla...

what is the code for page replacement algorithms in nachos os

Want a anti iframe breaker, Want a ANTI Iframe Breaker Project Descripti...

Want a ANTI Iframe Breaker Project Description: I want an ANTI Iframe Breaker code made, so that if I'm iframing a site, that contains JavaScript, flash or otherwise, to forc

What is linear search, What is linear search? It is the simplest form o...

What is linear search? It is the simplest form of search. It searches for the element sequentially starting from first element. This search has a drawback if element is located

Retrieving file records randomly, how to retrieve multiple records randomly...

how to retrieve multiple records randomly from a file and store it in another file

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