My widgets are not displayed in java (swing)

78 Views Asked by At

I'm new to java, and I have a bug that I've been trying to fix for hours.

I have the following code:

package MatchMe;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;

public class InterfazMatchMe extends JFrame{
    private ControllerMatchMe controller;
    private Boolean pressButton = false;
    private int SIZE_SQUARES_ARRAY = 8;
    private JPanel squaresArea;
    private Listener listener;
    private ImageIcon image;
    private JLabel square;
    private JButton match;
    private Timer timer;
    // Son importantes para que la clase 'Listener' sepa de estas
    private GridBagConstraints constraints;
    private Squares focusSquares[];
    private int dificult;
    private int id;
    
    InterfazMatchMe() {
        initGUI();
        this.setTitle("Match Me");
        this.setSize(800, 700); //800, 650
        this.setLocationRelativeTo(null);
        this.setResizable(false);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    
    public void initGUI() {
        this.getContentPane().setLayout(new GridBagLayout());
        constraints = new GridBagConstraints();
        
        listener = new Listener();
        controller = new ControllerMatchMe();
        squaresArea = new JPanel();
        generateButton();
        timer = new Timer(2000, listener);
        timer.start();
    }
    
    public void generateButton() {
        match = new JButton("MATCH");
        match.addActionListener(listener);
        constraints.gridx = 1;
        constraints.gridy = 3;
        constraints.gridwidth  = 1;
        constraints.gridheight = 1;
        constraints.fill = GridBagConstraints.NONE;
        add(match, constraints);
    }
    
    // this method does not work well
    public void game() {
        while(true) {
            this.dificult = controller.getDificult();
            Squares squares[] = controller.getSquares();
            Squares shuffleSquares[] = controller.shuffleArray(squares);
            Squares filteredSquares[] = controller.extractSpecificLength(shuffleSquares, dificult);
            while(true)
            {
                this.id = controller.changeElement(shuffleSquares, filteredSquares, dificult);
                this.focusSquares = controller.setFocus(id, filteredSquares, true);
                while(true) {
                    System.out.println(1);
                    generateSquaresArray(focusSquares, dificult, constraints);
                    break;
                }
                controller.setFocus(id, focusSquares, false);
                System.out.println(3);
                if(controller.getAciertos() % 3 == 0 && controller.getPermissionsChangeDificult()) {
                    controller.setPermissionsChangeDificult(false);
                    controller.setDificult(dificult+1);
                    break;
                }
                else if(controller.getMatch()) {
                    break;
                }
            }
        }
    }
    
    private void generateSquaresArray(Squares[] focusSquares, int dificult, GridBagConstraints constraints) {
        int x=0, y=0;
        
        for(int i=0; i<dificult; i++) {
            image = new ImageIcon("src/Images/"+focusSquares[i].getId()+".jpg");
            square = new JLabel(image);
            constraints.gridx = x;
            constraints.gridy = y;
            constraints.gridwidth  = 1;
            constraints.gridheight = 1;
            constraints.fill = GridBagConstraints.NONE;
            if(x == 3) {x = 0;y = 1;} else {x += 1;}
            squaresArea.add(square, constraints);
        }
        squaresArea.setPreferredSize(new Dimension(310, 180));
        constraints.gridx = 1;
        constraints.gridy = 1;
        constraints.gridwidth  = 2;
        constraints.gridheight = 2;
        constraints.fill = GridBagConstraints.NONE;
        add(squaresArea, constraints);
    }
    
    public void userInteraction(String answer) {
        controller.updateStatusGame(answer, focusSquares, dificult, id);
            
        if(controller.getGoodGame()) {
            System.out.println("\nBien hecho, has sumado 5 puntos\n");
        }
        else {
            System.out.println("\nMuy mal, tienes una vida menos\n");
            if(controller.getPlayerLives() == 0) {
                System.out.println("Game over !!!\n");
                System.out.print(controller.getStatisticsPlayer());
                System.exit(0);
            }
        }
    }
    
    private class Listener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent event) {
            System.out.println(2);
            if(event.getSource() == match) {
                pressButton = true;
                userInteraction("y");
                pressButton = false;
            }
            if(pressButton == false ) {
                userInteraction("n");
            }
        }
    }
}

In the code, there is a method that has this comment: this method does not work well

The problem is the following. Those System.out.print you see are repeating like they never got out of the loop.

On the other hand the widgets are not shown. Understanding by widget the "squares" that are generated in a JPanel called "squaresArea" and the "match button".

If I comment on the "game()" function, which is the one with this bug, the program behaves without any problem.

I ask for your help, I have already tried everything.

0

There are 0 best solutions below