Facebook
From Cobalt Frog, 5 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 236
  1. import java.awt.BorderLayout;
  2. import java.awt.Color;
  3. import java.awt.Container;
  4. import java.awt.Dimension;
  5. import java.awt.FlowLayout;
  6. import java.awt.GridLayout;
  7. import java.awt.Insets;
  8. import java.awt.event.ActionEvent;
  9. import java.awt.event.ActionListener;
  10. import java.util.ArrayList;
  11. import java.util.Random;
  12. import javax.swing.JButton;
  13. import javax.swing.JFrame;
  14. import javax.swing.JPanel;
  15. import javax.swing.JRadioButton;
  16. import javax.swing.JScrollPane;
  17. import javax.swing.JTextArea;
  18.          
  19.         public class ButtonPanel extends JPanel{
  20.            
  21.             private static int SIZE = 8;
  22.             private JPanel lButton, rButton, mButton;
  23.             private JButton[][] left, right, mid;
  24.             private int naszestatki=0;
  25.             private ArrayList<JButton> gracz = new ArrayList<>();
  26.             private ArrayList<JButton> komputer = new ArrayList<>();  
  27.             private JTextArea nick; // nazwa Gracz/Komputer
  28.             private int turn; // przypisuje czyj jest ruch 1 = gracza, 0 = komputera
  29.             private JRadioButton turnC, turnP; // ruch komputer/gracz
  30.            
  31.            
  32.             public ButtonPanel(){
  33.          
  34.                 lButton= new JPanel();
  35.                 lButton.setLayout(new GridLayout(SIZE,SIZE));
  36.                
  37.                 mButton = new JPanel();
  38.                 mButton.setLayout(new GridLayout(SIZE,SIZE));
  39.                
  40.                 rButton = new JPanel();
  41.                 rButton.setLayout(new GridLayout(SIZE,SIZE));
  42.                
  43.                 left = new JButton[SIZE][SIZE];
  44.                 mid = new JButton[SIZE][SIZE];
  45.                 right = new JButton[SIZE][SIZE];
  46.                
  47.                 /*Random rand = new Random();
  48.                 turn = rand.nextInt(2) + 1;
  49.                */
  50.                
  51.                
  52.                 for (int i = 0; i < SIZE; i++)
  53.                 {
  54.                     for (int j = 0; j < SIZE; j++)
  55.                     {                
  56.                         left[i][j] = new JButton();
  57.                         right[i][j] = new JButton();
  58.                         right[i][j].setEnabled(false);
  59.                         left[i][j].setMargin(new Insets(0, 0, 0, 0));
  60.                         left[i][j].setPreferredSize(new Dimension(50, 50));
  61.                         right[i][j].setMargin(new Insets(0, 0, 0, 0));
  62.                         right[i][j].setPreferredSize(new Dimension(50, 50));
  63.                         left[i][j].addActionListener(new ButtonPressed(i,j));
  64.                         //right[i][j].addActionListener(new ButtonPressed(i,j));  #tutaj musi być inny actionlistener
  65.                         gracz.add(left[i][j]);
  66.                         komputer.add(right[i][j]);
  67.                         lButton.add(left[i][j]);
  68.                         rButton.add(right[i][j]);
  69.                         mid[0][0] = new JButton("Wybierz pola dla swoich statków");
  70.                         mid[0][0].setEnabled(false);
  71.                     }
  72.                 }  
  73.                
  74.                        /* if((turn == 1) && (naszestatki==5)){
  75.                             mid[0][0] = new JButton("Kolejka Gracza");
  76.                             mid[0][0].setEnabled(false);
  77.                         }
  78.                         else if((turn == 2) && (naszestatki==5)){
  79.                             mid[0][0] = new JButton("Kolejka Komputera");
  80.                             mid[0][0].setEnabled(false);
  81.                         }
  82.                     */
  83.                         mid[0][0].setMargin(new Insets(0, 0, 0, 0));
  84.                         mid[0][0].setPreferredSize(new Dimension(250, 50));
  85.                         mid[0][0].setVisible(true);
  86.                         mButton.add(mid[0][0]);
  87.                        
  88.                         JPanel ButtonPanel = new JPanel();
  89.                         ButtonPanel.setLayout(new BorderLayout());
  90.                         ButtonPanel.add(lButton, BorderLayout.LINE_START);
  91.                         ButtonPanel.add(mButton, BorderLayout.CENTER);
  92.                         ButtonPanel.add(rButton, BorderLayout.LINE_END);
  93.                         add(ButtonPanel);  
  94.                
  95.                        
  96.             }  
  97.                
  98.          
  99.             @SuppressWarnings("unchecked")
  100.             // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  101.             private void initComponents() {
  102.          
  103.                 setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  104.          
  105.                 javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  106.                 getContentPane().setLayout(layout);
  107.                 layout.setHorizontalGroup(
  108.                     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  109.                     .addGap(0, 400, Short.MAX_VALUE)
  110.                 );
  111.                 layout.setVerticalGroup(
  112.                     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  113.                     .addGap(0, 300, Short.MAX_VALUE)
  114.                 );
  115.          
  116.                 pack();
  117.             }// </editor-fold>                        
  118.             public class ButtonPressed implements ActionListener{
  119.                
  120.             int a;
  121.             int b;
  122.            
  123.             public ButtonPressed(int row, int column){
  124.                 b=column;
  125.                 a=row;
  126.                
  127.             }
  128.             @Override
  129.             public void actionPerformed(ActionEvent e){
  130.                 if(naszestatki!=5)
  131.                 {
  132.                     left[a][b].setEnabled(false);
  133.                     naszestatki++;
  134.                     if(naszestatki==5){
  135.                        for(JButton button:gracz){
  136.                        button.setEnabled(false);
  137.                        }
  138.                        for(JButton button:komputer){
  139.                        button.setEnabled(true);
  140.                        
  141.                        
  142.                        }
  143.                     }
  144.                 }
  145.                
  146.                 //KOLEJKA GRACZ / KOMPUTER
  147.                
  148.                 if((turn == 1) && (naszestatki==5)){
  149.                             mid[0][0] = new JButton("Kolejka Gracza");
  150.                             mid[0][0].setEnabled(false);
  151.                         }
  152.                         else if((turn == 2) && (naszestatki==5)){
  153.                             mid[0][0] = new JButton("Kolejka Komputera");
  154.                             mid[0][0].setEnabled(false);
  155.                            
  156.                         }
  157.                
  158.                
  159.                 }
  160.             }
  161.        
  162.            
  163.          
  164.             private void setDefaultCloseOperation(int EXIT_ON_CLOSE) {
  165.                 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  166.             }
  167.          
  168.             private Container getContentPane() {
  169.                 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  170.             }
  171.          
  172.             private void pack() {
  173.                 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  174.             }
  175.          
  176.             private void If(boolean b) {
  177.                 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  178.             }
  179.          
  180.          
  181.          
  182.             // Variables declaration - do not modify                    
  183.             // End of variables declaration                  
  184.         }
  185.        
  186.          
  187.         class Test{
  188.             public static void main(String args[]) {
  189.                 /* Set the Nimbus look and feel */
  190.                 //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  191.                 /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  192.                  * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  193.                  */
  194.                 try {
  195.                     for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  196.                         if ("Nimbus".equals(info.getName())) {
  197.                             javax.swing.UIManager.setLookAndFeel(info.getClassName());
  198.                             break;
  199.                         }
  200.                     }
  201.                 } catch (ClassNotFoundException ex) {
  202.                     java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  203.                 } catch (InstantiationException ex) {
  204.                     java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  205.                 } catch (IllegalAccessException ex) {
  206.                     java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  207.                 } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  208.                     java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  209.                 }
  210.                 //</editor-fold>
  211.          
  212.                 /* Create and display the form */
  213.                 java.awt.EventQueue.invokeLater(new Runnable() {
  214.                     public void run() {
  215.                         JFrame frame = new JFrame();
  216.                         ButtonPanel test = new ButtonPanel();
  217.                         frame.add(test);
  218.                         frame.setVisible(true);
  219.                         frame.pack();
  220.                         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  221.                        
  222.                     }
  223.                 });
  224.             }
  225.          
  226.             private static class NewJFrame {
  227.          
  228.                 public NewJFrame() {
  229.                 }
  230.             }
  231.         }
  232.  
  233. /*
  234.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  235.     private void initComponents() {
  236.  
  237.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  238.  
  239.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  240.         getContentPane().setLayout(layout);
  241.         layout.setHorizontalGroup(
  242.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  243.             .addGap(0, 400, Short.MAX_VALUE)
  244.         );
  245.         layout.setVerticalGroup(
  246.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  247.             .addGap(0, 300, Short.MAX_VALUE)
  248.         );
  249.  
  250.         pack();
  251.     }// </editor-fold>                        
  252. */
  253.  
  254.  
  255.  
  256.     // Variables declaration - do not modify                    
  257.     // End of variables declaration                  
  258.  
  259.  
  260.  
  261.  
  262.