Java 如何更改计时器内的油漆颜色

Java 如何更改计时器内的油漆颜色,java,swing,timer,paintcomponent,Java,Swing,Timer,Paintcomponent,这是我的代码中我关心的部分,当我尝试再次使用计时器更改squaresToDisplay对象的颜色时,它会使帧变为白色(空白),但只工作1次。因此,当我运行这段代码时,它将执行我想要的操作1次,然后使屏幕变为空白。我想知道具体是什么原因造成的。我自己的假设是,当我启动SQTimer时,我可能会阻塞EDT,在这种情况下,我会不知所措,因为我不知道足够的java来修复这个问题:/ private Timer SQTimer; startButton.addActionListener(new Acti

这是我的代码中我关心的部分,当我尝试再次使用计时器更改squaresToDisplay对象的颜色时,它会使帧变为白色(空白),但只工作1次。因此,当我运行这段代码时,它将执行我想要的操作1次,然后使屏幕变为空白。我想知道具体是什么原因造成的。我自己的假设是,当我启动SQTimer时,我可能会阻塞EDT,在这种情况下,我会不知所措,因为我不知道足够的java来修复这个问题:/

private Timer SQTimer;
startButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {

        //Code that removes unrelated things from the frame

        final SquareObjects squaresToDisplay = new SquareObjects(x,y);//Creates the Object based on GUI width and height
        squaresToDisplay.setFocusable(true);
        squaresToDisplay.setVisible(true);//Allows it to be visible
        frame.add(squaresToDisplay);//Adds it to the frame
        SQTimer = new Timer(1000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e){
                squaresToDisplay.repaint();
                System.out.println("Repainted");
                }
        });
        System.out.println("Completed adding pixels");
        SQTimer.setRepeats(true);
        SQTimer.start();
    } 
});
    @Override
    protected void paintComponent(Graphics g){
        super.paintComponent(g);
        System.out.println("Beginning of paintComponent");
        System.out.println("Completed making the Graphics objects");
        for(int i = 0;i<(x*y)/64;i++){
        if(xInterceptLocation == 0){
            g.fillRect(xInterceptLocation, yInterceptLocation, 8, 8);
            xInterceptLocation += 8;
        }else{
            Color newColor = changingColors();
            g.setColor(newColor);
            g.fillRect(xInterceptLocation, yInterceptLocation, 8, 8);
            xInterceptLocation += 8;
            if(xInterceptLocation == 1920){
                xInterceptLocation = 0;
                yInterceptLocation += 8;
            }
        }
    }
}
专用定时器SQTimer;
addActionListener(新ActionListener()){
已执行的公共无效操作(操作事件e){
//从框架中删除不相关内容的代码
final SquareObjects squaresToDisplay=new SquareObjects(x,y);//基于GUI宽度和高度创建对象
squaresToDisplay.setFocusable(真);
squaresToDisplay.setVisible(true);//允许它可见
frame.add(squaresToDisplay);//将其添加到帧中
SQTimer=新计时器(1000,新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
squaresToDisplay.repaint();
System.out.println(“重新喷漆”);
}
});
System.out.println(“已完成添加像素”);
SQTimer.setRepeats(真);
SQTimer.start();
} 
});
@凌驾
受保护组件(图形g){
超级组件(g);
System.out.println(“部件开始”);
System.out.println(“完成图形对象的制作”);

对于(int i=0;i而言,问题在于yInterceptLocation从未设置回0,因此当程序重新绘制时,它继续添加8,从而使屏幕保持空白,因为它超出了帧边界

@Override
protected void paintComponent(Graphics g){
    super.paintComponent(g);
    System.out.println("Beginning of paintComponent");

    System.out.println("Completed making the Graphics objects");
    //yIntercept needs to be reinitialized when the repaint(); is called again
    if(yInterceptLocation == 1080){
        yInterceptLocation = 0;
    }

    for(int i = 0;i<(x*y)/64;i++){
            if(xInterceptLocation == 0){//If i == 0 then it wont add 8 first (thus preventing a gap)
            g.fillRect(xInterceptLocation, yInterceptLocation, 8, 8);
            xInterceptLocation += 8;
        }else{//Any other time we want to add 8 to space out the squares
            Color newColor = changingColors();
            g.setColor(newColor);
            g.fillRect(xInterceptLocation, yInterceptLocation, 8, 8);
            xInterceptLocation += 8;
            if(xInterceptLocation == 1920){//if xInterceptLocation = 1920 then it adds 8 to yIntercept and sets x to 0 (creating a new line)
                xInterceptLocation = 0;
                yInterceptLocation += 8;
            }
        }
    }
}
@覆盖
受保护组件(图形g){
超级组件(g);
System.out.println(“部件开始”);
System.out.println(“完成图形对象的制作”);
//再次调用repaint()时,需要重新初始化yIntercept
if(yInterceptLocation==1080){
yInterceptLocation=0;
}

对于(int i=0;创建图形对象数组没有必要也没有好处,因为数组中的每个项目都包含一个相同的图形对象。充其量这只是混淆。去掉数组。我删除了数组,它也做了相同的事情,但它不再创建数组。感谢您指出了超出计时器所有活动的原因nListener所做的是在组件上调用
repaint()
,这表明(并且您的代码确认)您的
paintComponent(…)
方法中有程序逻辑。我还建议您不要这样做,ActionListener更改类的状态,并且
paintComponent(…)
使用该状态来更改其绘图。您还需要在代码中加入调试语句,以查看它可能出错的地方。当您说Spready with debug语句时,您的意思是将系统输出放在代码的某些区域中,这些区域仅表示该区域中发生了什么?由于ActionListener更改了类的状态,因此n它第一次工作的原因是因为squareObject在类仍然有焦点时被实例化,但是当ActionListener被创建时,它会将焦点切换到另一个类并尝试重新绘制squareObject,但由于squareObject是另一个类的一部分,所以它无法重新绘制。这是否正确?编辑:一个问题:您似乎没有初始化您的在
paintComponent(…)
方法的开头截取