knugi

Untitled

Oct 3rd, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.48 KB | None | 0 0
  1. package pl.knugi;
  2.  
  3. import java.awt.EventQueue;
  4.  
  5. import javax.swing.JFrame;
  6. import javax.swing.JMenuBar;
  7. import javax.swing.JMenu;
  8. import javax.swing.JMenuItem;
  9. import javax.swing.JPanel;
  10. import javax.swing.JLabel;
  11. import javax.swing.JSeparator;
  12. import javax.swing.SwingConstants;
  13. import javax.swing.JButton;
  14.  
  15. import java.awt.event.ActionEvent;
  16. import java.awt.event.ActionListener;
  17. import java.util.ArrayList;
  18. import java.util.List;
  19. import java.util.Random;
  20.  
  21. import javax.swing.JRadioButton;
  22. import javax.swing.ButtonGroup;
  23. import javax.swing.JTextArea;
  24. import javax.swing.JToggleButton;
  25.  
  26. public class AppStart{
  27.  
  28. private JFrame frame;
  29. private JPanel panel;
  30. private JTextArea textArea;
  31. private final ButtonGroup buttonGroup = new ButtonGroup();
  32. private boolean isStarted = false;
  33. private List<Integer> lista;
  34. private Random random;
  35. private PanelAction pAction;
  36. public static JToggleButton[][] table;
  37. /**
  38. * Launch the application.
  39. */
  40. public static void main(String[] args) {
  41. EventQueue.invokeLater(new Runnable() {
  42. public void run() {
  43. try {
  44. AppStart window = new AppStart();
  45. window.frame.setVisible(true);
  46. } catch (Exception e) {
  47. e.printStackTrace();
  48. }
  49. }
  50. });
  51. }
  52.  
  53. /**
  54. * Create the application.
  55. */
  56. public AppStart() {
  57. initialize();
  58. pAction = new PanelAction(this);
  59. random = new Random();
  60. }
  61.  
  62. /**
  63. * Initialize the contents of the frame.
  64. */
  65. private void initialize() {
  66. frame = new JFrame();
  67. frame.setBounds(100, 100, 1024, 800);
  68. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  69.  
  70. JMenuBar menuBar = new JMenuBar();
  71. frame.setJMenuBar(menuBar);
  72.  
  73. JMenu mnPlik = new JMenu("Plik");
  74. menuBar.add(mnPlik);
  75.  
  76. JMenuItem mntmNowaGra = new JMenuItem("Nowa gra");
  77. mnPlik.add(mntmNowaGra);
  78.  
  79. JMenuItem mntmWyjscie = new JMenuItem("Wyjscie");
  80. mnPlik.add(mntmWyjscie);
  81.  
  82. JMenu mnPomoc = new JMenu("Pomoc");
  83. menuBar.add(mnPomoc);
  84.  
  85. JMenuItem mntmAutor = new JMenuItem("Autor");
  86. mnPomoc.add(mntmAutor);
  87. frame.getContentPane().setLayout(null);
  88.  
  89. JSeparator separator = new JSeparator();
  90. separator.setOrientation(SwingConstants.VERTICAL);
  91. separator.setBounds(199, 0, 6, 740);
  92. frame.getContentPane().add(separator);
  93.  
  94. panel = new JPanel();
  95. panel.setBounds(200, 0, 808, 740);
  96. frame.getContentPane().add(panel);
  97. panel.setLayout(null);
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104. JRadioButton rdbtnx_1 = new JRadioButton("4x4");
  105. rdbtnx_1.setSelected(true);
  106. rdbtnx_1.setActionCommand(rdbtnx_1.getText());
  107. buttonGroup.add(rdbtnx_1);
  108. rdbtnx_1.setBounds(10, 67, 109, 23);
  109. frame.getContentPane().add(rdbtnx_1);
  110.  
  111. JRadioButton rdbtnx_2 = new JRadioButton("6x6");
  112. rdbtnx_2.setActionCommand(rdbtnx_2.getText());
  113. buttonGroup.add(rdbtnx_2);
  114. rdbtnx_2.setBounds(10, 93, 109, 23);
  115. frame.getContentPane().add(rdbtnx_2);
  116.  
  117. JRadioButton rdbtnx_3 = new JRadioButton("8x8");
  118. rdbtnx_3.setActionCommand(rdbtnx_3.getText());
  119. buttonGroup.add(rdbtnx_3);
  120. rdbtnx_3.setBounds(10, 119, 109, 23);
  121. frame.getContentPane().add(rdbtnx_3);
  122.  
  123. JButton btnNowaGra = new JButton("Nowa gra");
  124. btnNowaGra.addActionListener(new ActionListener() {
  125.  
  126. public void actionPerformed(ActionEvent e) {
  127. if(!isStarted){
  128. isStarted=true;
  129. clearPanel();
  130.  
  131. int a = Integer.parseInt(buttonGroup.getSelection().getActionCommand().substring(0, 1));
  132. textArea.setText(a+"");
  133. table = new JToggleButton[a][a];
  134. int x = 0;
  135. int y = 0;
  136. for(int i = 0; i<a; i++){
  137. for(int j=0;j<a;j++){
  138. table[i][j] = (JToggleButton) panel.add(new JToggleButton());
  139. table[i][j].setActionCommand(i+" "+j);
  140. table[i][j].setBounds(x, y, 65, 65);
  141. table[i][j].addActionListener(pAction);
  142. x+=70;
  143.  
  144. }
  145. x=0;
  146. y+=70;
  147.  
  148. }
  149. lista = new ArrayList<Integer>();
  150. int g=1;
  151. for(int i=0;i<(a*a)/2;i++){
  152. lista.add(g);
  153. lista.add(g);
  154. g++;
  155. }
  156. for(int i=0;i<a;i++){
  157. for(int j=0;j<a;j++){
  158. table[i][j].setName(getRandomFromList()+"");
  159. table[i][j].setText(table[i][j].getName());
  160. }
  161. }
  162. panel.validate();
  163. panel.repaint();
  164.  
  165. return;
  166. }
  167.  
  168. }
  169. });
  170. btnNowaGra.setBounds(10, 11, 89, 23);
  171. frame.getContentPane().add(btnNowaGra);
  172.  
  173. JSeparator separator_1 = new JSeparator();
  174. separator_1.setBounds(0, 145, 200, 1);
  175. frame.getContentPane().add(separator_1);
  176.  
  177. textArea = new JTextArea();
  178. textArea.setEditable(false);
  179. textArea.setBounds(10, 149, 184, 44);
  180. frame.getContentPane().add(textArea);
  181.  
  182. JLabel label = new JLabel("");
  183. label.setBounds(0, 0, 1008, 740);
  184. frame.getContentPane().add(label);
  185.  
  186. }
  187.  
  188. public void setAreaText(String s){
  189. this.textArea.setText(s);
  190. }
  191.  
  192.  
  193. public void clearPanel(){
  194. this.frame.getContentPane().remove(panel);
  195. panel = new JPanel();
  196. panel.setBounds(200, 0, 808, 740);
  197. frame.getContentPane().add(panel);
  198. panel.setLayout(null);
  199. panel.validate();
  200. panel.repaint();
  201. }
  202. private Integer getRandomFromList(){
  203. int index = random.nextInt(lista.size());
  204. int ret = lista.get(index);
  205. lista.remove(index);
  206. return ret;
  207. }
  208. public boolean checkWin(){
  209. if(isStarted){
  210. for(int i=0;i<table.length;i++){
  211. for(int j=0;j<table.length;j++){
  212. if(table[i][j].isEnabled()){
  213. return false;
  214. }
  215. }
  216. }
  217. isStarted=false;
  218. table=null;
  219. clearPanel();
  220. return true;
  221. }
  222. return false;
  223. }
  224. }
Advertisement
Add Comment
Please, Sign In to add comment