Advertisement
Guest User

CFBoard.java

a guest
Jan 8th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.71 KB | None | 0 0
  1. package siena;
  2.  
  3. import java.awt.GraphicsEnvironment;
  4. import java.awt.GridBagConstraints;
  5. import java.awt.GridBagLayout;
  6. import java.awt.Insets;
  7. import java.io.File;
  8. import java.io.IOException;
  9.  
  10. import javax.imageio.ImageIO;
  11. import javax.swing.ImageIcon;
  12. import javax.swing.JButton;
  13. import javax.swing.JFrame;
  14. import javax.swing.JLabel;
  15.  
  16. public class CFBoard extends JFrame {
  17.    
  18.     /**
  19.      *
  20.      */
  21.     private static final long serialVersionUID = 1L;
  22.     public static ImageIcon[] img = new ImageIcon[3];
  23.     public static JLabel info;
  24.     public CFBoard() throws IOException {
  25.         super(GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration());
  26.         img[0] = read(new java.io.File("src/siena/gray.png"));
  27.         img[1] = read(new java.io.File("src/siena/red.png"));
  28.         img[2] = read(new java.io.File("src/siena/yellow.png"));
  29.  
  30.     }
  31.     public static JLabel slot[][] = new JLabel[7][6];
  32.     public void setUp() throws IOException {
  33.         this.setTitle("Connect Four");
  34.         this.setResizable(false);
  35.         this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  36.         GridBagLayout gridbag = new GridBagLayout();
  37.         this.setLayout(gridbag);
  38.         GridBagConstraints c = new GridBagConstraints();
  39.         c.gridwidth = 7;
  40.         JButton ng = new JButton("New Game");
  41.         c.anchor = c.NORTH;
  42.         c.fill = c.HORIZONTAL;
  43.         c.insets = new Insets(5, 5, 5, 5);
  44.         this.add(ng, c);
  45.         c.gridy = 1;
  46.         c.gridwidth = 1;
  47.         final JButton add[] = new JButton[7];
  48.         ButtonClicker clickers[] = new ButtonClicker[8];
  49.         for (int i = 0; i < clickers.length; i++) {
  50.             clickers[i] = new ButtonClicker(i);
  51.         }
  52.         ng.addActionListener(clickers[0]);
  53.         for(int i = 0; i < 7; i++) {
  54.             add[i] = new JButton("Drop token");
  55.             add[i].addActionListener(clickers[i + 1]);
  56.             c.gridx = i;
  57.             this.add(add[i], c);
  58.         }
  59.         c.ipadx = 0;
  60.         c.ipady = 0;
  61.         c.gridx = 0;
  62.         c.gridy = 2;
  63.         for (int x = 0; x < 7; x++) {
  64.             for (int y = 0; y < 6; y++) {
  65.                 slot[x][y] = new JLabel(img[0]);
  66.                 this.add(slot[x][y], c);
  67.                 c.gridy++;
  68.             }
  69.             c.gridx++;
  70.             c.gridy = 2;
  71.         }
  72.         c.gridx = 0;
  73.         c.gridy = 8;
  74.         c.gridwidth = 7;
  75.         c.anchor = c.CENTER;
  76.         info = new JLabel("It is the RED player's turn.");
  77.         info.setHorizontalAlignment(info.CENTER);
  78.         this.add(info, c);
  79.         this.pack();
  80.         this.setVisible(true);
  81.     }
  82.  
  83.     public void doThing() {
  84.         for (int x = 0; x < 7; x++) {
  85.             for (int y = 0; y < 6; y++) {
  86.                 this.slot[x][y].setIcon(img[ConnectFour.slotStat[x][y]]);
  87.             }
  88.         }
  89.         if(ConnectFour.redsTurn) this.info.setText("It is RED's turn.");
  90.         else this.info.setText("It is YELLOW's turn.");
  91.     }
  92.     public static ImageIcon read(File x) throws IOException {
  93.         return new ImageIcon(ImageIO.read(x));
  94.     }
  95.     public void updateInfo(String s) {
  96.         this.info.setText(s);
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement