Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / April 2005

Tip: Looking for answers? Try searching our database.

font definition - beginner's question

Thread view: 
Joe - 25 Apr 2005 10:36 GMT
I'm trying to write a program where the user enters the font name (e.g.
SansSerif), font style (e.g. italic), and font size, and then in a
suitably-sized new window, in the specified font, it displays various
information about the font (its ascent, descent, leading, etc.).

I can do the font name and size, but I can't work out how to convert
the user's input for font style into a form that can be used in the
definition of the new font. I.e. the first and third of the two
arguments in the following statement work, but the middle one doesn't:
Font f = new Font(fontName, "Font." + fontStyle, fontSize);

Thanks very much for any help.

***************************************************

Actually this is Programming Project 1 on p250 of "Java Programming
>From the Beginning" (ISBN 0393974375). What I have to do is modify the
program below. (This uses some classes written by the author of the
book.)

// Displays information about a font. Uses the FontMetrics
// class to obtain the information to be displayed.

import java.awt.*;
import jpb.*;

public class FontInfo {
 public static void main(String[] args) {
   // Create drawable frame
   DrawableFrame df = new DrawableFrame("Font Information");
   df.show();
   df.setSize(215, 175);

   // Get graphics context
   Graphics g = df.getGraphicsContext();

   // Create font, get metrics for font, and compute height
   // of font
   Font f = new Font("SansSerif", Font.PLAIN, 20);
   FontMetrics fm = g.getFontMetrics(f);
   int height = fm.getHeight();

   // Display information about font
   g.setFont(f);
   g.drawString("Ascent: " + fm.getAscent(), 10, height);
   g.drawString("Descent: " + fm.getDescent(), 10,
                height * 2);
   g.drawString("Height: " + height, 10, height * 3);
   g.drawString("Leading: " + fm.getLeading(), 10,
                height * 4);
   g.drawString("Maximum advance: " + fm.getMaxAdvance(), 10,
                height * 5);
   g.drawString("Width of \"Why?\": " +
                fm.stringWidth("Why?"), 10, height * 6);

   // Repaint frame
   df.repaint();
 }
}
Boudewijn Dijkstra - 25 Apr 2005 11:18 GMT
> I'm trying to write a program where the user enters the font name (e.g.
> SansSerif), font style (e.g. italic), and font size, and then in a
[quoted text clipped - 6 lines]
> arguments in the following statement work, but the middle one doesn't:
> Font f = new Font(fontName, "Font." + fontStyle, fontSize);

As you can see in the Font constructor specification, the middle argument is
an int, not a String.  You would have to convert your fontStyle to the
appropriate int value.  I hope you can do that yourself.
Peter MacMillan - 25 Apr 2005 13:07 GMT
> I'm trying to write a program where the user enters the font name (e.g.
> SansSerif), font style (e.g. italic), and font size, and then in a
[quoted text clipped - 6 lines]
> arguments in the following statement work, but the middle one doesn't:
> Font f = new Font(fontName, "Font." + fontStyle, fontSize);

try:
http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Font.html#decode(java.lang.String)

Format your font request into a string and then pass it into Font.decode.

eg.

//---
Font f = Font.decode("SansSerif ITALIC 12");
//---

Signature

Peter MacMillan
e-mail/msn: peter@writeopen.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.