import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Kletki extends JFrame {
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JButton[][]knopki=new JButton[50][50];
JButton lab=new JButton();
int igrok =1;
int[][] pole=new int[50][50];
JLabel l1=new JLabel("for 1");
JLabel l2=new JLabel("for 2");
Kletki(){
super();
this.setLayout(null);
p1.setLayout(new GridLayout(50,50));
p1.setBounds(0,0,800,800);
p2.setBounds(800,0,400,400);
p2.setBackground(Color.pink);
p2.setLayout(null);
lab.setBounds(10,10,50,50);
lab.setBackground(Color.blue);
p2.add(lab);
l1.setBounds(10,100,50,50);
l1.setForeground(Color.red);
//l1.setBackground(Color.blue);
p2.add(l1);
l2.setBounds(10,200,50,50);
l2.setForeground(Color.blue);
//l2.setBackground(Color.blue);
p2.add(l2);
//public void class l1
// p2.setLayout(new GridLayout(,4));
//p2.setLayout(null);
//;
//p2.setBackground(Color.pink);
this.add(p1);
this.add(p2);
this.setResizable(true);
/* setArray();
pole[0][0]=1;
pole[49][49]=2;*/
setButtons();
}
public void setButtons(){
for(int i=0; i<knopki.length; i++)
for(int k=0; k<knopki.length; k++){
knopki[i][k]=new JButton();
p1.add(knopki[i][k]);
knopki[i][k].setActionCommand(""+i+","+k);
knopki[i][k].addActionListener(new AL());}
}
class AL implements ActionListener{
Color c, c2;
public void actionPerformed(ActionEvent ac){
String s=ac.getActionCommand();
String[]a=s.split(",");
int i=Integer.parseInt(a[0]);
int k=Integer.parseInt(a[1]);
if(pole[i][k]>0) return;
pole[i][k]=igrok;
boolean b=sosed(i,k);
if(b){
pole[i-1][k]=igrok;
pole[i+1][k]=igrok;
pole[i][k+1]=igrok;
pole[i][k-1]=igrok;
pole[i-1][k-1]=igrok;
pole[i+1][k-1]=igrok;
pole[i-1][k+1]=igrok;
pole[i+1][k+1]=igrok;
}
if(igrok==1) {
c=Color.blue;
//c2=new Color(100,100,255);
lab.setBackground(Color.red);
igrok=2;
}
else {
c=Color.red;
//c2=new Color(255,100,100);
lab.setBackground(Color.blue);
igrok=1;
}
knopki[i][k].setBackground(c);
if(b) kras(i, k, c);
}
}
public void kras(int i, int k, Color c){
knopki[i-1][k].setBackground(c);
knopki[i+1][k].setBackground(c);
knopki[i][k+1].setBackground(c);
knopki[i][k-1].setBackground(c);
knopki[i-1][k-1].setBackground(c);
knopki[i+1][k-1].setBackground(c);
knopki[i-1][k+1].setBackground(c);
knopki[i+1][k+1].setBackground(c);
}
public boolean sosed(int i, int k){
if(i>0) if(pole[i-1][k]==igrok) return true;
if(i<pole.length-1) if(pole[i+1][k]==igrok) return true;
if(pole[i][k+1]==igrok) return true;
if(pole[i][k-1]==igrok) return true;
return false;
}
public static void main(String[] args) {
Kletki m=new Kletki();
m.setVisible(true);
m.setSize(1200,820);
// TODO Auto-generated method stub
}
}
[свернуть]