gpt4 book ai didi

java - setBackground 不适用于第一次但第二次,为什么?

转载 作者:行者123 更新时间:2023-11-29 05:31:31 26 4
gpt4 key购买 nike

我在一本书中看到过这个例子,工作正常,但出现的唯一问题是背景在第一次调用 Paint 时没有变黑,当 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 () 在开始时只被调用一次,但为什么不能在开始时改变背景?

最佳答案

在您的初始化方法中,将 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 变量

boolean firstTime = true;

在 paint() 中

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

关于java - setBackground 不适用于第一次但第二次,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20931057/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com