1

(general idea of what I'm doing and what I have)

I am trying to create a simple game where the user will need to guess the right spots in a 5x5 square grid based on luck. I would like to have the program create random spots (approx. 3-5) where the user will need to find each spot. So far I have the overall layout of the frame with a "life" counter that will go down every time the user clicks on the wrong spot. Temporarily, I hard-coded 3 spots the user will need to find named "hit". I'm still working on a way to implement a random number system where the correct spots will be made at random. This code is not complete so don't mind empty methods.

(Here is my code, main method class below this Display class)

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

public class Display implements ActionListener{

    int lives = 3;

    int[] hit = {10,2,22};
    JFrame frame = new JFrame();
    JPanel tile_panel = new JPanel();
    JButton[] button = new JButton[25];
    JTextField title = new JTextField();
    JTextField life_label = new JTextField();
    JLabel life_counter = new JLabel();
   

    public Display(){
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600,600);
        frame.getContentPane().setBackground(Color.GRAY);
        frame.setLayout(null);


        title.setBounds(0,0,600,75);
        title.setBackground(Color.GRAY);
        title.setForeground(Color.GREEN);
        title.setFont(new Font("Ink Free", Font.BOLD, 50));
        title.setEditable(false);
        title.setHorizontalAlignment(JTextField.CENTER);
        title.setBorder(null);
        title.setText("Sequence Game");

        life_label.setBounds(480,260,100,25);
        life_label.setBackground(Color.GRAY);
        life_label.setForeground(Color.GREEN);
        life_label.setFont(new Font("Roboto",Font.PLAIN,25));
        life_label.setText("Lives: ");
        life_label.setBorder(null);
        life_label.setEditable(false);

        life_counter.setBounds(500,300,100,25);
        life_counter.setForeground(Color.GREEN);
        life_counter.setFont(new Font("Roboto",Font.PLAIN,25));
        life_counter.setText(String.valueOf(lives));

        
        tile_panel.setBounds(25,120,400,400);
        tile_panel.setBackground(Color.GRAY);
        tile_panel.setLayout(new GridLayout(5,5, 1,1));

        for (int i = 0; i < button.length; i++){
            button[i] = new JButton();
            button[i].setSize(80,80);
            button[i].addActionListener(this);

            tile_panel.add(button[i]);
        }

        frame.add(tile_panel);
        frame.add(life_label);
        frame.add(life_counter);
        frame.add(title);
        frame.setVisible(true);


    }

    @Override
    public void actionPerformed(ActionEvent e) {

        boolean correct = false;

        for(int i = 0; i < button.length; i++){
            if (e.getSource() == button[i]){
                for (int k = 0; k < hit.length; k++){
                    if (hit[k] == i){
                        button[i].setBackground(Color.GREEN);
                        correct = true;
                    } 
                }
                if (correct == false){
                    button[i].setBackground(Color.RED);
                    lives--;
                    life_counter.setText(String.valueOf(lives));
                }
                i = button.length;
            }
        }
    }

    public void results(){

    }

}


    public class Main{

    public static void main(String[] args){
        new Display();
    }

}

However, my problem is that the code I currently have doesn't work on Mac. For example, my font of the words don't show up properly, and most importantly my tiles don't change color like it should be doing. Funny enough, my code can only run properly on a Windows device.

Is there anyone that knows the reason for this?

  • 2
    I'd guess your game crashes because it can't find the fonts on mac? How do you run this on Mac? Start it from command line and see if you get stacktraces printed. Also GUI should be initialized on the EDT, i.e., `SwingUtilities.invokeLater(() -> new Display());`. – akarnokd Jun 22 '22 at 17:17
  • 2
    The documentation for `Font` tells us that if you refer to a specific font (vs pre-defined font family), and the local machine can't find that font, you'll get whatever is defined for font.DIALOG instead. That sounds like what you're experiencing. https://docs.oracle.com/en/java/javase/18/docs/api/java.desktop/java/awt/Font.html#%3Cinit%3E(java.lang.String,int,int) – Gus Jun 22 '22 at 17:26
  • Sorry, I'm still a new coder so I'm just confused about how would the function of EDT help my program? – AskingLotsOfQuestions Jun 22 '22 at 18:23
  • 1
    Good Q/A for EDT: https://stackoverflow.com/questions/11825281/java-swing-running-on-edt – Gus Jun 22 '22 at 19:06

1 Answers1

1

The tiles aren't changing color on Mac because of the UI's default look and feel. I have the same issue on my Mac due to com.apple.laf.AquaLookAndFeel. You can switch to a cross-platform L&F like Metal: UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");

thorin9000
  • 171
  • 1
  • 6