AskingLotsOfQuestions
AskingLotsOfQuestions

Reputation: 37

Why does my code not work on mac but will work on windows?

(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?

Upvotes: 1

Views: 155

Answers (1)

thorin9000
thorin9000

Reputation: 171

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");

Upvotes: 1

Related Questions