FontMetrics help needed

Discussion in 'Java' started by David Cogen, Aug 17, 2006.

  1. David Cogen

    David Cogen Guest

    Hi, this seems like a "chicken and egg" problem, but I am sure there is a way to
    do this:

    In order to compute the height of a JPanel I want to instantiate I need to know
    the height of a line of text, which I could do using
    JPanel.getGraphics().getFontMetrics().getHeight().

    But that doesn't work until I call JPanel.setVisible() (null pointer exception).

    But before I can make it visible I need to know its size!

    Any suggestions?
     
    David Cogen, Aug 17, 2006
    #1
    1. Advertising

  2. David Cogen

    PofN Guest

    David Cogen wrote:
    > Hi, this seems like a "chicken and egg" problem, but I am sure there is a way to
    > do this:
    >
    > In order to compute the height of a JPanel I want to instantiate I need to know
    > the height of a line of text, which I could do using
    > JPanel.getGraphics().getFontMetrics().getHeight().
    >
    > But that doesn't work until I call JPanel.setVisible() (null pointer exception).
    >
    > But before I can make it visible I need to know its size!
    >
    > Any suggestions?


    Write a LayoutManager.

    PofN
     
    PofN, Aug 17, 2006
    #2
    1. Advertising

  3. David Cogen

    David Cogen Guest

    PofN wrote:
    > David Cogen wrote:
    >
    >>Hi, this seems like a "chicken and egg" problem, but I am sure there is a way to
    >>do this:
    >>
    >>In order to compute the height of a JPanel I want to instantiate I need to know
    >>the height of a line of text, which I could do using
    >>JPanel.getGraphics().getFontMetrics().getHeight().
    >>
    >>But that doesn't work until I call JPanel.setVisible() (null pointer exception).
    >>
    >>But before I can make it visible I need to know its size!
    >>
    >>Any suggestions?

    >
    >
    > Write a LayoutManager.
    >
    > PofN
    >


    That seems more trouble than it's worth though, and I'm having a hard time
    figuring out how it would help in this situation. The design doesn't use a
    layout manager currently and I'm not about to redesign from scratch if there is
    any other way.

    Surely there must be a way to find out what the default font's height will be
    before I make the JPanel visible. If I can do this one little thing my problem
    is solved. Isn't there?
     
    David Cogen, Aug 17, 2006
    #3
  4. David Cogen wrote:
    > Hi, this seems like a "chicken and egg" problem, but I am sure there is
    > a way to do this:
    >
    > In order to compute the height of a JPanel I want to instantiate I need
    > to know the height of a line of text, which I could do using
    > JPanel.getGraphics().getFontMetrics().getHeight().
    >
    > But that doesn't work until I call JPanel.setVisible() (null pointer
    > exception).
    >
    > But before I can make it visible I need to know its size!
    >
    > Any suggestions?


    My approach would be to set the height of the JPanel to be the point
    size of the font since the point size is the recommended vertical line
    spacing on most fonts.

    Jeff Coffield
     
    Jeffrey H. Coffield, Aug 18, 2006
    #4
  5. David Cogen wrote:
    > Hi, this seems like a "chicken and egg" problem, but I am sure there is
    > a way to do this:
    >
    > In order to compute the height of a JPanel I want to instantiate I need
    > to know the height of a line of text, which I could do using
    > JPanel.getGraphics().getFontMetrics().getHeight().
    >
    > But that doesn't work until I call JPanel.setVisible() (null pointer
    > exception).
    >
    > But before I can make it visible I need to know its size!
    >
    > Any suggestions?


    Very easy if you can use 1.5 or later.

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    public class test3 extends JPanel {
    String str = "Now is the time for all good men to come to the aid";
    int height;

    public test3(Font font) {
    setFont(font);
    FontMetrics fm = getFontMetrics(font);
    height = fm.getHeight();
    int width = fm.stringWidth(str);
    setPreferredSize(new Dimension(width,height));
    }

    public void paintComponent(Graphics g) {
    g.drawString(str,0,height);
    }

    public static void main(String[] args) {
    Runnable r = new Runnable() {
    public void run() {
    test3 t3 = new test3(new Font("Arial",Font.BOLD,24));
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.add(t3,BorderLayout.CENTER);
    f.pack();
    f.setVisible(true);
    }
    };
    EventQueue.invokeLater(r);
    }
    }

    --

    Knute Johnson
    email s/nospam/knute/
     
    Knute Johnson, Aug 18, 2006
    #5
  6. David Cogen

    Guest

    Knute Johnson wrote:
    > David Cogen wrote:
    > > Hi, this seems like a "chicken and egg" problem, but I am sure there is
    > > a way to do this:
    > >
    > > In order to compute the height of a JPanel I want to instantiate I need
    > > to know the height of a line of text, which I could do using
    > > JPanel.getGraphics().getFontMetrics().getHeight().
    > >
    > > But that doesn't work until I call JPanel.setVisible() (null pointer
    > > exception).
    > >
    > > But before I can make it visible I need to know its size!
    > >
    > > Any suggestions?

    >
    > Very easy if you can use 1.5 or later.
    >
    > import java.awt.*;
    > import java.awt.event.*;
    > import javax.swing.*;
    >
    > public class test3 extends JPanel {
    > String str = "Now is the time for all good men to come to the aid";
    > int height;
    >
    > public test3(Font font) {
    > setFont(font);
    > FontMetrics fm = getFontMetrics(font);
    > height = fm.getHeight();
    > int width = fm.stringWidth(str);
    > setPreferredSize(new Dimension(width,height));
    > }
    >
    > public void paintComponent(Graphics g) {
    > g.drawString(str,0,height);
    > }
    >
    > public static void main(String[] args) {
    > Runnable r = new Runnable() {
    > public void run() {
    > test3 t3 = new test3(new Font("Arial",Font.BOLD,24));
    > JFrame f = new JFrame();
    > f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    > f.add(t3,BorderLayout.CENTER);
    > f.pack();
    > f.setVisible(true);
    > }
    > };
    > EventQueue.invokeLater(r);
    > }
    > }
    >
    > --
    >
    > Knute Johnson
    > email s/nospam/knute/


    Please see Java font tutorial
    http://www.developerzone.biz/index.php?option=com_content&task=view&id=92&Itemid=36
     
    , Aug 18, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joana
    Replies:
    4
    Views:
    1,741
    William Brogden
    Oct 2, 2003
  2. Thomas Nick
    Replies:
    0
    Views:
    1,978
    Thomas Nick
    Jun 13, 2005
  3. Ryan Macy

    Ideas needed & help needed!

    Ryan Macy, Jul 19, 2006, in forum: Ruby
    Replies:
    2
    Views:
    546
    Ryan Macy
    Jul 19, 2006
  4. LeTubs
    Replies:
    6
    Views:
    137
    Tad McClellan
    Jan 25, 2004
  5. John Farold
    Replies:
    0
    Views:
    409
    John Farold
    Aug 29, 2012
Loading...

Share This Page