Skip to content
Advertisement

Alternating between even and odd elements in Array with GUI

I’m trying to work with more GUI stuff but I’m having problem with something. I have an array of JLabels. Each of them contain 1 number from 0 to 7. I’m making the numbers “light up” by changing the background color from black to green. Is there any way to make all the even numbers “light up” while keeping all the odd numbers dark and then vice versa? I tried using a timer but my algorithm isn’t working as it should. Below is the code for the method that configures the timer. Thanks

    public void configureAlternatingTimer() {
    if (this.timer != null) {
        this.timer.stop();
    }

    this.timer = new Timer(100, new ActionListener() {
        public void actionPerformed(ActionEvent evt) {

            for (int i = 0; i <= 8; i++) {
                if (i == 0 || i == 2 || i == 4 || i == 6) {
                    lights[1].setBackground(Color.black);
                    lights[3].setBackground(Color.black);
                    lights[5].setBackground(Color.black);
                    lights[7].setBackground(Color.black);
                    lights[i].setBackground(Color.green);
                }
                if (i == 1 || i == 3 || i == 5 || i == 7) {
                    lights[0].setBackground(Color.black);
                    lights[2].setBackground(Color.black);
                    lights[4].setBackground(Color.black);
                    lights[6].setBackground(Color.black);
                    lights[i].setBackground(Color.green);
                }
                if(i==8) {
                    return;
                }
            }

        }

    });
    this.timer.start();

}

Also, I’m trying to simulate a “larson scanner” which would light up to 7 then go back down to 0 then repeat. I can get it to go from 0 to 7 it’s just the going back down part that I’m having trouble with. Thanks

Advertisement

Answer

Drop the for-loop, it’s preventing the Event Dispatching Thread from processing repaint requests

Instead, each time that the actionPerformed method is called, update some kind of counter and then take action on it, for example…

this.timer = new Timer(100, new ActionListener() {
    private int sequence = 0;
    public void actionPerformed(ActionEvent evt) {
        if (sequence % 2 == 0) {
            lights[1].setBackground(Color.black);
            lights[3].setBackground(Color.black);
            lights[5].setBackground(Color.black);
            lights[7].setBackground(Color.black);
            lights[sequence].setBackground(Color.green);
        } else {
            lights[0].setBackground(Color.black);
            lights[2].setBackground(Color.black);
            lights[4].setBackground(Color.black);
            lights[6].setBackground(Color.black);
            lights[sequence].setBackground(Color.green);
        }
        
        sequence++;
        if (sequence > 7) {
            // This seems to be important...?
        }
    }
});

Updated based on comments

This should show all odds or all evens…

Timer timer = new Timer(500, new ActionListener() {
    private int sequence = 0;
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println(sequence + "; " + (sequence % 2));

        for (int index = 0; index < lights.length; index++) {

            if (index % 2 == 0 && sequence % 2 == 0 || index % 2 != 0 && sequence % 2 != 0) {
                lights[index].setBackground(Color.GREEN);
            } else {
                lights[index].setBackground(Color.BLACK);
            }

        }

        sequence++;
        if (sequence > 7) {
            sequence = 0;
        }
    }
});
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement