• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

FontMetrics

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Many Thanks, I added that import and everything worked fine.
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.)
 
Of course, I found a very beautiful couch. Definitely. And this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic