0

So I'm making this game that gets you to remember a pattern and recreate it. The graphics work good the first time but does not work the second. I tried using Swing Timer but It didn't work for me (Maybe I am not using it right, regardless I still need help)

public static void main(String args[]) {
    frame.getContentPane().setBackground(new Color(255, 218, 185));
    frame.setBounds(100, 100, 400, 600);
    frame.setResizable(false);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    Counter = 0;
    //
    JButton btnScore = new JButton("Time Left: ");
    btnScore.setHorizontalAlignment(SwingConstants.LEFT);
    btnScore.setFont(new Font("Tahoma", Font.PLAIN, 26));
    //
    btnScore.setBounds(86, 113, 207, 73);
    frame.getContentPane().add(btnScore);
    //
    JButton btnHighScore = new JButton("High Score: ");
    btnHighScore.setHorizontalAlignment(SwingConstants.LEFT);
    btnHighScore.setFont(new Font("Tahoma", Font.PLAIN, 16));
    btnHighScore.setBounds(86, 455, 207, 50);
    frame.getContentPane().add(btnHighScore);
    //

    Scorebutton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

        }
    });
    //
    Scorebutton.setHorizontalAlignment(SwingConstants.LEFT);
    Scorebutton.setFont(new Font("Tahoma", Font.PLAIN, 16));
    Scorebutton.setBounds(86, 398, 207, 50);
    frame.getContentPane().add(Scorebutton);
    //
    A.setBackground(Color.GRAY);
    A.setContentAreaFilled(false);
    A.setOpaque(true);
    //
    A.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (code[Counter] == 1) {

                Score++;

            } else {
                End();
            }
            Counter++;
            if (Counter == Size) {
                Size++;
                n++;
                frame.setVisible(true);
                main(args);
            }
        }
    });
    A.setBounds(86, 187, 89, 73);
    frame.getContentPane().add(A);
    //
    B.setBackground(Color.GRAY);
    B.setContentAreaFilled(false);
    B.setOpaque(true);
    B.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            if (code[Counter] == 2) {

                Score++;
            } else {
                End();
            }
            Counter++;
            if (Counter == Size) {
                Size++;
                n++;
                frame.setVisible(true);
                main(args);
            }
        }
    });
    //
    B.setBounds(204, 187, 89, 73);
    frame.getContentPane().add(B);
    //
    C.setBackground(Color.GRAY);
    C.setContentAreaFilled(false);
    C.setOpaque(true);
    C.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            if (code[Counter] == 3) {

                Score++;
            } else {
                End();
            }
            Counter++;
            if (Counter == Size) {
                Size++;
                n++;
                frame.setVisible(true);
                main(args);
            }
        }
    });
    //

    C.setBounds(86, 286, 89, 73);
    frame.getContentPane().add(C);
    //
    D.setBackground(Color.GRAY);
    D.setContentAreaFilled(false);
    D.setOpaque(true);
    D.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            if (code[Counter] == 4) {

                Score++;

            } else {
                End();
            }
            Counter++;

        }
    });
    //
    D.setBounds(204, 286, 89, 73);
    frame.getContentPane().add(D);
    //
    frame.setVisible(true);
    //

    try {

        generate();
    } catch (InterruptedException e1) {
    }
}

public static void generate() throws InterruptedException {

    Counter = 0;

    rn = new Random();
    while (Counter != Size) {
        int Number = rn.nextInt(4) + 1;
        code[Counter] = (Number);
        Counter++;
    }
    Counter = 0;
    while (Counter != Size) {
        System.out.println(code[Counter]);
        Counter++;
    }
    Counter = 0;
    Show();

}
public static int x = 0, z = 0;

public static void Show() throws InterruptedException {

    timer1 = new Timer(250, new ActionListener() {// Timer 4 seconds
        public void actionPerformed(ActionEvent e) {
            if (Counter == Size) {
                Size++;
                n++;
                frame.setVisible(true);
                main(null);
            }

        }

    });
    if (Size != Counter) {

        Go();

    }
    if (Size == Counter) {

        Counter = 0;
        timer1.start();
    }

}`public static void Go() throws InterruptedException {
    if (code[Counter] == 1) {
        System.out.println("worked");
        Thread.sleep(500);

        A.setBackground(Color.RED);
        Thread.sleep(500);

        A.setBackground(Color.GRAY);
        Counter++;

    }
    if (code[Counter] == 2) {
        System.out.println("worked");
        Thread.sleep(500);

        B.setBackground(Color.RED);
        Thread.sleep(500);

        B.setBackground(Color.GRAY);
        Counter++;
    }
    if (code[Counter] == 3) {
        System.out.println("worked");

        Thread.sleep(500);
        C.setBackground(Color.RED);
        Thread.sleep(500);
        C.setBackground(Color.GRAY);
        Counter++;
    }
    if (code[Counter] == 4) {
        System.out.println("worked");

        Thread.sleep(500);
        D.setBackground(Color.RED);
        Thread.sleep(500);
        D.setBackground(Color.GRAY);

        Counter++;
    }
    Show();
}`
  • 3
    1) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Jun 15 '17 at 18:25
  • 4
    .. 3) Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it is an `UPPER_CASE_CONSTANT`) and use it consistently. – Andrew Thompson Jun 15 '17 at 18:26
  • 2
    4) Also along with the Java nomenclature as suggested by @AndrewThompson above, variable names should be descriptive `A.setBackground(Color.GRAY);` what is `A`? 5) Don't make everything static, learn what's its [real purpose](https://stackoverflow.com/questions/413898/what-does-the-static-keyword-do-in-a-class), it's not a magic "cross method" word that lets you call methods from main... 6) Talking about `main`, your program is mainly a `main` method, structure it better so it's easier to understand – Frakcool Jun 15 '17 at 18:41
  • The problem might be related to your failure to run the GUI on the Event Dispatch Thread (EDT), as explained in the Java Tutorial. It's hard to be sure given the naming issues alluded to by others. – Lew Bloch Jun 15 '17 at 19:48
  • Don't call `main` inside the code called by `main`, by the way. – Lew Bloch Jun 15 '17 at 19:50

0 Answers0