Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. import java.awt.*;
  2. import javax.swing.*;
  3. import java.awt.event.*;
  4. import java.util.Random;
  5.  
  6. class buttonGame
  7. {
  8. public static void main(String[] args) {
  9. buttonGameFrame myFrame = new buttonGameFrame();
  10. myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  11. }
  12. }
  13.  
  14. class buttonGameFrame extends JFrame implements ActionListener {
  15. JButton[][] buttons = new JButton[5][4];
  16. JPanel mPanel = new JPanel();
  17. JPanel bPanel = new JPanel();
  18. JPanel leftPanel = new JPanel();
  19. JPanel rightPanel = new JPanel();
  20. JPanel bottomPanel = new JPanel();
  21. JTextArea textArea = new JTextArea();
  22. Container c = getContentPane();
  23. int randomRow;
  24. int randomCol;
  25. int counter = 0;
  26.  
  27. public buttonGameFrame() {
  28. buttonGenerator();
  29. generateRandomShot();
  30. changeSide();
  31. bPanel.setLayout(new GridLayout(5,4));
  32. mPanel.setLayout(new BorderLayout());
  33. mPanel.add(bPanel, BorderLayout.CENTER);
  34. mPanel.add(leftPanel, BorderLayout.WEST);
  35. mPanel.add(rightPanel, BorderLayout.EAST);
  36. bottomPanel.add(textArea);
  37. textArea.setText("Pozostało ruchów: 20");
  38. mPanel.add(bottomPanel, BorderLayout.SOUTH);
  39. c.add(mPanel);
  40. setTitle("Egzamin 25.02.2020");
  41. setSize(800,400);
  42. setLocation(200,200);
  43. setVisible(true);
  44. }
  45.  
  46. private void buttonGenerator() {
  47. for(int i=0;i<5;i++)
  48. for(int j=0;j<4;j++) {
  49. buttons[i][j] = new JButton();
  50. buttons[i][j].setBackground(Color.yellow);
  51. buttons[i][j].setActionCommand("button" +i +"_" +j);
  52. buttons[i][j].addActionListener(this);
  53. bPanel.add(buttons[i][j]);
  54. }}
  55.  
  56. private void generateRandomShot(){
  57. Random generator = new Random();
  58. randomRow = generator.nextInt(4);
  59. randomCol = generator.nextInt(3);
  60. System.out.println("Kolumna: " +(randomCol+1) + ", Wiersz: " + (randomRow+1));
  61. }
  62.  
  63. public void changeSide(){
  64. if(counter%2==0) {
  65. leftPanel.setBackground(Color.red);
  66. rightPanel.setBackground(Color.white);
  67. } else if(counter%2!=0){
  68. leftPanel.setBackground(Color.white);
  69. rightPanel.setBackground(Color.red);
  70. }}
  71.  
  72. public void actionPerformed(ActionEvent e) {
  73. if(e.getActionCommand().contains("button"))
  74. {
  75. int i = Integer.parseInt(Character.toString(e.getActionCommand().replaceAll("button","").replaceAll("_", "").charAt(0)));
  76. int j = Integer.parseInt(Character.toString(e.getActionCommand().replaceAll("button","").replaceAll("_", "").charAt(1)));
  77. if(j==randomCol && i==randomRow){
  78. JOptionPane.showMessageDialog(null, "Przegrałeś!");
  79. System.exit(0);
  80. }
  81. if(counter<20){
  82. counter++;
  83. changeSide();
  84. textArea.setText("Pozostało ruchów: " + (20-counter));
  85. } buttons[i][j].setBackground(Color.blue);
  86. }
  87. }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement