CodeMasterOver9000
CodeMasterOver9000

Reputation: 11

Java - Timer Delay between actions

I have a (probably stupid) Question... I've been trying for hours to program a timer that pauses between commands, but I can't get it to work. It should look like a kind of "slot machine" effect. Have already tried a few things and am desperate. Could someone please help me or at least give me a hint what I'm doing wrong?

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Window implements ActionListener
{
    Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
    JFrame MAIN = new JFrame("Test");
    Integer[] ZufallsNUMBERen = { 1, 2, 3, 4 };
    String[] Names = { "Name1", "Name2", "Name3", "Name4" };
    JButton B_Start = new JButton();
    Integer NUMBER = 0;
    Timer timer;

    JLabel Name1 = new JLabel(Names[0]);
    JLabel Name2 = new JLabel(Names[1]);
    JLabel Name3 = new JLabel(Names[2]);
    JLabel Name4 = new JLabel(Names[3]);

    public static void main(String[] args)
    {
        new Window().window();

    }

    public void window()
    {
        MAIN.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        MAIN.setLayout(null);
        MAIN.setResizable(false);
        MAIN.setSize( 665, 519);
        MAIN.setForeground(Color.BLACK);
        MAIN.getContentPane().setBackground(Color.decode("#008080"));
        MAIN.setVisible(true);
        int x = (int) ((dimension.getWidth() - MAIN.getWidth()) / 2);
        int y = (int) ((dimension.getHeight() - MAIN.getHeight()) / 2 - 100);
        MAIN.setLocation(x,y);

        B_Start.setBounds(250, 110, 300, 300);


        Name1.setBounds(100,125,200,40);
        Name2.setBounds(100,200,200,40);
        Name3.setBounds(100,275,200,40);
        Name4.setBounds(100,350,200,40);
        Name1.setFont(new Font("SnowCaps", Font.BOLD, 30)); 
        Name2.setFont(new Font("SnowCaps", Font.BOLD, 30)); 
        Name3.setFont(new Font("SnowCaps", Font.BOLD, 30)); 
        Name4.setFont(new Font("SnowCaps", Font.BOLD, 30)); 


        MAIN.add(B_Start);
        MAIN.add(Name1);
        MAIN.add(Name2);
        MAIN.add(Name3);
        MAIN.add(Name4);

        B_Start.addActionListener(this);

        timer = new Timer(500, new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e){
                    NUMBER = NUMBER +1;

                    Slot1();
                    System.out.println(NUMBER);

                    Slot2();
                    System.out.println(NUMBER);

                    Slot3();
                    System.out.println(NUMBER);

                    Slot4();
                    System.out.println(NUMBER);
            }
        });
    }
    public void Slot1(){
        Name1.setText(Names[0]);
        Name2.setText(Names[1]);
        Name3.setText(Names[2]);
        Name4.setText(Names[3]);

        NUMBER = NUMBER +1;
    }
    public void Slot2() {
        Name1.setText(Names[3]);
        Name2.setText(Names[0]);
        Name3.setText(Names[1]);
        Name4.setText(Names[2]);

        NUMBER = NUMBER +1;
    }
    public void Slot3() {
        Name1.setText(Names[2]);
        Name2.setText(Names[3]);
        Name3.setText(Names[0]);
        Name4.setText(Names[1]);

        NUMBER = NUMBER +1;
    }
    public void Slot4() {
        Name1.setText(Names[1]);
        Name2.setText(Names[2]);
        Name3.setText(Names[3]);
        Name4.setText(Names[0]);

        NUMBER = NUMBER +1;
    }


    @Override
    public void actionPerformed(ActionEvent e) {

        if (e.getSource() == B_Start)
        {
            if(!timer.isRunning())
                timer.start();
            else
                timer.stop();
        }

    }

}

Thanks

Upvotes: 1

Views: 105

Answers (1)

ControlAltDel
ControlAltDel

Reputation: 35011

Use NUMBER (or another iterator variable) to break up the timing of your Slot method calls.

        public void actionPerformed(ActionEvent e){
                NUMBER = NUMBER +1;

                If (NUMBER.intValue() == 1) {
                  Slot1();
                  System.out.println(NUMBER);
                }
                If (NUMBER.intValue() == 2) {
                  Slot2();
                  System.out.println(NUMBER);
                }   
                ...
        }

Upvotes: 2

Related Questions