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

Difference between object state and behavior, What is difference between ob...

What is difference between object state and behavior? Ans) If you change the state of an object, you ask it to perform a behavior. An object kept its states in a field e.g. vari

Describe the ? operator in java langauge, Describe The ? operator in Java l...

Describe The ? operator in Java langauge? The conditional operator just works for assigning a value to a variable, using a value within a method invocation, or in a few other w

Luminous Jewels, Byteland county is very famous for luminous jewels. Lumino...

Byteland county is very famous for luminous jewels. Luminous jewels are used in making beautiful necklaces. A necklace consists of various luminous jewels of particular colour. Nec

How many modules are there in spring, Spring comprises of seven modules. Th...

Spring comprises of seven modules. They are.. a) The core container b) Spring context c) Spring AOP d) Spring DAO e) Spring ORM f)  Spring Web module g)  Sprin

Basic difference between equals and identity method, The == gives true, i...

The == gives true, if the variable reference points to the similar types of object in memory. That is a " shallow comparison ".   The equals () - gives the results of run

Illustrate inheritance and composition?, The ' is a ' relationship is use...

The ' is a ' relationship is used with inheritance and ' has a ' relationship is used with composition . Both composition and inheritance allow you to perform sub-objects in

Write an aspect that counts different kinds of method calls, Problem statem...

Problem statement Part 1 Write an aspect that counts different kinds of method calls in a Java program. Your aspect should meet the following requirements:  The aspect should ma

Program on remote procedure call on a cs architecture, This project simulat...

This project simulates a remote procedure call on a client-server architecture. You will create both the client and the server. The server will maintain a linked list. It must N

Java and Arrays, AskWrite an application that uses an Array to store 10mess...

AskWrite an application that uses an Array to store 10messages of type String. You will store this Array with 10 messages of your choosing. For example, a message could be “I love

What are inner beans, When wiring beans, if a bean element is embedded to a...

When wiring beans, if a bean element is embedded to a property tag directly, then that bean is said to the Inner Bean. The disadvantage of this bean is that it cannot be reused any

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