Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * LogGame with GUI
- */
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- public class LogGame implements ActionListener {
- /*
- * create array with 7 items
- * 0 = space free
- * 1 = space X
- * 2 = space Y
- */
- int[] row = {1,1,1,0,2,2,2};
- int place;
- JFrame frame;
- JPanel contentPane;
- JLabel status;
- JButton btnRow[] = new JButton[row.length];
- JButton btnMoveLeft, btnMoveRight, btnReset;
- public LogGame(){
- /* create and set up frame */
- frame = new JFrame("Log Game");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- /* create content pane */
- contentPane = new JPanel();
- contentPane.setBackground(Color.white);
- contentPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
- //contentPane.setLayout(new GridLayout(2,7,0,5));
- /* create and initialize buttons */
- /* create a button for each item in the array */
- for(int i = 0; i < btnRow.length; i++){
- btnRow[i] = new JButton();
- btnRow[i].setText(Integer.toString(row[i]));
- btnRow[i].setActionCommand(Integer.toString(i));
- btnRow[i].addActionListener(this);
- btnRow[i].setBackground(Color.white);
- contentPane.add(btnRow[i]);
- }
- btnMoveLeft = new JButton(" X");
- btnMoveRight = new JButton("X ");
- btnMoveLeft.setActionCommand("L");
- btnMoveRight.setActionCommand("4");
- btnMoveLeft.addActionListener(this);
- btnMoveRight.addActionListener(this);
- btnMoveLeft.setEnabled(false);
- btnMoveRight.setEnabled(false);
- contentPane.add(btnMoveLeft);
- contentPane.add(btnMoveRight);
- /* add content to frame */
- frame.setContentPane(contentPane);
- /* size and display frame */
- frame.pack();
- frame.setVisible(true);
- }
- /*
- * create and show GUI
- */
- public static void runGUI(){
- LogGame run = new LogGame();
- }
- public void actionPerformed(ActionEvent event){
- String eventName = event.getActionCommand();
- int index = Integer.parseInt(eventName);
- int rowSelected, placeRight, placeRightHop, placeLeft, placeLeftHop;
- boolean moveRight = false, moveRightHop = false, moveLeft = false, moveLeftHop = false;
- if (!eventName.equals("3") || !eventName.equals("4")){
- rowSelected = index;
- place = rowSelected;
- resetButtonColor();
- btnRow[index].setBackground(Color.gray);
- //check placeRight and placeRightHop
- try {
- placeRight = row[rowSelected + 1];
- placeRightHop = row[rowSelected + 2];
- if (placeRight != 0 || placeRightHop != 0){
- moveRight = false;
- btnMoveRight.setEnabled(false);
- btnMoveRight.setText("X");
- if (placeRight == 0){
- moveRight = true;
- btnMoveRight.setEnabled(true);
- btnMoveRight.setText(">");
- } else if (placeRightHop == 0){
- moveRight = true;
- btnMoveRight.setEnabled(true);
- btnMoveRight.setText(">>");
- }
- }
- } catch (ArrayIndexOutOfBoundsException e){
- moveRight = false;
- btnMoveRight.setEnabled(false);
- btnMoveRight.setText("X");
- }
- //check placeLeft and placeLeftHop
- try {
- placeLeft = row[rowSelected - 1];
- placeLeftHop = row[rowSelected - 2];
- if (placeLeft != 0 || placeLeftHop != 0){
- moveLeft = false;
- btnMoveLeft.setEnabled(false);
- btnMoveLeft.setText("X");
- if (placeLeft == 0){
- moveLeft = true;
- btnMoveLeft.setEnabled(true);
- btnMoveLeft.setText("<");
- } else if (placeLeftHop == 0){
- moveRight = true;
- btnMoveLeft.setEnabled(true);
- btnMoveLeft.setText("<<");
- }
- }
- } catch (ArrayIndexOutOfBoundsException e){
- moveLeft = false;
- btnMoveLeft.setEnabled(false);
- btnMoveLeft.setText("X");
- }
- } else if (eventName.equals("3") && moveLeft == true){
- row[place - 1] = row[place]; //left
- row[place] = 0; //selectedPlace
- updateRow();
- } else if (eventName.equals("3") && moveLeftHop == true){
- row[place - 2] = row[place]; //leftHop
- row[place] = 0; //selectedPlace
- updateRow();
- } else if (eventName.equals("4") && moveRight == true){
- row[place + 1] = row[place]; //right
- row[place] = 0; //selectedPlace
- updateRow();
- } else if (eventName.equals("4") && moveRightHop == true){
- row[place + 2] = row[place]; //rightHop
- row[place] = 0; //selectedPlace
- updateRow();
- }
- }
- public static void main(String[] args) {
- /*
- * start GUI
- */
- javax.swing.SwingUtilities.invokeLater(new Runnable(){
- public void run(){
- runGUI();
- }
- });
- }
- /*
- * pre: none
- * post: updates row
- * sets button text to the integer stored in array
- */
- public void updateRow(){
- for(int i = 0; i < btnRow.length; i++){
- btnRow[i].setText(Integer.toString(row[i]));
- }
- resetButtonColor();
- }
- /*
- * pre: none
- * post: resets button colors
- * sets button colors to white
- */
- public void resetButtonColor(){
- for(int i = 0; i < btnRow.length; i++){
- btnRow[i].setBackground(Color.white);
- }
- }
- }
Add Comment
Please, Sign In to add comment