Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

FontMetrics

 
Mike Louys Mike Louys
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Mike Louys Mike Louys
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many Thanks, I added that import and everything worked fine.
 
Joel McNary
Bartender
Posts: 1824
Eclipse IDE Java Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic