Search code examples
javaimageappletrendering

Drawing string on applet only once


New in Java and might be repeated question.

Using full screen applet without title bar to render images on ever 10 millisecond time interval with help of paint method. At the same time I want specific text to be displayed on applet screen at any position.

Since images are dynamic hence I am rendering on 10 millisecond but text is fix and not going to change in whole life of applet.

Now concern is, if I draw text in paint method then its burden on paint method to draw image and text on every 10 millisecond time.

public void paint(Graphics  g) 
{   
    if(img != null)
    {
        g.drawImage(img, 0, 0, null);   
        g.drawString("Hey there!", 0, 0); //Additional load
    }           
}   

Cannot place text on title bar too.

Is it possible to draw text at once and rendered images frequently?


Solution

  • public void paint(Graphics  g) 
    { 
    

    Should instead be:

    public void paint(Graphics  g) 
    { 
         super.paint(g); // paint the background, borders etc.
    

    So given doing that will erase the previously drawn text, the answer to your question is - no.

    BTW - look up 'premature optimization'.