Advertisement
Giftednarwhals

TicTacToeGameboard

Mar 5th, 2014
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Name         :   Kyle Blanchard
  2.  * Due Date     :   2/14/2014
  3.  * Course       :   CSCI - 211
  4.  * Problem No.      :   12.7
  5.  * Description      :   Create and fill in a TicTacToe Board
  6.  * Email        :   Kwblanchard@student.stcc.edu
  7.  */
  8.  
  9. package guiBasics;
  10.  
  11. import java.awt.*;
  12. import javax.swing.*;
  13.  
  14. public class TicTacToe extends JFrame {
  15.    
  16.     // Assigns the two images to the variables x and o.
  17.  
  18.     private ImageIcon x ;
  19.     private ImageIcon o ;
  20.  
  21.     public static void main(String[] args) {
  22.  
  23.         // Creates the Frame.
  24.        
  25.         TicTacToe frame = new TicTacToe("TicTacToe");
  26.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  27.         frame.setSize(200, 200);
  28.         frame.setLocationRelativeTo(null);
  29.         frame.setVisible(true);
  30.     }
  31.    
  32.     // Constructer
  33.  
  34.     public TicTacToe(String title) {
  35.  
  36.         super(title);
  37.         // Create board layout.
  38.  
  39.         setLayout(new GridLayout(3, 3));
  40.        
  41.         x = new ImageIcon(getClass().getResource("/image/x.gif"));
  42.         o = new ImageIcon(getClass().getResource("/image/o.gif"));
  43.        
  44.         // Places an x or an o image in each board slot.
  45.  
  46.         for (int i = 0; i < 9; i++) {
  47.             int randomizer = (int) (Math.random() * 2);
  48.             if (randomizer == 1)
  49.                 add(new JLabel(this.x));
  50.             else if (randomizer == 0)
  51.                 add(new JLabel(this.o));
  52.         }
  53.     }
  54.  
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement