Question

I have seen this examlpe in a book , and is working fine but the only problem coming up is that the Background Does not turns black on First Call to Paint, When clearCounter become == 5, and then the screen is Cleared and again when Painting Starts, then the Background is turned Black.

public class apletprg extends JApplet implements ActionListener 
    {
int clearCounter;
    Timer t;
public void init(){

    setBackground(Color.black);
    clearCounter = 0;
    Timer t = new Timer(1000, this);
    t.start();

}

public void paint(Graphics g)
{
    setBackground(Color.black);
    clearCounter++;
    Graphics2D g2 = (Graphics2D) g;

    if (clearCounter == 5){
        g2.clearRect(0, 0, 500, 400);
        clearCounter=0;
    }

    for (int i = 1; i <= 40; i++) {
        Color c = chooseColor();
        g2.setColor(c);
        Font f = chooseFont();
        g2.setFont(f);
        drawJava(g2);
        }
}
public void actionPerformed(ActionEvent ae){
repaint();
}
public Color chooseColor(){
int r= (int) (Math.random() * 255);
int g= (int) (Math.random() * 255);
int b= (int) (Math.random() * 255);
Color c = new Color(r,g,b);
return c;
}
public Font chooseFont(){

    int fontChoice = (int) (Math.random() * 4) + 1;
    Font f = null;
    switch (fontChoice) {
    case 1: f = new Font("Serif", Font.BOLD + Font.ITALIC, 20);break;
    case 2: f = new Font("SansSerif", Font.PLAIN, 17);break;
    case 3: f = new Font("Monospaced", Font.ITALIC, 23);break;
    case 4: f = new Font("Dialog", Font.ITALIC, 30);break;
    }
    return f;

}
public void drawJava(Graphics2D g2){
    int x = (int) (Math.random() * 500);
    int y = (int) (Math.random() * 400);
    g2.drawString("Adnan", x, y);   
}
}

I Know that the Init () is called only once at the Start , but Why is that not able to change the Background at start?

Was it helpful?

Solution 2

Resolved the ProbleBy Just Adding One more Variable and Calling the ClearRect() at the Start of painting and to ensure that This will be Called only once , by the Help of Newly Added Variable.

public void init(){

    setBackground(Color.black);
    clearCounter = 0;

    Timer t = new Timer(1000, this);
    t.start();
    check = 0;   <------------ New Variable


}
public void paint(Graphics g)
{
    if (check==0){
        g.clearRect(0, 0, 500, 400);   <------------ To Ensure That it will Excute Only Once , beacuse check is incremented later in Code
    }



    clearCounter++;
    check++;
    Graphics2D g2 = (Graphics2D) g;

    if (clearCounter == 5){
        g2.clearRect(0, 0, 500, 400);
        clearCounter=0;
    }

    for (int i = 1; i <= 40; i++) {
        Color c = chooseColor();
        g2.setColor(c);
        Font f = chooseFont();
        g2.setFont(f);
        drawJava(g2);
        }
}

OTHER TIPS

In your init method replace setBackground(Color.black) with getContentPane().setBackground(Color.black)

And add super.paint(g) as the first line in your paint method.

Otherwise if you don't want to use Swing features then go ahead and import java.applet.Applet and make you class extend Applet instead of JApplet

public class NewClass extends JApplet implements ActionListener {

    int clearCounter;
    Timer t;



    public void init() {

        getContentPane().setBackground(Color.black);
        repaint();
        clearCounter = 0;
        //t = new Timer("1000", true);


    }

    public void paint(Graphics g) {
        super.paint(g);
        setBackground(Color.black);
        clearCounter++;
        Graphics2D g2 = (Graphics2D) g;

        if (clearCounter == 5) {
            g2.clearRect(0, 0, 500, 400);
            clearCounter = 0;
        }

        for (int i = 1; i <= 40; i++) {
            Color c = chooseColor();
            g2.setColor(c);
            Font f = chooseFont();
            g2.setFont(f);
            drawJava(g2);
        }
    }
    @Override
    public void actionPerformed(ActionEvent ae) {
        repaint();
    }

    public Color chooseColor() {
        int r = (int) (Math.random() * 255);
        int g = (int) (Math.random() * 255);
        int b = (int) (Math.random() * 255);
        Color c = new Color(r, g, b);
        return c;
    }

    public Font chooseFont() {

        int fontChoice = (int) (Math.random() * 4) + 1;
        Font f = null;
        switch (fontChoice) {
        case 1:
            f = new Font("Serif", Font.BOLD + Font.ITALIC, 20);
            break;
        case 2:
            f = new Font("SansSerif", Font.PLAIN, 17);
            break;
        case 3:
            f = new Font("Monospaced", Font.ITALIC, 23);
            break;
        case 4:
            f = new Font("Dialog", Font.ITALIC, 30);
            break;
        }
        return f;

    }

    public void drawJava(Graphics2D g2) {
        int x = (int) (Math.random() * 500);
        int y = (int) (Math.random() * 400);
        g2.drawString("Adnan", x, y);
    }
}

If you want to execute super.paint() only once add a boolean variable in your class

boolean firstTime = true;

In paint()

if(firstTime) {
    super.paint(g);
    firstTime = false;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top