3

Here is the portion of my code I'm concerned with, When I try to change the color of squaresToDisplay object again using the Timer, it makes the frame white (blank) but it works only 1 time. So when I run this piece of code, it will do what I want 1 time then make the screen blank. I am wondering what causes this specifically. My own assumption is that I might be blocking the EDT when I start the SQTimer, in which case I'm at a loss because I don't know enough java to fix this :/

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;
            }
        }
    }
}

Just for clarification, these to methods are in seperate classes, the first one is in a class called GUI while the second one is in a class called SquareObjects

4

1 に答える 1

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 の功績

于 2013-06-23T18:56:49.020 に答える