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