0

I have something I need to draw on the screen, but that requires font metrics to draw correctly. I also need to use repaint() or something else to draw to the screen right then and there.

If I have a paintComponent(Graphics) method, I can retrieve the font metrics correctly via g.getFontMetrics(g.getFont()). The problem is, I cannot tell it to paint it's self. It only does it when something happens such as the component being resized.

Then if I use just normal paint(Graphics), I may use repaint() to draw when I want it to, but calling g.getFontMetrics(g.getFont()) does not return correct values. Any ideas?

repaint();//I need to call repaint() or something similar to draw to the screen when I want it to

public void paint(Graphics g){
    FontMetrics metrics = g.getFontMetrics(g.getFont());//Returns different metrics than that of paintComponent(Graphics)
}

public void paintComponent(Graphics g){
    FontMetrics metrics = g.getFontMetrics(g.getFont());//Returns different metrics than that of paint(Graphics)
}

1 Answers1

2

The problem is, I cannot tell it to paint it's self.

You just use repaint() on the component. The repaint() method will invoke paint() which in turn invokes paintComponent(). See A Closer Look at the Paint Mechanism for more information.

I set it to the variable and use that font object instead of using g.getFont()

From the tutorial link given above you will see that you should be overriding paintComponent() not paint(), so this is not an issue.

camickr
  • 321,443
  • 19
  • 166
  • 288