Advertisement
Guest User

Untitled

a guest
May 27th, 2015
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.47 KB | None | 0 0
  1. package checkers;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Font;
  5. import java.awt.Graphics;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8.  
  9. import javax.swing.*;
  10.  
  11. public class Real extends JFrame{
  12.     private static final long serialVersionUID = 1481112238070662165L;
  13.  
  14.         /*
  15.          * After setting the serialverisonID to deal with the warning (above), here
  16.          * are the global, "member" variables--the constant called BUTTON_SIZE, a TextArea to allow for a multiline display,
  17.          * and the two dimensional array of JButtons called "buttons."  This array will hold JButtons, but it is empty now.
  18.          * (Notice there are no comments at the ends of methods and other closing "squirrels." From now on, you are not
  19.          * required to do that.
  20.          *
  21.          * Also notice the "final" modifier.  For primitive variables (those that don't need "new" to be created), that means
  22.          * the value can't change. For other items, the meaning is a bit more complex: Once the identity of a final object
  23.          * reference is set, it can still change its state, but not its identity (that is, you can't re-point the object
  24.          * reference to some other object).
  25.          *
  26.          * tl;dr:  it is best to use final whenever you can; your code will be clearer and more efficient.
  27.          * It is not required in Beginning Programming, however.
  28.          */
  29.         private final static int BUTTON_SIZE = 120;
  30.         private final JTextArea display;
  31.         private final JButton[][] buttons = new JButton[8][8];  //set to 8 "columns" x 8 "rows"
  32.  
  33.         /*
  34.          * In the main method,the instance (object) is created of this class (in the calculator exercise, that was done in
  35.          * the go method), and the frame settings are set.
  36.          */
  37.         public static void main(String[] args) {
  38.                 Real instance = new Real();  //create object of this class (is a JFrame) -- this calls the constructor method
  39.                 instance.setLayout(null);//not using a preset layout; each object is placed in the frame explicitly
  40.                 instance.setSize(BUTTON_SIZE * 8 + BUTTON_SIZE/3, BUTTON_SIZE *9);
  41.                 instance.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//Closes Application When You Hit the X
  42.                 instance.setVisible(true);//You Can See It
  43.                 instance.setBackground(Color.green);
  44.                
  45.         }
  46.  
  47.         /*
  48.          * Here's the constructor where the display and buttons are created and added--and the action listeners are
  49.          * added to the buttons.  In this example, the buttons are in an array, and are set up all at once unlike
  50.          * the calculator where we separated creating from adding (and both of those were separate from
  51.          * adding the action listeners).
  52.          *
  53.          */
  54.        
  55.        
  56.      
  57.          
  58.         private Real(){  //constructor method!
  59.                 display = new JTextArea();//text area allows for multiline display--unlike text field
  60.                 display.setBounds(0, BUTTON_SIZE * buttons[0].length, BUTTON_SIZE * 3, BUTTON_SIZE * 2);//X, Y, Width, Height
  61.                 display.setText("Hey!\nI am a JTextArea.\n");//set initial display value
  62.                 display.setVisible(true);
  63.                 add(display);
  64.                 /*
  65.                 This is a "nested for loop" where one loop is inside another.  Nested for loops are used to go through two-
  66.                 dimensional arrays.  Note that the inner loop runs through all of its values each time the outer loop runs once.
  67.                  */
  68.                 for(int col = 0; col < buttons.length; col++){  //go through each column
  69.                         for(int row = 0; row <buttons[col].length; row++){//for each column, go through a row
  70.                                 buttons[col][row] = new JButton("C" + col + " R"+row + " ");//make a button, adding a label of col and row
  71.                                 buttons[col][row].setBounds(col*BUTTON_SIZE, row * BUTTON_SIZE, BUTTON_SIZE, BUTTON_SIZE);
  72.                                 buttons[col][row].setBorderPainted(true);
  73.                                 if(row % 2 == 0){
  74.                                  if(col % 2 == 0){
  75.                                      buttons[col][row].setForeground(Color.gray);
  76.                                     buttons[col][row].setBackground(Color.gray);
  77.                                   }else{
  78.                                      
  79.                                       buttons[col][row].setForeground(Color.white);
  80.                                      buttons[col][row].setBackground(Color.white);
  81.  
  82.                                    
  83.                                   }
  84.                              }else{
  85.                                  if(col % 2 == 1){
  86.                                    
  87.                                     buttons[col][row].setForeground(Color.gray);
  88.                                     buttons[col][row].setBackground(Color.gray);
  89.  
  90.                                   }else{
  91.                                      
  92.                                        buttons[col][row].setForeground(Color.white);
  93.                                        buttons[col][row].setBackground(Color.white);
  94.  
  95.                                    
  96.                                    
  97.                                   }
  98.                              }
  99.                              
  100.                              buttons[col][row].setOpaque(true);  
  101.                              buttons[col][row].setVisible(true);
  102.                              buttons[col][row].setEnabled(true);  
  103.                                
  104.                                
  105.                                
  106.                                
  107.                                 buttons[col][row].addActionListener(new ActionListener() {//add an action listener to each new button
  108.                                         @Override
  109.                                         public void actionPerformed(ActionEvent e) {
  110.                                                 buttonClicked(e);   //pass e to the "buttonClicked" method
  111.                                         }
  112.                                 });
  113.                                 add(buttons[col][row]);//add the buttons to the frame
  114.                         }
  115.                 }
  116.         }
  117.        
  118.         /*
  119.          * This is the method that takes the input from the buttons.  The method
  120.          * takes in the column and row of the button, and assigns the value 'X'
  121.          * to the same spot in the array.
  122.          *
  123.          * Then the method uses nested for loops to run through the array, printing
  124.          * each value.  At the end of each row, a "\n" for "next line" is added to
  125.          * the display.
  126.          */
  127.         private void buttonClicked(ActionEvent ae){
  128.                 /*ae is the "event object" that contains info about what was clicked.
  129.                 "getActionCommand" will give us the label from the button
  130.                 */
  131.                 String s = ae.getActionCommand();
  132.                 int c = Integer.parseInt("" + s.charAt(1));  //extract the column number from the button label
  133.                 int r = Integer.parseInt("" + s.charAt(s.length() - 2)); //extract the row from the button label
  134.                 buttons[c][r].setEnabled(false);
  135.                 display.setText("");//clear the display
  136.                 buttons[c][r].setText(buttons[c][r].getText() + "Clicked"); //replace the small 'z' with a large 'X' in the spot that was clicked
  137.                 buttons[c][r].setBackground(Color.green);
  138.              
  139.                 checkForMatch(c,r); //call the "check for match"
  140.         }
  141.  
  142.      
  143.        
  144.          
  145.        
  146.        
  147.        
  148.         private void checkForMatch(int c, int r){  //finds other buttons that have been clicked; sets display
  149.                 for(int thisCol = 0; thisCol < buttons.length; thisCol++){
  150.                         for(int thisRow = 0; thisRow < buttons[thisCol].length; thisRow++){
  151.                                 String current = buttons[thisCol][thisRow].getText();
  152.                                 String incoming = buttons[c][r].getText();
  153.                                 if(current.contains("Clicked") && incoming.contains("Clicked") && !(thisRow == r && thisCol ==c)){
  154.                                         display.setText(display.getText() +"\n" + "C" + c + "R" + r + " Matches C"+ thisCol + "R"+ thisRow);
  155.                                 }
  156.                         }
  157.                 }
  158.         }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement