Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util. * ;
- import java.awt. * ;
- import java.awt.event. * ;
- import javax.swing. * ;
- public class BreakthroughGUI {
- //Button array
- JButton[][] BUTTONS;
- //'Selected' GameButton Placeholder
- GameButton selectedButton;
- //Constructor
- public BreakthroughGUI() {
- //set up GUI
- JFrame frame = new JFrame("Breakthrough");
- frame.setLayout(new GridLayout(8, 8));
- selectedButton = null;
- //Create the buttons
- //We want our board to be 8x8,
- //so we'll make an 8x8 2D array
- int cols = 8;
- int rows = 8;
- BUTTONS = new GameButton[cols][rows];
- //now we loop through the board and instantiate a button for each
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < cols; j++) {
- GameButton theButt = new GameButton();
- BUTTONS[i][j] = theButt;
- theButt.setXPos(j);
- theButt.setYPos(i);
- //BUTTONS[i][j].setID(500);
- frame.add(BUTTONS[i][j]);
- }
- }
- for (int x = 0; x < 2; x++) {
- for (int y = 0; y < 8; y++) {
- BUTTONS[y][x].setText("X");
- }
- }
- for (int z = 7; z > 5; z--) {
- for (int y = 0; y < 8; y++) {
- BUTTONS[y][z].setText("O");
- }
- }
- for (int i = 0; i < 8; i++) {
- for (int j = 0; j < 8; j++) {
- BUTTONS[i][j].addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent ae) {
- GameButton button = (GameButton) ae.getSource();
- if (selectedButton == null) {
- select(button);
- } else {
- if (selectedButton.getText() == "X") {
- //Check if it's going backwards
- if (button.getXPos() < selectedButton.getXPos()) {
- //Drop out if it is backwards
- return;
- }
- //Check to see if it's in front of the selected box
- //If we subtract the clicked squares's point from the selected square's point,
- // x should be 1 and y should be -1 through 1
- int xDif = button.getXPos() - selectedButton.getXPos();
- if (xDif != 1) {
- return;
- }
- int yDif = button.getYPos() - selectedButton.getYPos();
- if (-1 > yDif || yDif > 1) {
- return;
- }
- //Should be all clear for now (the return;s would have dropped us out of the method
- // if the x and y difs didnt match what we need
- //Move the 'X' over
- selectedButton.setText("");
- button.setText("X");
- deselect();
- } else if (selectedButton.getText() == "O") {
- //Check if it's going backwards
- if (button.getXPos() > selectedButton.getXPos()) {
- //Drop out if it is backwards
- return;
- }
- //Check to see if it's in front of the selected box
- //If we subtract the clicked squares's point from the selected square's point,
- // x should be -1 and y should be -1 through 1
- int xDif = button.getXPos() - selectedButton.getXPos();
- if (xDif != -1) {
- return;
- }
- int yDif = button.getYPos() - selectedButton.getYPos();
- if (-1 > yDif || yDif > 1) {
- return;
- }
- //Should be all clear for now (the return;s would have dropped us out of the method
- // if the x and y difs didnt match what we need
- //Move the 'O' over
- selectedButton.setText("");
- button.setText("O");
- deselect();
- }
- }
- }
- });
- }
- }
- frame.setVisible(true);
- frame.pack();
- frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
- }
- //Main constructor
- public static void main(String[] args) {
- new BreakthroughGUI();
- }
- //Functions for changing whats selected and what isn't
- // This lets us change what happens during the (de)select process
- // (e.g. graphical attributes, text changes, etc)
- public void deselect() {
- selectedButton.setEnabled(true);
- selectedButton = null;
- }
- public void select(GameButton btn) {
- selectedButton = btn;
- selectedButton.setEnabled(false);
- }
- }
- //Here's our custom button class!
- class GameButton extends JButton {
- //Positions in the grid
- private int xPos = -1;
- private int yPos = -1;
- //Constructor
- public GameButton() {
- super();
- }
- //Getters and Setters for the grid position
- public void setXPos(int val) {
- xPos = val;
- }
- public void setYPos(int val) {
- yPos = val;
- }
- public int getXPos() {
- return xPos;
- }
- public int getYPos() {
- return yPos;
- }
- }
Add Comment
Please, Sign In to add comment