Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package numbergame;
  7.  
  8. import java.awt.BorderLayout;
  9. import java.awt.Color;
  10. import java.awt.Container;
  11. import java.awt.Dimension;
  12. import java.awt.GridLayout;
  13. import java.util.ArrayList;
  14. import javax.swing.JButton;
  15. import javax.swing.JFrame;
  16. import javax.swing.JLabel;
  17. import javax.swing.JPanel;
  18.  
  19. /**
  20. *
  21. * @author Philip
  22. */
  23. public class View extends JFrame {
  24. ArrayList<ArrayList<JButton>> knappar;
  25. Model model;
  26. JLabel scoreText;
  27.  
  28. public View (Model model){
  29. setDefaultCloseOperation(EXIT_ON_CLOSE);
  30. setSize(new Dimension(500,500));
  31. setResizable(false);
  32. JPanel game, score;
  33. Container container = getContentPane();
  34. container.setLayout(new BorderLayout());
  35. game = new JPanel(new GridLayout(model.boardSize(),model.boardSize()));
  36. scoreText = new JLabel("Score: " + 0);
  37. score = new JPanel();
  38. score.add(scoreText);
  39. knappar = new ArrayList<ArrayList<JButton>>();
  40. Control c = new Control(model);
  41. this.model = model;
  42.  
  43.  
  44. for (int i = 0; i < model.boardSize(); i++) {
  45. knappar.add(new ArrayList<JButton>());
  46. for (int j = 0; j < model.boardSize(); j++) {
  47. knappar.get(i).add(new JButton(Integer.toString(model.getNumber(i, j))));
  48. knappar.get(i).get(j).setActionCommand("" + i + j);
  49. knappar.get(i).get(j).addActionListener(c);
  50. game.add(knappar.get(i).get(j));
  51. }
  52. }
  53. container.add(game, BorderLayout.CENTER);
  54. container.add(score, BorderLayout.SOUTH);
  55. }
  56.  
  57. public void update(){
  58. for (int i = 0; i < knappar.size(); i++) {
  59. for (int j = 0; j < knappar.get(i).size(); j++) {
  60. if (model.isBlocked(i, j)) {
  61. knappar.get(i).get(j).setEnabled(false);
  62. }
  63. if (model.isChoosen(i, j)) {
  64. knappar.get(i).get(j).setBackground(Color.RED);
  65. scoreText.setText("Score: " + model.getSum());
  66. }
  67. }
  68. }
  69. }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement