Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package checkers;
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.Graphics;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.*;
- public class Real extends JFrame{
- private static final long serialVersionUID = 1481112238070662165L;
- /*
- * After setting the serialverisonID to deal with the warning (above), here
- * are the global, "member" variables--the constant called BUTTON_SIZE, a TextArea to allow for a multiline display,
- * and the two dimensional array of JButtons called "buttons." This array will hold JButtons, but it is empty now.
- * (Notice there are no comments at the ends of methods and other closing "squirrels." From now on, you are not
- * required to do that.
- *
- * Also notice the "final" modifier. For primitive variables (those that don't need "new" to be created), that means
- * the value can't change. For other items, the meaning is a bit more complex: Once the identity of a final object
- * reference is set, it can still change its state, but not its identity (that is, you can't re-point the object
- * reference to some other object).
- *
- * tl;dr: it is best to use final whenever you can; your code will be clearer and more efficient.
- * It is not required in Beginning Programming, however.
- */
- private final static int BUTTON_SIZE = 120;
- private final JTextArea display;
- private final JButton[][] buttons = new JButton[8][8]; //set to 8 "columns" x 8 "rows"
- /*
- * In the main method,the instance (object) is created of this class (in the calculator exercise, that was done in
- * the go method), and the frame settings are set.
- */
- public static void main(String[] args) {
- Real instance = new Real(); //create object of this class (is a JFrame) -- this calls the constructor method
- instance.setLayout(null);//not using a preset layout; each object is placed in the frame explicitly
- instance.setSize(BUTTON_SIZE * 8 + BUTTON_SIZE/3, BUTTON_SIZE *9);
- instance.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//Closes Application When You Hit the X
- instance.setVisible(true);//You Can See It
- instance.setBackground(Color.green);
- }
- /*
- * Here's the constructor where the display and buttons are created and added--and the action listeners are
- * added to the buttons. In this example, the buttons are in an array, and are set up all at once unlike
- * the calculator where we separated creating from adding (and both of those were separate from
- * adding the action listeners).
- *
- */
- private Real(){ //constructor method!
- display = new JTextArea();//text area allows for multiline display--unlike text field
- display.setBounds(0, BUTTON_SIZE * buttons[0].length, BUTTON_SIZE * 3, BUTTON_SIZE * 2);//X, Y, Width, Height
- display.setText("Hey!\nI am a JTextArea.\n");//set initial display value
- display.setVisible(true);
- add(display);
- /*
- This is a "nested for loop" where one loop is inside another. Nested for loops are used to go through two-
- dimensional arrays. Note that the inner loop runs through all of its values each time the outer loop runs once.
- */
- for(int col = 0; col < buttons.length; col++){ //go through each column
- for(int row = 0; row <buttons[col].length; row++){//for each column, go through a row
- buttons[col][row] = new JButton("C" + col + " R"+row + " ");//make a button, adding a label of col and row
- buttons[col][row].setBounds(col*BUTTON_SIZE, row * BUTTON_SIZE, BUTTON_SIZE, BUTTON_SIZE);
- buttons[col][row].setBorderPainted(true);
- if(row % 2 == 0){
- if(col % 2 == 0){
- buttons[col][row].setForeground(Color.gray);
- buttons[col][row].setBackground(Color.gray);
- }else{
- buttons[col][row].setForeground(Color.white);
- buttons[col][row].setBackground(Color.white);
- }
- }else{
- if(col % 2 == 1){
- buttons[col][row].setForeground(Color.gray);
- buttons[col][row].setBackground(Color.gray);
- }else{
- buttons[col][row].setForeground(Color.white);
- buttons[col][row].setBackground(Color.white);
- }
- }
- buttons[col][row].setOpaque(true);
- buttons[col][row].setVisible(true);
- buttons[col][row].setEnabled(true);
- buttons[col][row].addActionListener(new ActionListener() {//add an action listener to each new button
- @Override
- public void actionPerformed(ActionEvent e) {
- buttonClicked(e); //pass e to the "buttonClicked" method
- }
- });
- add(buttons[col][row]);//add the buttons to the frame
- }
- }
- }
- /*
- * This is the method that takes the input from the buttons. The method
- * takes in the column and row of the button, and assigns the value 'X'
- * to the same spot in the array.
- *
- * Then the method uses nested for loops to run through the array, printing
- * each value. At the end of each row, a "\n" for "next line" is added to
- * the display.
- */
- private void buttonClicked(ActionEvent ae){
- /*ae is the "event object" that contains info about what was clicked.
- "getActionCommand" will give us the label from the button
- */
- String s = ae.getActionCommand();
- int c = Integer.parseInt("" + s.charAt(1)); //extract the column number from the button label
- int r = Integer.parseInt("" + s.charAt(s.length() - 2)); //extract the row from the button label
- buttons[c][r].setEnabled(false);
- display.setText("");//clear the display
- buttons[c][r].setText(buttons[c][r].getText() + "Clicked"); //replace the small 'z' with a large 'X' in the spot that was clicked
- buttons[c][r].setBackground(Color.green);
- checkForMatch(c,r); //call the "check for match"
- }
- private void checkForMatch(int c, int r){ //finds other buttons that have been clicked; sets display
- for(int thisCol = 0; thisCol < buttons.length; thisCol++){
- for(int thisRow = 0; thisRow < buttons[thisCol].length; thisRow++){
- String current = buttons[thisCol][thisRow].getText();
- String incoming = buttons[c][r].getText();
- if(current.contains("Clicked") && incoming.contains("Clicked") && !(thisRow == r && thisCol ==c)){
- display.setText(display.getText() +"\n" + "C" + c + "R" + r + " Matches C"+ thisCol + "R"+ thisRow);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement