简体   繁体   中英

Run a method only once in paint () (Java)

Im trying to run a method only once when I call it in the paint () method, but when I call it normally it just runs over and over. Example:

private void someMethod ()
{
 System.out.println ("Success");
}
@Override
public void paintComponent (Graphics g)
{
 someMethod ();
 repaint ();
}

This will output "Success" forever, I only can output it once.

I think you should write the same bridge

Try this:

public class someClass{
    private boolean hasDone = false;
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        if(!hasDone){
            someMethod();
            hasDone = true;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM