如何更改计时器内的颜色

时间:2013-06-23 17:46:29

标签: java swing timer paintcomponent

以下是我关注的代码部分,当我尝试使用Timer再次更改squaresToDisplay对象的颜色时,它会使框架变为白色(空白),但它只能运行一次。因此,当我运行这段代码时,它将执行我想要的一次,然后将屏幕留空。我想知道是什么原因造成的。我自己的假设是,当我启动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;
            }
        }
    }
}

为了澄清,这些方法是在单独的类中,第一个是在一个名为GUI的类中,而第二个是在一个名为SquareObjects的类中

1 个答案:

答案 0 :(得分:1)

问题是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;
            }
        }
    }
}

HoverCraft Full Of Eels的信用,因为注意到它没有重新初始化