Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package numbergame;
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.Container;
- import java.awt.Dimension;
- import java.awt.GridLayout;
- import java.util.ArrayList;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- /**
- *
- * @author Philip
- */
- public class View extends JFrame {
- ArrayList<ArrayList<JButton>> knappar;
- Model model;
- JLabel scoreText;
- public View (Model model){
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- setSize(new Dimension(500,500));
- setResizable(false);
- JPanel game, score;
- Container container = getContentPane();
- container.setLayout(new BorderLayout());
- game = new JPanel(new GridLayout(model.boardSize(),model.boardSize()));
- scoreText = new JLabel("Score: " + 0);
- score = new JPanel();
- score.add(scoreText);
- knappar = new ArrayList<ArrayList<JButton>>();
- Control c = new Control(model);
- this.model = model;
- for (int i = 0; i < model.boardSize(); i++) {
- knappar.add(new ArrayList<JButton>());
- for (int j = 0; j < model.boardSize(); j++) {
- knappar.get(i).add(new JButton(Integer.toString(model.getNumber(i, j))));
- knappar.get(i).get(j).setActionCommand("" + i + j);
- knappar.get(i).get(j).addActionListener(c);
- game.add(knappar.get(i).get(j));
- }
- }
- container.add(game, BorderLayout.CENTER);
- container.add(score, BorderLayout.SOUTH);
- }
- public void update(){
- for (int i = 0; i < knappar.size(); i++) {
- for (int j = 0; j < knappar.get(i).size(); j++) {
- if (model.isBlocked(i, j)) {
- knappar.get(i).get(j).setEnabled(false);
- }
- if (model.isChoosen(i, j)) {
- knappar.get(i).get(j).setBackground(Color.RED);
- scoreText.setText("Score: " + model.getSum());
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement