-1

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){}
    }
}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Udesh
  • 2,415
  • 2
  • 22
  • 32
  • Applets are a dead technlogy. modern browsers even don't support Java applets anymore. So if you want to do graphics programming in Java, use awt, swing or javafx. – P.J.Meisch Mar 30 '19 at 06:24

3 Answers3

2

It is not getting cleared after for loop finished.Screenshot of Applet viewer and code comment out side for loop

Piyush N
  • 742
  • 6
  • 12
1

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

1

To maintain the paint follow the idea of Abhinav. But to change the color see the code bellow: (everything is not fixed but you can start with the idea)

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;)
    {
        length+=2;
        width++;

        if(length>=50&&length<=75)
            setForeground(Color.cyan);

    }

    int length_ = 50; width=50;
    for(;length_<250;)
    {
        g.drawArc(200-length_/2,200-width/2,length_,width,0,180);

        length_+=2;
        width++;

        try
        {
            Thread.sleep(20);
        }
        catch(InterruptedException ie){}
    }
}
}
Jaja
  • 662
  • 7
  • 15