Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package siena;
- import java.awt.GraphicsEnvironment;
- import java.awt.GridBagConstraints;
- import java.awt.GridBagLayout;
- import java.awt.Insets;
- import java.io.File;
- import java.io.IOException;
- import javax.imageio.ImageIO;
- import javax.swing.ImageIcon;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- public class CFBoard extends JFrame {
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- public static ImageIcon[] img = new ImageIcon[3];
- public static JLabel info;
- public CFBoard() throws IOException {
- super(GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration());
- img[0] = read(new java.io.File("src/siena/gray.png"));
- img[1] = read(new java.io.File("src/siena/red.png"));
- img[2] = read(new java.io.File("src/siena/yellow.png"));
- }
- public static JLabel slot[][] = new JLabel[7][6];
- public void setUp() throws IOException {
- this.setTitle("Connect Four");
- this.setResizable(false);
- this.setDefaultCloseOperation(EXIT_ON_CLOSE);
- GridBagLayout gridbag = new GridBagLayout();
- this.setLayout(gridbag);
- GridBagConstraints c = new GridBagConstraints();
- c.gridwidth = 7;
- JButton ng = new JButton("New Game");
- c.anchor = c.NORTH;
- c.fill = c.HORIZONTAL;
- c.insets = new Insets(5, 5, 5, 5);
- this.add(ng, c);
- c.gridy = 1;
- c.gridwidth = 1;
- final JButton add[] = new JButton[7];
- ButtonClicker clickers[] = new ButtonClicker[8];
- for (int i = 0; i < clickers.length; i++) {
- clickers[i] = new ButtonClicker(i);
- }
- ng.addActionListener(clickers[0]);
- for(int i = 0; i < 7; i++) {
- add[i] = new JButton("Drop token");
- add[i].addActionListener(clickers[i + 1]);
- c.gridx = i;
- this.add(add[i], c);
- }
- c.ipadx = 0;
- c.ipady = 0;
- c.gridx = 0;
- c.gridy = 2;
- for (int x = 0; x < 7; x++) {
- for (int y = 0; y < 6; y++) {
- slot[x][y] = new JLabel(img[0]);
- this.add(slot[x][y], c);
- c.gridy++;
- }
- c.gridx++;
- c.gridy = 2;
- }
- c.gridx = 0;
- c.gridy = 8;
- c.gridwidth = 7;
- c.anchor = c.CENTER;
- info = new JLabel("It is the RED player's turn.");
- info.setHorizontalAlignment(info.CENTER);
- this.add(info, c);
- this.pack();
- this.setVisible(true);
- }
- public void doThing() {
- for (int x = 0; x < 7; x++) {
- for (int y = 0; y < 6; y++) {
- this.slot[x][y].setIcon(img[ConnectFour.slotStat[x][y]]);
- }
- }
- if(ConnectFour.redsTurn) this.info.setText("It is RED's turn.");
- else this.info.setText("It is YELLOW's turn.");
- }
- public static ImageIcon read(File x) throws IOException {
- return new ImageIcon(ImageIO.read(x));
- }
- public void updateInfo(String s) {
- this.info.setText(s);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement