JPanel bloqueado

2 respostas
fernandofhr

Estou montando um jogo de campo minado no java, o facil funcionou normal, mas o dificil está bloqueando metade da tela e os botões estão fora de ordem. Como arrumar isso?

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package campominado;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 *
 * @author Said
 */
public class CampoMinadoMedio extends frmModelo implements ActionListener {

    public JButton[] Botoes = new JButton[400];
    public int[] Vetor = new int[400];
    public JButton novo = new JButton("Novo Jogo");
    public JPanel grid = new JPanel();
    Container cp;
    public boolean fim = false;
    public Random rnd;

    public void CampoMinadoMedio2() {

        setVisible(true);
        setSize(1300, 700);
        setTitle("Campo Minado");
        centerJFrame(this);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);

        cp = getContentPane();
        cp.setLayout(null);

        grid.setBounds(5, 50, 600, 600);
        grid.setLayout(new GridLayout(30, 35, 3, 3));

        for (int i = 0; i < 400; i++) {
            Botoes[i] = new JButton("");
            Botoes[i].addActionListener(this);
            Botoes[i].setFocusable(false);
            grid.add(Botoes[i]);
        }

        novo.setBounds(200, 4, 100, 41);
        novo.setFocusable(false);
        novo.addActionListener(this);

        cp.add(grid);
        cp.add(novo);

        preencheVetor(Vetor);

    }

    public void preencheVetor(int[] Vetor) {

        int contador = 0, flag = 0;
        rnd = new Random();

        while (contador < 40) {
            contador = 0;

            for (int i = 0; i < 400; i++) {
                flag = rnd.nextInt(2);

                if (contador >= 40) {
                    flag = 0;
                }

                // REDUNDÂNCIA PARA AS "BOMBAS" SEREM BEM DISTRIBUÍDAS

                if (flag == 1) {
                    flag = rnd.nextInt(2);

                    if (flag == 1) {
                        flag = rnd.nextInt(2);

                        if (flag == 1) {
                            flag = rnd.nextInt(2);

                            if (flag == 1) {
                                contador += 1;
                            }
                        }
                    }
                }

                Vetor[i] = flag;
            }
        }
    }

    public void centerJFrame(JFrame frame) {

        Dimension paneSize = frame.getSize();
        Dimension screenSize = frame.getToolkit().getScreenSize();
        frame.setLocation((screenSize.width - paneSize.width) / 2, (screenSize.height - paneSize.height) / 2);
    }

    public void abreJogo() {

        for (int i = 0; i < 400; i++) {
            Botoes[i].doClick();
        }
    }

    public void actionPerformed(ActionEvent e) {

        if (e.getSource() == novo) {
            preencheVetor(Vetor);

            for (int i = 0; i < 400; i++) {
                Botoes[i].setText("");
                Botoes[i].setEnabled(true);
            }

            fim = false;
            JOptionPane.showMessageDialog(this, "Jogo Reiniciado!", "Novo Jogo", JOptionPane.INFORMATION_MESSAGE);
        }

        for (int i = 0; i < 400; i++) {
            if (e.getSource() == Botoes[i]) {

                if (Vetor[i] == 0) {
                    int contador = 0;
                    contador = getBombas(i);

                    if (!fim) {

                        if (contador > 0) {
                            Botoes[i].setText("" + contador);
                            Botoes[i].setEnabled(false);
                        } else {
                            explodir(i);
                        }

                    } else {

                        if (contador > 0) {
                            Botoes[i].setText("" + contador);
                        }

                        Botoes[i].setEnabled(false);

                    }
                } else {

                    if (Vetor[i] == 1) {
                        Botoes[i].setText("X");

                        if (!fim) {
                            JOptionPane.showMessageDialog(null, "BOOOM!!!");
                            fim = true;
                            abreJogo();
                        }
                    }
                }
            }
        }
    }

    public void explodir(int i) {

        int contador = 0;
        Botoes[i].setEnabled(false);

        if (i + 1 < 400 & i % 20 != 9) {
            contador = getBombas(i + 1);

            if (contador == 0) {

                if (Botoes[i + 1].isEnabled() == true) {
                    Botoes[i + 1].doClick();
                }
            }
        }

        if (i - 1 >= 0 & i % 20 != 0) {
            contador = getBombas(i - 1);

            if (contador == 0) {

                if (Botoes[i - 1].isEnabled() == true) {
                    Botoes[i - 1].doClick();
                }
            }
        }

        if (i - 20 >= 0) {
            contador = getBombas(i - 20);

            if (contador == 0) {

                if (Botoes[i - 20].isEnabled() == true) {
                    Botoes[i - 20].doClick();
                }
            }
        }

        // Verifica bomba abaixo

        if (i + 20 < 400) {
            contador = getBombas(i + 20);

            if (contador == 0) {

                if (Botoes[i + 20].isEnabled() == true) {
                    Botoes[i + 20].doClick();
                }
            }
        }
    }

    public int getBombas(int i) {

        int contador = 0;

        // Verifica bomba à direita

        if (i + 1 < 400 & i % 20 != 9) {

            if (Vetor[i + 1] == 1) {
                contador += 1;
            }
        }

        // Verifica bomba à esquerda

        if (i - 1 >= 0 & i % 20 != 0) {

            if (Vetor[i - 1] == 1) {
                contador += 1;
            }
        }

        // Verifica bomba acima

        if (i - 20 >= 0) {

            if (Vetor[i - 20] == 1) {
                contador += 1;
            }
        }

        // Verifica bomba abaixo

        if (i + 20 < 400) {

            if (Vetor[i + 20] == 1) {
                contador += 1;
            }
        }

        // Verifica bomba à esquerda acima

        if (i - 21 >= 0 & i % 20 != 0) {

            if (Vetor[i - 21] == 1) {
                contador += 1;
            }
        }

        // Verifica bomba à direita acima

        if (i - 9 >= 0 & i % 20 != 9) {

            if (Vetor[i - 9] == 1) {
                contador += 1;
            }
        }

        // Verifica bomba à esquerda abaixo

        if (i + 9 < 400 && i % 20 != 0) {

            if (Vetor[i + 9] == 1) {
                contador += 1;
            }
        }

        // Verifica bomba à direita abaixo

        if (i + 21 < 400 && i % 20 != 9) {

            if (Vetor[i + 21] == 1) {
                contador += 1;
            }
        }

        return contador;

    }
//        public static void main (String args []) {
//
//               CampoMinado campo = new CampoMinado ();
//               campo.setVisible (true);
//
//        }
}

2 Respostas

T

Estava tentando ajudar mas faltou a classe frmModelo
A Classe CmpoMinadoMedio extende a frmModelo

poderia me mandar o restante do código?
Se quiser pode ser via mp

abraço

fernandofhr

Segue ela

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package campominado;

import javax.swing.JFrame;

/**
 *
 * @author Fernando
 */
public class frmModelo extends javax.swing.JFrame {

    /**
     * Creates new form frmModelo
     */
    public frmModelo() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenu3 = new javax.swing.JMenu();
        jMenuItem4 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jMenu1.setText("Nivel");

        jMenuItem1.setText("Facil");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuItem2.setText("Médio");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem2);

        jMenuItem3.setText("Dificíl");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem3);

        jMenuBar1.add(jMenu1);

        jMenu3.setText("Ferramentas");

        jMenuItem4.setText("Sair");
        jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem4ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem4);

        jMenuBar1.add(jMenu3);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 1500, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 779, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>                        

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {                                           
       CampoMinadoMedio medio = new CampoMinadoMedio ();
       medio.CampoMinadoMedio2();
       setVisible(false);
    }                                          

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        CampoMinado facil = new CampoMinado ();
        facil.CampoMinadoFacil();
        setVisible(false);
    }                                          

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        CampoMinadoDificil dificil = new CampoMinadoDificil ();
        dificil.CampoMinadoDificil2();
        setVisible(false);
    }                                          

    private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {                                           
       System.exit(0);
    }                                          

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(frmModelo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(frmModelo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(frmModelo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(frmModelo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new frmModelo().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                     
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    // End of variables declaration                   
}
Criado 8 de novembro de 2012
Ultima resposta 9 de nov. de 2012
Respostas 2
Participantes 2