Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 挫折在第一次不起作用,但在第二次起作用,为什么?_Java_Swing_Applet_Japplet_Setbackground - Fatal编程技术网

Java 挫折在第一次不起作用,但在第二次起作用,为什么?

Java 挫折在第一次不起作用,但在第二次起作用,为什么?,java,swing,applet,japplet,setbackground,Java,Swing,Applet,Japplet,Setbackground,我在一本书中看到过这个例子,效果很好,但出现的唯一问题是,第一次调用Paint时背景不会变黑,当clearCounter变为==5时,然后屏幕被清除,当开始绘制时,再次将背景变黑 public class apletprg extends JApplet implements ActionListener { int clearCounter; Timer t; public void init(){ setBackground(Color.black); c

我在一本书中看到过这个例子,效果很好,但出现的唯一问题是,第一次调用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);   
}
}
公共类apletprg扩展了JApplet实现ActionListener
{
int clearCounter;
定时器t;
公共void init(){
挫折背景(颜色:黑色);
clearCounter=0;
定时器t=新定时器(1000,此);
t、 start();
}
公共空间涂料(图g)
{
挫折背景(颜色:黑色);
clearCounter++;
图形2d g2=(图形2d)g;
如果(clearCounter==5){
g2.clearRect(0,0500400);
clearCounter=0;
}

对于init方法中的(inti=1;i,将
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);
    }
}
在油漆中()


解决了这个问题,只需再添加一个变量,并在绘制开始时调用ClearRect(),并通过新添加的变量确保只调用一次

公共void init(){

backbackground(颜色:黑色);
clearCounter=0;
定时器t=新定时器(1000,此);
t、 start();

检查=0;我将
计时器增加到5000毫秒,以便有更多时间查看渲染工件,并在1.7.0_25小程序查看器中运行了几次。BG每次都立即变黑。您是否看到非黑色BG顶部的文字?它运行的是什么Java版本?显示或打印
System.getProperty(“Java.version”))
在小程序中,确保它以您期望的版本运行。我的版本是1.7.0_07,如何更新它以使用最新版本?我在我最喜欢的搜索引擎中输入了“java最新版本”,链接就在列表顶部。您可以试一试。;)是的,一定要更新它。AFAIR那些早期版本的Java 7充满了漏洞。更糟糕的是,他们有公开的安全漏洞。如果安装了这样的Java版本,我在网上冲浪会感到不安全。我安装了最新版本,因为我的更新状态显示Java今天已经更新,但如果你是u使用IDE可能需要更改那里的运行时。您是否尝试在init内部调用repaint?您的努力并没有完全白费。+1:)@andrewhompsom谢谢:-)当然您认为是对的,但是在paint()中,super.paint(g)每次重新绘制时都会删除以前绘制的单词,但我真正想做的是仅在第五次调用repaint()时删除以前的单词;@AdnanAhmadKhan您可以在调用
super.paint(g)
@AdnanAhmadKhan查看更新后的答案。最后一部分。我已经尝试过了,并且效果很好。
boolean firstTime = true;
if(firstTime) {
    super.paint(g);
    firstTime = false;
}
    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);
        }
}