繁体   English   中英

setBackground第一次不起作用,但第二次,为什么?

[英]setBackground does not works on First time but on Second, Why?

我在一本书中看过这个考试,并且工作正常,但是唯一的问题是,在第一次调用绘制时背景不会变黑,当clearCounter变为== 5时,然后屏幕被清除并且再次绘制时开始,然后背景变为黑色。

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);   
}
}

我知道Init()在Start时只调用一次,但为什么在开始时无法更改Background?

在你的init方法中用getContentPane().setBackground(Color.black)替换setBackground(Color.black) getContentPane().setBackground(Color.black)

并添加super.paint(g)作为paint方法的第一行。

否则,如果您不想使用Swing功能,那么继续导入java.applet.Applet并使您的类扩展Applet而不是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);
    }
}

如果只想在类中添加一个布尔变量,就要执行super.paint()

boolean firstTime = true;

在油漆()

if(firstTime) {
    super.paint(g);
    firstTime = false;
}

解决了问题只需添加一个变量并在绘画开始时调用ClearRect()并确保通过新增变量的帮助仅调用一次。

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);
        }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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