import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.Random; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JRadioButton; import javax.swing.JScrollPane; import javax.swing.JTextArea; public class ButtonPanel extends JPanel{ private static int SIZE = 8; char nazwa='A'; private JPanel lButton, rButton, mButton; private JButton[][] left, right, mid; private char graczastatki[][]=new char[8][8]; private char komputerastatki[][]=new char[8][8]; private int naszestatki=0; private ArrayList gracz = new ArrayList<>(); private ArrayList komputer = new ArrayList<>(); private JTextArea nick; // nazwa Gracz/Komputer // private int turn; // przypisuje czyj jest ruch 1 = gracza, 0 = komputera private JRadioButton turnC, turnP; // ruch komputer/gracz public ButtonPanel(){ lButton= new JPanel(); lButton.setLayout(new GridLayout(SIZE,SIZE)); mButton = new JPanel(); mButton.setLayout(new GridLayout(SIZE,SIZE)); rButton = new JPanel(); rButton.setLayout(new GridLayout(SIZE,SIZE)); left = new JButton[SIZE][SIZE]; mid = new JButton[SIZE][SIZE]; right = new JButton[SIZE][SIZE]; for (int i = 0; i < SIZE; i++){ for (int j = 0; j < SIZE; j++){ left[i][j] = new JButton(""+nazwa+(j+1)); right[i][j] = new JButton(""+nazwa+(j+1)); right[i][j].setEnabled(false); left[i][j].setMargin(new Insets(0, 0, 0, 0)); left[i][j].setPreferredSize(new Dimension(50, 50)); right[i][j].setMargin(new Insets(0, 0, 0, 0)); right[i][j].setPreferredSize(new Dimension(50, 50)); left[i][j].addActionListener(new UstawianieStatkow(i,j)); right[i][j].addActionListener(new Atak(i,j)); gracz.add(left[i][j]); komputer.add(right[i][j]); lButton.add(left[i][j]); rButton.add(right[i][j]); } nazwa++; } //komputerastatki[1][1]='s'; mid[0][0] = new JButton("Ustaw swoje statki!"); mid[0][0].setMargin(new Insets(0, 0, 0, 0)); mid[0][0].setPreferredSize(new Dimension(250, 50)); mid[0][0].setVisible(true); mButton.add(mid[0][0]); JPanel ButtonPanel = new JPanel(); ButtonPanel.setLayout(new BorderLayout()); ButtonPanel.add(lButton, BorderLayout.LINE_START); ButtonPanel.add(mButton, BorderLayout.CENTER); ButtonPanel.add(rButton, BorderLayout.LINE_END); add(ButtonPanel); } @SuppressWarnings("unchecked") // private void initComponents() { setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 400, Short.MAX_VALUE) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 300, Short.MAX_VALUE) ); pack(); }// public class UstawianieStatkow implements ActionListener{ int a; int b; public UstawianieStatkow(int wiersz, int kolumna){ b=kolumna; a=wiersz; } @Override public void actionPerformed(ActionEvent e){ if(naszestatki!=5) { left[a][b].setBackground(Color.green); graczastatki[a][b]='s'; naszestatki++; if(naszestatki==5){ for(JButton button:gracz){ button.removeActionListener(this); } for(JButton button:komputer){ button.setEnabled(true); } losowanie(); } } } } public class Atak implements ActionListener{ int a; int b; public Atak(int wiersz, int kolumna){ b=kolumna; a=wiersz; } @Override public void actionPerformed(ActionEvent e){ if(komputerastatki[a][b]!='s'){ right[a][b].setBackground(Color.blue); right[a][b].removeActionListener(this); } if(komputerastatki[a][b]=='s'){ right[a][b].setBackground(Color.red); right[a][b].removeActionListener(this); } } } private void losowanie(){ int losA = 0; int losB = 0; int zliczajstatki=0; Random rand = new Random(); do{ losA = rand.nextInt(1) + 6; losB = rand.nextInt(1) + 6; if(komputerastatki[losA][losB] != 's'){ komputerastatki[losA][losB] = 's'; } zliczajstatki++; }while(zliczajstatki != 5); } private void setDefaultCloseOperation(int EXIT_ON_CLOSE) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } private Container getContentPane() { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } private void pack() { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } private void If(boolean b) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } } class Test{ public static void main(String args[]) { /* Set the Nimbus look and feel */ // /* 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(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } // /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { JFrame frame = new JFrame(); ButtonPanel test = new ButtonPanel(); frame.add(test); frame.setVisible(true); frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //new ActionFrame(); } }); } private static class NewJFrame { public NewJFrame() { } } } /* // private void initComponents() { setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 400, Short.MAX_VALUE) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 300, Short.MAX_VALUE) ); pack(); }// */ // Variables declaration - do not modify // End of variables declaration