The Artima Developer Community
Sponsored Link

Java Answers Forum
problem with TEXT

1 reply on 1 page. Most recent reply: Nov 29, 2004 1:06 AM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
karim

Posts: 5
Nickname: apahliani
Registered: Nov, 2004

problem with TEXT Posted: Nov 28, 2004 7:42 AM
Reply to this message Reply
Advertisement
I am writing an application to show a text and measure different aspect of it as follows:

// Simple animation with Java
import java.awt.*;
import javax.swing.*;
public class AnimateEx extends JFrame {

String theString = "Boing!!!";
private int stringTop, stringBottom, stringWidth;


public void paint(Graphics g)
{
g.drawString(theString, 100,200);
}
public AnimateEx()
{
setSize(770,420);
setFont( new Font("TimesRoman", Font.BOLD+Font.ITALIC, 24));
setBackground(Color.yellow);
setForeground(Color.red);
Graphics g=this.getGraphics();
FontMetrics fm= g.getFontMetrics();
stringWidth = fm.stringWidth(theString);
stringTop = fm.getAscent();
stringBottom = fm.getDescent();

}

public static void main(String[] args){
AnimateEx ex= new AnimateEx();
ex.setVisible(true);
}
}

When I run the application without the followin lines in the constructor :
FontMetrics fm= g.getFontMetrics();
stringWidth = fm.stringWidth(theString);
stringTop = fm.getAscent();
stringBottom = fm.getDescent();


(which I add to get some text measure )it works well but after adding those lines I got this error (no error during compile but have it when run):

Exception in thread "main" java.lang.NullPointerException
at AnimateEx.<init>(AnimateEx.java:20)
at AnimateEx.main(AnimateEx.java:28)


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: problem with TEXT Posted: Nov 29, 2004 1:06 AM
Reply to this message Reply
Did you check wich value is null?
I would guess that g == null, because you didn't show() the JFrame yet.

Flat View: This topic has 1 reply on 1 page
Topic: a simple problem with GUI Previous Topic   Next Topic Topic: installing a java application

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use