Search code examples
javaappletawtjava-2d

After the for loop is finished why nothing is displayed in the applet screen?


Below is a simple Applet Code the problem is after for loop is finished.

Nothing is displayed on the applet screen.

I guess screen is cleared after for loop is finished.

I am unable to fix it I would like to know how to prevent the screen from clearing so that my output is there on the screen.

public class ColorArcs extends Applet
{
int width=50;
int length=50;

int topx=200-25,topy=200-25;

public void paint(Graphics g)
{
    for(;length<250;)
    {
        g.drawArc(200-length/2,200-width/2,length,width,0,180);

        length+=2;
        width++;

        if(length>=50&&length<=75)
            setForeground(Color.cyan);
        else
            if(length>=75&&length<=100)
            setForeground(Color.yellow);
        else
            if(length>=100&&length<=125)
            setForeground(Color.green);
        else
            setForeground(Color.red);

        try
        {
            Thread.sleep(80);
        }
        catch(InterruptedException ie){}
    }
}
}

Solution

  • You are setting the foreground after setting the arc, therefore, it gets written over. That's why you're not getting anything to see.