Advertisement
Guest User

PROGACHELOVECHESTVA

a guest
Jan 18th, 2020
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.67 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.Color;
  3. import java.awt.EventQueue;
  4.  
  5. import javax.swing.JFrame;
  6. import javax.swing.JPanel;
  7. import javax.swing.border.EmptyBorder;
  8. import javax.swing.JLabel;
  9. import javax.swing.JOptionPane;
  10. import javax.swing.JButton;
  11. import java.awt.GridLayout;
  12. import java.awt.event.MouseAdapter;
  13. import java.awt.event.MouseEvent;
  14. import java.awt.event.MouseMotionAdapter;
  15. import java.awt.event.MouseMotionListener;
  16.  
  17. import javax.swing.JTextField;
  18. import javax.swing.JComboBox;
  19. import javax.swing.ImageIcon;
  20. import java.awt.Font;
  21. import java.awt.event.ActionListener;
  22. import java.awt.event.ActionEvent;
  23.  
  24. public class pole extends JFrame {
  25. public int player;
  26. private JPanel contentPane;
  27. private int first;
  28. public int xx;
  29. public int yy;
  30. public int razm = 50;
  31. public JButton[][] knopki = new JButton[razm][razm];
  32. public int[][] map = new int[razm][razm];
  33. public int x , y;
  34. public int blueS , redS;
  35.  
  36.  
  37. public pole(int color) {
  38. player = color;
  39. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  40. setBounds(100, 100, 818, 709);
  41. contentPane = new JPanel();
  42. contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  43. setContentPane(contentPane);
  44. contentPane.setLayout(null);
  45.  
  46. JPanel panel = new JPanel();
  47. panel.setBounds(0, 0, 802, 600);
  48. contentPane.add(panel);
  49. panel.setLayout(new GridLayout(50, 50, 0, 0));
  50.  
  51. JLabel lblNewLabel = new JLabel("New label");
  52.  
  53.  
  54.  
  55.  
  56. lblNewLabel.setIcon(new ImageIcon("\\\\main\\RDP\\32\u041F\\\u043C\u0438\u0445\u0430\u0439\u043B\u043E\u0432\u0432\u0430\\Desktop\\\u041C\u0414\u041A0302\\KALKULATOR\\\u043A\u0430\u0440\u0442\u0438\u043D\u043A\u0438\\red.jpg"));
  57. lblNewLabel.setBounds(30, 611, 48, 48);
  58. contentPane.add(lblNewLabel);
  59.  
  60. JLabel label = new JLabel("New label");
  61. label.setIcon(new ImageIcon("\\\\main\\RDP\\32\u041F\\\u043C\u0438\u0445\u0430\u0439\u043B\u043E\u0432\u0432\u0430\\Desktop\\\u041C\u0414\u041A0302\\KALKULATOR\\\u043A\u0430\u0440\u0442\u0438\u043D\u043A\u0438\\blue.jpg"));
  62. label.setBounds(196, 611, 48, 48);
  63. contentPane.add(label);
  64.  
  65. JLabel red = new JLabel("0");
  66. red.setFont(new Font("Tahoma", Font.PLAIN, 20));
  67. red.setBounds(88, 611, 67, 48);
  68. contentPane.add(red);
  69.  
  70. JLabel blue = new JLabel("0");
  71. blue.setFont(new Font("Tahoma", Font.PLAIN, 20));
  72. blue.setBounds(255, 611, 67, 48);
  73. contentPane.add(blue);
  74.  
  75. JButton btnNewButton = new JButton("\u0417\u0430\u043A\u043E\u043D\u0447\u0438\u0442\u044C \u0438\u0433\u0440\u0443");
  76. btnNewButton.addActionListener(new ActionListener() {
  77. public void actionPerformed(ActionEvent arg0) {
  78. if (redS > blueS)
  79. {
  80. JOptionPane.showMessageDialog(contentPane, "Победили красные набрав " +redS +" очков" ,"Победа", JOptionPane.INFORMATION_MESSAGE);
  81. }
  82. else
  83. { if (redS < blueS)
  84. JOptionPane.showMessageDialog(contentPane, "Победили синие набрав " +blueS +" очков" ,"Победа", JOptionPane.INFORMATION_MESSAGE);
  85. }
  86. }
  87. });
  88. btnNewButton.setBounds(411, 624, 140, 23);
  89. contentPane.add(btnNewButton);
  90.  
  91. JLabel kost1 = new JLabel("New label");
  92. kost1.setFont(new Font("Tahoma", Font.PLAIN, 20));
  93. kost1.setBounds(605, 616, 48, 31);
  94. contentPane.add(kost1);
  95.  
  96. JLabel kost2 = new JLabel("New label");
  97. kost2.setFont(new Font("Tahoma", Font.PLAIN, 20));
  98. kost2.setBounds(664, 616, 48, 31);
  99. contentPane.add(kost2);
  100.  
  101. x = random();
  102. y = random();
  103. kost1.setText(x +"");
  104. kost2.setText(y +"");
  105. for (int i = 0; i< razm; i++)
  106. {
  107. for (int j = 0; j< razm; j++)
  108. {
  109. map[i][j] = 0;
  110. }
  111. }
  112.  
  113. for (int i = 0; i< knopki.length; i++)
  114. {
  115. for (int j = 0; j< knopki.length; j++)
  116. {
  117. knopki[i][j] = new JButton();
  118. panel.add(knopki[i][j]);
  119. knopki[i][j].setBackground(Color.WHITE);
  120. }
  121. }
  122.  
  123. for (int i = 0; i< knopki.length; i++)
  124. {
  125. for (int j = 0; j< knopki.length; j++)
  126. {
  127. knopki[i][j].addMouseMotionListener(new MouseMotionListener() {
  128.  
  129. public void mouseMoved(MouseEvent e) {
  130.  
  131. rebuild();
  132.  
  133. var elem1 = e.getComponent();
  134.  
  135. for (int i = 0; i< knopki.length; i++)
  136. {
  137. for (int j = 0; j< knopki.length; j++)
  138. {
  139. if(knopki[i][j].equals(elem1))
  140. {
  141. xx = i;
  142. yy = j;
  143.  
  144. }
  145. }
  146. }
  147.  
  148.  
  149.  
  150. for (int i = xx; i < xx+x; i++)
  151. {
  152. for (int j = yy; j< yy+y; j++)
  153. {
  154. switch(player)
  155. {
  156. case 1:
  157. knopki[i][j].setBackground(Color.RED);
  158. break;
  159. case 2:
  160. knopki[i][j].setBackground(Color.BLUE);
  161. break;
  162. }
  163. }
  164.  
  165. }
  166. }
  167.  
  168.  
  169. public void mouseDragged(MouseEvent arg0) {
  170.  
  171. }
  172. });
  173. }
  174. }
  175.  
  176.  
  177. for (int i = 0; i< knopki.length; i++)
  178. {
  179. for (int j = 0; j< knopki.length; j++)
  180. {
  181. knopki[i][j].addMouseListener(new MouseAdapter() {
  182. @Override
  183. public void mousePressed(MouseEvent e) {
  184.  
  185.  
  186. for (int i = xx; i < xx+x; i++)
  187. {
  188. for (int j = yy; j< yy+y; j++)
  189. {
  190. if(map[i][j] != 0)
  191. {
  192. return;
  193. }
  194. }
  195.  
  196. }
  197. int chislo = x*y;
  198. for (int i = xx; i < xx+x; i++)
  199. {
  200. for (int j = yy; j< yy+y; j++)
  201. {
  202. switch(player)
  203. {
  204. case 1:
  205. knopki[i][j].setBackground(Color.RED);
  206. map[i][j] = 1;
  207. break;
  208. case 2:
  209. knopki[i][j].setBackground(Color.BLUE);
  210. map[i][j] = 2;
  211. break;
  212. }
  213.  
  214.  
  215. }
  216.  
  217. }
  218.  
  219. switch(player)
  220. {
  221. case 1:
  222. redS = redS + x*y;
  223. red.setText(redS+"");
  224. break;
  225. case 2:
  226. blueS += x*y;
  227. blue.setText(blueS+"");
  228. break;
  229. }
  230. if(player == 1)
  231. {
  232. player = 2;
  233. }
  234. else
  235. {
  236. player = 1;
  237. }
  238. x = random();
  239. y = random();
  240. kost1.setText(x +"");
  241. kost2.setText(y +"");
  242.  
  243. }
  244. });
  245. }
  246. }
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255. if (color == 1)
  256. {
  257. first = 1;
  258. }
  259. else
  260. {
  261. first = 2;
  262. }
  263.  
  264.  
  265.  
  266.  
  267. }
  268. public void rebuild()
  269. {
  270. for (int i = 0; i< map.length; i++)
  271. {
  272. for (int j = 0; j< map.length; j++)
  273. {
  274. if (map[i][j] == 0)
  275. {
  276. knopki[i][j].setBackground(Color.WHITE);
  277. }else
  278. if (map[i][j] == 1)
  279. {
  280. knopki[i][j].setBackground(Color.RED);
  281. }else
  282. if (map[i][j] == 2)
  283. {
  284. knopki[i][j].setBackground(Color.BLUE);
  285. }
  286.  
  287. }
  288. }
  289. }
  290.  
  291. public int random()
  292. {
  293. int kub = (int) (Math.random()*6+1);
  294. return kub;
  295.  
  296.  
  297. }
  298. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement