Advertisement
puttepw

MVCView

May 19th, 2011
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.75 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.GridLayout;
  3. import java.util.Observable;
  4. import java.util.Observer;
  5.  
  6. import javax.swing.JButton;
  7. import javax.swing.JFrame;
  8. import javax.swing.JPanel;
  9.  
  10.  
  11. public class GameView extends JFrame implements Observer
  12. {
  13.     private GameModel model;
  14.     private GameController controller;
  15.  
  16.     private JPanel gridPanel = new JPanel();
  17.    
  18.     private GridLayout gridLayout = new GridLayout(3,3);
  19.    
  20.    
  21.     JButton btn0;
  22.     JButton btn1;
  23.     JButton btn2;
  24.     JButton btn3;
  25.     JButton btn4;
  26.     JButton btn5;
  27.     JButton btn6;
  28.     JButton btn7;
  29.     JButton btn8;
  30.     JButton btn9;
  31.    
  32.     public GameView(GameController aController, GameModel aModel)
  33.     {
  34.         controller = aController;
  35.         model = aModel;
  36.    
  37.         this.setTitle("TicTacToe");
  38.         this.setSize(400,400);
  39.         this.setLayout(new BorderLayout());
  40.         this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  41.        
  42.        
  43.         createGameButtons();
  44.         setupGridPanel();
  45.        
  46.         this.setVisible(true);
  47.        
  48.     }
  49.    
  50.     private void createGameButtons()
  51.     {
  52.          btn0 = new JButton();
  53.          btn1 = new JButton();
  54.          btn2 = new JButton();
  55.          btn3 = new JButton();
  56.          btn4 = new JButton();
  57.          btn5 = new JButton();
  58.          btn6 = new JButton();
  59.          btn7 = new JButton();
  60.          btn8 = new JButton();
  61.          btn9 = new JButton("Nytt Spel");
  62.        
  63.         btn0.setActionCommand("btn1");
  64.         btn1.setActionCommand("btn2");
  65.         btn2.setActionCommand("btn3");
  66.         btn3.setActionCommand("btn4");
  67.         btn4.setActionCommand("btn5");
  68.         btn5.setActionCommand("btn6");
  69.         btn6.setActionCommand("btn7");
  70.         btn7.setActionCommand("btn8");
  71.         btn8.setActionCommand("btn9");
  72.         btn9.setActionCommand("nytt");
  73.        
  74.        
  75.        
  76.            
  77.         btn0.addActionListener(controller);
  78.         btn1.addActionListener(controller);
  79.         btn2.addActionListener(controller);
  80.         btn3.addActionListener(controller);
  81.         btn4.addActionListener(controller);
  82.         btn5.addActionListener(controller);
  83.         btn6.addActionListener(controller);
  84.         btn7.addActionListener(controller);
  85.         btn8.addActionListener(controller);
  86.         btn9.addActionListener(controller);
  87.  
  88.        
  89.     }
  90.    
  91.     private void setupGridPanel()
  92.     {
  93.         gridPanel.setLayout(gridLayout);
  94.        
  95.             gridPanel.add(btn0);           
  96.             gridPanel.add(btn1);
  97.             gridPanel.add(btn2);
  98.             gridPanel.add(btn3);
  99.             gridPanel.add(btn4);
  100.             gridPanel.add(btn5);
  101.             gridPanel.add(btn6);
  102.             gridPanel.add(btn7);
  103.             gridPanel.add(btn8);
  104.             gridPanel.add(btn9);
  105.  
  106.  
  107.  
  108.        
  109.        
  110.        
  111.         this.add(gridPanel, BorderLayout.CENTER);
  112.         this.add(btn9, BorderLayout.NORTH);
  113.     }
  114.    
  115.    
  116.     private void updateView()
  117.     {
  118.         if(model.btn1State()==false)
  119.         {
  120.             btn0.setEnabled(false);
  121.             btn0.setText("1");
  122.             model.btn1ChangeTrue();
  123.    
  124.         }
  125.        
  126.         else if(model.btn2State()==false)
  127.         {
  128.             btn1.setEnabled(false);
  129.             btn1.setText("2");
  130.             model.btn2ChangeTrue();
  131.            
  132.         }
  133.        
  134.         else if(model.btn3State()==false)
  135.         {
  136.             btn2.setEnabled(false);
  137.             btn2.setText("3");
  138.             model.btn3ChangeTrue();
  139.         }
  140.         else if(model.btn4State()==false)
  141.         {
  142.             btn3.setEnabled(false);
  143.             btn3.setText("4");
  144.             model.btn4ChangeTrue();
  145.            
  146.         }
  147.        
  148.         else if(model.btn5State()==false)
  149.         {
  150.             btn4.setEnabled(false);
  151.             btn4.setText("5");
  152.             model.btn5ChangeTrue();
  153.            
  154.         }
  155.        
  156.         else if(model.btn6State()==false)
  157.         {
  158.             btn5.setEnabled(false);
  159.             btn5.setText("6");
  160.             model.btn6ChangeTrue();
  161.            
  162.         }
  163.        
  164.         else if(model.btn7State()==false)
  165.         {
  166.             btn6.setEnabled(false);
  167.             btn6.setText("7");
  168.             model.btn7ChangeTrue();
  169.            
  170.         }
  171.        
  172.         else if(model.btn8State()==false)
  173.         {
  174.             btn7.setEnabled(false);
  175.             btn7.setText("8");
  176.             model.btn8ChangeTrue();
  177.            
  178.         }
  179.        
  180.         else if(model.btn9State()==false)
  181.         {
  182.             btn8.setEnabled(false);
  183.             btn8.setText("9");
  184.             model.btn9ChangeTrue();
  185.            
  186.         }
  187.     }
  188.    
  189.    
  190.     public void update(Observable model, Object arg)
  191.     {
  192.         GameModel aModel = (GameModel)model;
  193.         updateView();
  194.        
  195.     }
  196.  
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement