Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- HELPFUL LINKS
- http://www.dreamincode.net/forums/topic/234693-connect-four-gui/
- https://github.com/sloev/connect-4---java/blob/master/src/connect4/gui.java
- http://www.coderanch.com/t/642941/GUI/java/Connect-game-edited-version
- http://cs.gettysburg.edu/~tneller/cs111/connect4/part1/files/Connect4.java
- https://electronicentertainment.wordpress.com/2009/11/13/here-is-a-small-java-connect-4-game-i-created/
- -----------------------------------------------------------------------------------------------------------------------------------
- // Connect 4
- // Description: This class is the GUI part of the program. It is the Main Menu Section.
- // Sammy Samkough
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class Connect4GUI extends JFrame
- {
- // size of screen
- public static final int WIDTH = 400;
- public static final int HEIGHT = 500;
- // title
- public static final String text = "MALLIKATRON CONNECT 4";
- private JLabel title;
- /* Buttons Include: 1) Start 2) About
- Start - starts the game
- About - includes the game layout, how it works, and the people behind the project
- */
- private JButton startB, aboutB, exitB;
- // layout
- private FlowLayout layout = new FlowLayout(FlowLayout.LEFT, 50, 50);
- public Connect4GUI()
- {
- // this is for the space between the buttons as well
- this.setLayout(new FlowLayout(FlowLayout.LEFT, 50, 50));
- // buttons
- startB = new JButton("START");
- startB.setPreferredSize(new Dimension(300, 50));
- aboutB = new JButton("ABOUT");
- aboutB.setPreferredSize(new Dimension(300, 50));
- exitB = new JButton("EXIT");
- exitB.setPreferredSize(new Dimension(300, 50));
- if(exitB.getModel().isPressed())
- {
- int confirm = JOptionPane.showOptionDialog(null, "Are You Sure to Close Application?", "Exit Confirmation", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null);
- if (confirm == 0)
- {
- System.exit(0);
- }
- }
- // labels
- title = new JLabel(text);
- title.setFont(title.getFont().deriveFont(24.0f));
- // additions
- add(title);
- add(startB);
- add(aboutB);
- add(exitB);
- // listeners
- event e1 = new event();
- startB.addActionListener(e1);
- event2 e2 = new event2();
- aboutB.addActionListener(e2);
- event3 e3 = new event3();
- exitB.addActionListener(e3);
- addWindowListener(exitListener);
- }
- WindowListener exitListener = new WindowAdapter()
- {
- @Override
- public void windowClosing(WindowEvent e)
- {
- int confirm = JOptionPane.showOptionDialog(null, "Are you sure you want to close application?", "Exit Confirmation", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null);
- if (confirm == 0)
- {
- System.exit(0);
- }
- }
- };
- public class event implements ActionListener
- {
- public void actionPerformed(ActionEvent e)
- {
- Connect4GUIStart gui = new Connect4GUIStart();
- gui.setTitle("Connect 4");
- gui.setSize(gui.WIDTH, gui.HEIGHT);
- gui.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
- gui.setLocationRelativeTo(null);
- gui.setVisible(true);
- gui.getContentPane().setBackground(new Color(242, 75, 75));
- dispose();
- }
- }
- public class event2 implements ActionListener
- {
- public void actionPerformed(ActionEvent e)
- {
- Connect4GUIAbout gui = new Connect4GUIAbout();
- gui.setTitle("About");
- gui.setSize(gui.WIDTH, gui.HEIGHT);
- gui.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
- gui.setLocationRelativeTo(null);
- gui.setVisible(true);
- gui.setContentPane(new JLabel(new ImageIcon("res/about.png")));
- }
- }
- public class event3 implements ActionListener
- {
- public void actionPerformed(ActionEvent e)
- {
- int confirm = JOptionPane.showOptionDialog(null, "Are you sure you want to close application?", "Exit Confirmation", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null);
- if (confirm == 0)
- {
- dispose();
- System.exit(0);
- }
- else
- {
- // continue
- }
- }
- }
- public static void main (String args[])
- {
- Connect4GUI frame = new Connect4GUI();
- frame.setTitle("Main Menu");
- frame.setSize(WIDTH, HEIGHT);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setLocationRelativeTo(null);
- frame.setVisible(true);
- frame.getContentPane().setBackground(new Color(242, 75, 75));
- }
- }
- -----------------------------------------------------------------------------------------------------------------------------------
- // Connect 4
- // Description: This class is the GUI part of the program. It is the Game Playing Section.
- // Sammy Samkough
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import java.util.*;
- public class Connect4GUIStart extends JFrame
- {
- // size of screen
- public static final int WIDTH = 900;
- public static final int HEIGHT = 900;
- private ImageIcon[][] board;
- private ImageIcon blank, red, black;
- private int turn;
- private JLabel chips;
- public Connect4GUIStart()
- {
- setLayout(new FlowLayout());
- setSize(WIDTH, HEIGHT);
- getContentPane().setBackground(new Color(242, 75, 75));
- blank = new ImageIcon("res/50x50/emptyspace50x50.png");
- red = new ImageIcon("res/50x50/redchip50x50.png");
- black = new ImageIcon("res/50x50/blackchip50x50.png");
- // listeners
- addWindowListener(exitListener);
- chips = new JLabel(blank);
- chips.setIcon(blank);
- add(chips);
- turn = 0;
- board = new ImageIcon[6][7];
- for(ImageIcon[] row: board)
- Arrays.fill(row, blank);
- }
- WindowListener exitListener = new WindowAdapter()
- {
- @Override
- public void windowClosing(WindowEvent e)
- {
- Connect4GUI frame = new Connect4GUI();
- frame.setTitle("Main Menu");
- frame.setSize(frame.WIDTH, frame.HEIGHT);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setLocationRelativeTo(null);
- frame.setVisible(true);
- frame.getContentPane().setBackground(new Color(242, 75, 75));
- dispose();
- }
- };
- /*
- // Player should be player 1 or 2
- // slot should be the column the piece is being placed in (0-6)
- // Returns: an empty string unless there is an issue (invalid player or slot number, or the slot in filled up) in
- // which case it will return an error message
- public String takeTurn(int player, int slot)
- {
- if(slot < 7 && slot > -1) // Check for correct slot count
- {
- if(player == 1 || player == 2)
- {
- for(int i = board.length - 1; i >= 0; i--)
- {
- if(board[i][slot] == 0)
- {
- board[i][slot] = player;
- return "";
- }
- }
- return "ERROR: That column is full";
- }
- else
- return "ERROR: player input invalid (possible values: 1 or 2)";
- }
- else
- return "ERROR: invalid column number (possible values: 0-6)";
- }
- // Returns number of player whos turn it is or 0 if error
- public int checkTurn()
- {
- int play1Count = 0;
- int play2Count = 0;
- for(int i = 0; i < board.length; i++)
- {
- for(int j = 0; j < board[i].length; j++)
- {
- if(board[i][j] == 1)
- play1Count++;
- else if(board[i][j] == 2)
- play2Count++;
- }
- }
- if(play1Count > play2Count)
- return 2;
- else if(play2Count == play1Count)
- return 1;
- return 0;
- }
- // Returns: 0 if no win, 1 for player 1 win, 2 for player 2 win, 3 for a tie
- // MUST BE CALLED AFTER EVERY TURN TO AVOID HAVING BOTH PEOPLE WIN
- public int winDetect()
- {
- //Check for horizontal win
- for(int i = 0; i < board.length; i++)
- {
- for(int j = 0; j < board[i].length - 3; j++)
- {
- for(int k = 1; k < 3; k++)
- {
- if(board[i][j] == k)
- if(board[i][j+1] == k)
- if(board[i][j+2] == k)
- if(board[i][j+3] == k)
- return k;
- }
- }
- }
- // Check for vertical win
- for(int i = 0; i < board.length - 3; i++)
- {
- for(int j = 0; j < board[i].length; j++)
- {
- for(int k = 1; k < 3; k++)
- {
- if(board[i][j] == k)
- if(board[i+1][j] == k)
- if(board[i+2][j] == k)
- if(board[i+3][j] == k)
- return k;
- }
- }
- }
- // Check for rightup-diagnol win
- for(int i = board.length - 3; i < board.length; i++)
- {
- for(int j = 0; j < board[i].length - 3; j++)
- {
- for(int k = 1; k < 3; k++)
- {
- if(board[i][j] == k)
- if(board[i-1][j+1] == k)
- if(board[i-2][j+2] == k)
- if(board[i-3][j+3] == k)
- return k;
- }
- }
- }
- // Check for leftdown-diagnol win
- for(int i = 0; i < board.length-3; i++)
- {
- for(int j = 0; j < board[i].length-3; j++)
- {
- for(int k = 1; k < 3; k++)
- {
- if(board[i][j] == k)
- if(board[i+1][j+1] == k)
- if(board[i+2][j+2] == k)
- if(board[i+3][j+3] == k)
- return k;
- }
- }
- }
- // Check for full board (tie)
- int poop = 0;
- for(int[] row: board)
- for(int col: row)
- if(col == 0)
- poop++;
- if(poop == 0)
- return 3;
- else
- return 0;
- }
- public String toString()
- {
- String result = "";
- for(int[] row: board)
- {
- for(int col: row)
- result+= col;
- result+= "\n";
- }
- return result;
- }
- */
- public static void main (String args[])
- {
- }
- }
- -----------------------------------------------------------------------------------------------------------------------------------
- // Connect 4
- // Description: This class is the GUI part of the program. It is the About Section.
- // Sammy Samkough
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class Connect4GUIAbout extends JFrame
- {
- // size of screen
- public static final int WIDTH = 600;
- public static final int HEIGHT = 600;
- public Connect4GUIAbout()
- {
- setLayout(new FlowLayout());
- // listeners
- addWindowListener(exitListener);
- }
- WindowListener exitListener = new WindowAdapter()
- {
- @Override
- public void windowClosing(WindowEvent e)
- {
- Connect4GUI frame = new Connect4GUI();
- frame.setTitle("Main Menu");
- frame.setSize(frame.WIDTH, frame.HEIGHT);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setLocationRelativeTo(null);
- frame.setVisible(true);
- frame.getContentPane().setBackground(new Color(242, 75, 75));
- dispose();
- }
- };
- public static void main (String args[])
- {
- }
- }
- -----------------------------------------------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement