Guest User

Untitled

a guest
Jan 5th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.93 KB | None | 0 0
  1. /*
  2.  * LogGame with GUI
  3.  */
  4.  
  5. import javax.swing.*;
  6. import java.awt.*;
  7. import java.awt.event.*;
  8.  
  9. public class LogGame implements ActionListener {
  10.     /*
  11.          * create array with 7 items
  12.          * 0 = space free
  13.          * 1 = space X
  14.          * 2 = space Y
  15.          */
  16.     int[] row = {1,1,1,0,2,2,2};
  17.     int place;
  18.     JFrame frame;
  19.     JPanel contentPane;
  20.     JLabel status;
  21.     JButton btnRow[] = new JButton[row.length];
  22.     JButton btnMoveLeft, btnMoveRight, btnReset;
  23.    
  24.    
  25.     public LogGame(){
  26.         /* create and set up frame */
  27.         frame = new JFrame("Log Game");
  28.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  29.        
  30.         /* create content pane */
  31.         contentPane = new JPanel();
  32.         contentPane.setBackground(Color.white);
  33.         contentPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
  34.         //contentPane.setLayout(new GridLayout(2,7,0,5));
  35.        
  36.         /* create and initialize buttons */        
  37.         /* create a button for each item in the array */
  38.         for(int i = 0; i < btnRow.length; i++){
  39.             btnRow[i] = new JButton();
  40.             btnRow[i].setText(Integer.toString(row[i]));
  41.             btnRow[i].setActionCommand(Integer.toString(i));
  42.             btnRow[i].addActionListener(this);
  43.             btnRow[i].setBackground(Color.white);
  44.             contentPane.add(btnRow[i]);
  45.         }
  46.        
  47.         btnMoveLeft = new JButton(" X");
  48.         btnMoveRight = new JButton("X ");
  49.        
  50.         btnMoveLeft.setActionCommand("L");
  51.         btnMoveRight.setActionCommand("4");
  52.    
  53.         btnMoveLeft.addActionListener(this);
  54.         btnMoveRight.addActionListener(this);
  55.        
  56.         btnMoveLeft.setEnabled(false);
  57.         btnMoveRight.setEnabled(false);
  58.  
  59.         contentPane.add(btnMoveLeft);
  60.         contentPane.add(btnMoveRight);
  61.        
  62.         /* add content to frame */
  63.         frame.setContentPane(contentPane);
  64.        
  65.         /* size and display frame */
  66.         frame.pack();
  67.         frame.setVisible(true);
  68.     }
  69.    
  70.     /*
  71.      * create and show GUI
  72.      */
  73.     public static void runGUI(){
  74.         LogGame run = new LogGame();
  75.     }
  76.    
  77.     public void actionPerformed(ActionEvent event){
  78.         String eventName = event.getActionCommand();
  79.         int index = Integer.parseInt(eventName);
  80.         int rowSelected, placeRight, placeRightHop, placeLeft, placeLeftHop;
  81.         boolean moveRight = false, moveRightHop = false, moveLeft = false, moveLeftHop = false;
  82.        
  83.         if (!eventName.equals("3") || !eventName.equals("4")){
  84.             rowSelected = index;
  85.             place = rowSelected;
  86.             resetButtonColor();
  87.             btnRow[index].setBackground(Color.gray);
  88.             //check placeRight and placeRightHop
  89.             try {
  90.                 placeRight = row[rowSelected + 1];
  91.                 placeRightHop = row[rowSelected + 2];
  92.                 if (placeRight != 0 || placeRightHop != 0){
  93.                     moveRight = false;
  94.                     btnMoveRight.setEnabled(false);
  95.                     btnMoveRight.setText("X");
  96.                     if (placeRight == 0){
  97.                         moveRight = true;
  98.                         btnMoveRight.setEnabled(true);
  99.                         btnMoveRight.setText(">");
  100.                     } else if (placeRightHop == 0){
  101.                         moveRight = true;
  102.                         btnMoveRight.setEnabled(true);
  103.                         btnMoveRight.setText(">>");
  104.                     }
  105.                 }              
  106.             } catch (ArrayIndexOutOfBoundsException e){
  107.                 moveRight = false;
  108.                 btnMoveRight.setEnabled(false);
  109.                 btnMoveRight.setText("X");
  110.             }
  111.             //check placeLeft and placeLeftHop
  112.             try {
  113.                 placeLeft = row[rowSelected - 1];
  114.                 placeLeftHop = row[rowSelected - 2];
  115.                 if (placeLeft != 0 || placeLeftHop != 0){
  116.                     moveLeft = false;
  117.                     btnMoveLeft.setEnabled(false);
  118.                     btnMoveLeft.setText("X");
  119.                     if (placeLeft == 0){
  120.                         moveLeft = true;
  121.                         btnMoveLeft.setEnabled(true);
  122.                         btnMoveLeft.setText("<");
  123.                     } else if (placeLeftHop == 0){
  124.                         moveRight = true;
  125.                         btnMoveLeft.setEnabled(true);
  126.                         btnMoveLeft.setText("<<");
  127.                     }
  128.                 }              
  129.             } catch (ArrayIndexOutOfBoundsException e){
  130.                 moveLeft = false;
  131.                 btnMoveLeft.setEnabled(false);
  132.                 btnMoveLeft.setText("X");
  133.             }
  134.         } else if (eventName.equals("3") && moveLeft == true){
  135.             row[place - 1] = row[place]; //left
  136.             row[place] = 0; //selectedPlace
  137.             updateRow();
  138.         } else if (eventName.equals("3") && moveLeftHop == true){
  139.             row[place - 2] = row[place]; //leftHop
  140.             row[place] = 0; //selectedPlace
  141.             updateRow();
  142.         } else if (eventName.equals("4") && moveRight == true){
  143.             row[place + 1] = row[place]; //right
  144.             row[place] = 0; //selectedPlace
  145.             updateRow();
  146.         } else if (eventName.equals("4") && moveRightHop == true){
  147.             row[place + 2] = row[place]; //rightHop
  148.             row[place] = 0; //selectedPlace
  149.             updateRow();
  150.         }
  151.     }
  152.    
  153.     public static void main(String[] args) {
  154.         /*
  155.          * start GUI
  156.          */
  157.         javax.swing.SwingUtilities.invokeLater(new Runnable(){
  158.             public void run(){
  159.                 runGUI();
  160.             }
  161.         });
  162.     }
  163.    
  164.     /*
  165.      * pre: none
  166.      * post: updates row
  167.      * sets button text to the integer stored in array
  168.      */
  169.     public void updateRow(){
  170.         for(int i = 0; i < btnRow.length; i++){
  171.             btnRow[i].setText(Integer.toString(row[i]));
  172.         }
  173.         resetButtonColor();
  174.     }
  175.     /*
  176.      * pre: none
  177.      * post: resets button colors
  178.      * sets button colors to white
  179.      */
  180.     public void resetButtonColor(){
  181.         for(int i = 0; i < btnRow.length; i++){
  182.             btnRow[i].setBackground(Color.white);
  183.         }
  184.     }
  185. }
Add Comment
Please, Sign In to add comment