java - Getting the font metrics before the paint method id called -


hi creating news ticker/ text scroller. using following method:

import java.awt.color; import java.awt.fontmetrics; import java.awt.graphics;  import javax.swing.jframe; import javax.swing.jpanel;  public class scroll1 extends jpanel{     private int x;     private int x2;     private int y;     private string text;     final int startx=-100;     public scroll1(int startx)     {         x2=-650;         x = 20;         y=150;         text= "some words , others, , must longer text takes whole panel/ frame test work   ";     }     @override     public void paint(graphics g)     {         g.setcolor(color.white);         g.fillrect(0, 0, 400, 300);         g.setcolor(color.black);         g.drawstring(text, x, y);         g.drawstring(text, x2, y);         fontmetrics fm= g.getfontmetrics();          system.out.println(fm.stringwidth(text));;     }      public void start() throws interruptedexception{         while(true){             while(x<= 650){                 x++;                 x2++;                 y = getheight()/2;                 repaint();                 thread.sleep(10);                 if(x2>650)                     x2=-650;             }              if(x>=0)             {                 x=-650;              }         }     }     public static void main(string[] args) {         jframe frame = new jframe("scrolling panel");         frame.setdefaultcloseoperation(jframe.exit_on_close);         scroll1 scrolling = new scroll1(-100);          frame.getcontentpane().add(scrolling);         frame.setsize(400, 300);         frame.setvisible(true);         try {             scrolling.start();         } catch (interruptedexception e) {             // todo auto-generated catch block             e.printstacktrace();         }     } } 

basically has 2 strings being drawn. 1 starts @ 0 position , other starts @ -650. got -650 number using font metrics inside of paint method. problem had hard code number, , if did different string has different metrics, not work. tried making instance variable called width stores font metrics, seems width not inputted until paint method called. there anyway can metrics before starts drawing it?

is there anyway can metrics before starts drawing it?

just initialize variable in first call paint (or better yet, paintcomponent - see below) - can using boolean flag, or initialize it's value extreme , check on value.

int x = integer.min_value; ... protected void paintcomponent(graphics g){     super.paintcomponent(g);     if ( x == integer.min_value ){         x = -g.getfontmetrics().stringwidth(text);     }     ... } 

some other tips:

  1. use swing timer perform animation, or sure dispatch swing specific calls edt using swingutilities.
  2. don't override paint, rather override paintcomponent (and sure call parent method super.paintcomponent(g))

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -