点数不会在立即点击时增加,但在随后的点击时会增加

Points not increasing on right away click but on subsequent click it increases

我创建了一个颜色猜谜游戏,其中正方形从 2 种颜色的数组中获得随机颜色(红色或蓝色),并且有 2 个按钮,一个红色和一个蓝色。游戏从方块中已有的任何一种颜色开始,如果我们猜测下一种颜色是红色,我们按下红色按钮,如果猜测正确,我们得到 1 分。但不知何故,即使猜测正确,点的更新也不会立即显示,并且更新仅在第二次单击同一个红色按钮时出现。总之更新延迟或等待下一次点击。

import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.*;
import java.util.Random;

class apple1 extends Frame{
    private Button b;
    private Button b2;
    Graphics2D g2;
    TextField tf;
    TextArea lbl;
    int count = 1;
    public void paint(Graphics g) {
        g2 = (Graphics2D) g;
        Color[] clrs = {Color.red,Color.blue};
        Random rand = new Random();
        g2.setColor(clrs[rand.nextInt(clrs.length)]);
        g2.fillRect (60, 50, 200, 200);  
      }
    public apple1(){      
        tf = new TextField("Points: ");
        tf.setBounds(10, 30, 280, 20);

        //create components   
        b=new Button("RED");
        b.setBackground(Color.red);
        b.setBounds(80,260,80,30); 

        //register listener
        b.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                if(b.getBackground().equals(g2.getColor())){
                    tf.setText(String.valueOf(count++));

         }
                else{
                    tf.setText("Sorry your guess was wrong");       
                }
                repaint();
            }

        });//passing current instance 

        b2=new Button("BLUE");
        b2.setBackground(Color.blue);
        b2.setBounds(180,260,80,30); 

        b2.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                if(b2.getBackground().equals(g2.getColor())){
                    tf.setText(String.valueOf(count++));

         }
                else{
                    tf.setText("Sorry your guess was wrong");       
                }
                repaint();
            }

        });//passing current instance         
        //add components and set size, layout and visibility  
        add(b);add(b2);add(tf);  
        setSize(600,600);  
        setLayout(null);
        setVisible(true);
        }


public static void main(String args[]){  
new apple1();  
}  
}  

请帮帮我,我很沮丧,我无法解决这么小的问题。

============================================= ==============================

根据@VGR 的建议对上述代码进行了修改。这是现在的样子,问题已解决。谢谢@VGR

import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.*;
import java.util.Random;

class apple1 extends Frame{
    private Button b;
    private Button b2;
    Graphics2D g2;
    TextField tf;
    TextArea lbl;
    int count = 1;
    private static final Color[] clrs = {Color.red,Color.blue};
    private Random rand = new Random();
    private Color bg = clrs[rand.nextInt(clrs.length)];
    public void paint(Graphics g) {
        g2 = (Graphics2D) g;
        g2.setColor(bg);
        g2.fillRect (60, 50, 200, 200);  
      }
    public apple1(){      
        tf = new TextField("Points: ");
        tf.setBounds(10, 30, 280, 20);

        //create components   
        b=new Button("RED");
        b.setBackground(Color.red);
        b.setBounds(80,260,80,30); 

        //register listener
        b.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub

                bg = clrs[rand.nextInt(clrs.length)];
                repaint();
                if(b.getBackground().equals(bg)){
                    tf.setText(String.valueOf(count++));

         }
                else{
                    tf.setText("Sorry your guess was wrong");       
                }
            }

        });//passing current instance 

        b2=new Button("BLUE");
        b2.setBackground(Color.blue);
        b2.setBounds(180,260,80,30); 

        b2.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub

                bg = clrs[rand.nextInt(clrs.length)];
                repaint();
                if(b2.getBackground().equals(bg)){
                    tf.setText(String.valueOf(count++));

         }
                else{
                    tf.setText("Sorry your guess was wrong");       
                }
            }

        });//passing current instance         
        //add components and set size, layout and visibility  
        add(b);add(b2);add(tf);  
        setSize(600,600);  
        setLayout(null);
        setVisible(true);
        }


public static void main(String args[]){  
new apple1();  
}  
}  

如果按下按钮,存储按下的颜色。

随机一个新的背景颜色。

检查存储的颜色是否与新颜色相同。

添加点并设置新背景颜色。

  1. 传递给绘画方法的 Graphics(或 Graphics2D)对象仅在该绘画方法运行期间有效。一旦退出 paint 方法,您将无法使用它。
  2. 绘画方法由系统调用。您无法控制他们被调用的频率。除了 repaint() 调用之外,还有很多原因可以调用 Paint 方法,包括 window 成为 moved/raised/lowered/resized,甚至只是将鼠标指针移到 window 上。因此,任何改变状态的逻辑都不能发生在绘画方法中。

将您对背景的更改移至 ActionListener,并且不要尝试将 Graphics 对象存储在实例字段中。将颜色值本身存储在字段中:

private static final Color[] clrs = {Color.red,Color.blue};

private final Random rand = new Random();

private Color background = clrs[rand.nextInt(clrs.length)];

// ...

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    g2.setColor(background);
    g2.fillRect(60, 50, 200, 200);  
}

// ...

    b.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {

            if (b.getBackground().equals(background)) {
                tf.setText(String.valueOf(count++));
            } else {
                tf.setText("Sorry your guess was wrong");       
            }

            background = clrs[rand.nextInt(clrs.length)];
            repaint();
        }
    });

    // Do the same for the other ActionListener...

注意 paint 方法没有改变任何东西;它仅使用由其他方法更改的状态变量。另请注意,没有代码引用 paint 方法之外的 Graphics 对象。