File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes FontMetrics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "FontMetrics" Watch "FontMetrics" New topic
Author

FontMetrics

Mike Louys Mike Louys
Greenhorn

Joined: Feb 17, 2005
Posts: 2
Writing an Applet that takes a string and places it in the center of specified area. Utilizing drawString to position the string for the output. I want to use FontMetric to determine the size of the string in Pixels.

I have the following lines coded to display the value returned from FontMetrics processing when perfroming the drawString.

import javax.swing.JApplet;
import javax.swing.JOptionPane;
import java.awt.Graphics;
import java.awt.Color;



public class WriteNameApplet extends JApplet
{

String sName;

int iWidth ,iHeight, sLength;

int topXSpot, topYSpot, lowXSpot, lowYSpot;


public void init()
{

sName = JOptionPane.showInputDialog(null,
"What is your Name?","Write your Name",
JOptionPane.QUESTION_MESSAGE);
}


public void start()
{
//obtain length of input string
sLength = sName.length( );

//obtain width and height of applet from html file
iWidth = getWidth( );
iHeight = getHeight( );

// get x coordinates based on width /2 - (length of name * Pixel per char)/2
topXSpot = iWidth/2 - (sLength*8)/2;
lowXSpot = iWidth/2 - (sLength*8)/2;

// get y coordinates based on height / 4 . In order to center in
// the middle of upper and lower halves.
topYSpot = iHeight/4;
lowYSpot = 3*(iHeight/4);

}


public void paint(Graphics g)
{
int fontWidth;

// creates new font metrics instance
FontMetrics fm = g.getFontMetrics();
fontWidth = fm.stringWidth(sName);

//set background color
setBackground(Color.cyan);

//change string to lower case letters

//output string sName
g.drawString(sName.toLowerCase( ) + ' ' + fontWidth,topXSpot, topYSpot);


//change string to upper case letters
//output string sName for second time
g.drawString(sName.toUpperCase( ),lowXSpot,lowYSpot);
}


}


When I go to compile the code, I get the following message:
WriteNameApplet.java:58:cannot resolve symbol
Symbol : class FontMetrics
Location : class WriteNameApplet
FontMetrics fm = g.getFontMetrics();
^
1 error

When I looked at the entries on the forum, it appears that I have this coded properly but....

I would appreciate any suggestions as to what the problem may be?
Liam Tiarnach
Ranch Hand

Joined: Aug 06, 2004
Posts: 51
Well looking at the error message...

then looking at your import statements...

looks like this import might just be missing...


at least that what jumped out at me...


- Liam...<br />- ' He who never sleeps... '
Mike Louys Mike Louys
Greenhorn

Joined: Feb 17, 2005
Posts: 2
Many Thanks, I added that import and everything worked fine.
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

Mike Louys Mike Louys

Welcome to JavaRanch! We don't have too many rules 'round here, but we do have our Naming Standards. Please Change your display name to match these standards. (Mike Louys will suffice/Even 'though the name is nice/There's no need to say it twice.)


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
 
 
subject: FontMetrics