Results 1 to 4 of 4
  1. #1
    New2Java is offline Member
    Join Date
    Jul 2009
    Location
    Reading, Pennsylvania
    Posts
    6
    Rep Power
    0

    Question Creating Checkerboard from a 2 dimensional 'for' loop.

    Let me start off saying that I am very new to java, and a novice programmer in high school. Any input would be highly appreciated!

    I have come across a problem when i was trying to see if it was possible to create a checkerboard in java's Graphic User Interface. I already know how to create a three by three grid without the use of for loops, and was wondering if it was possible to create a custom grid using them. Here is my already working code of the three by three grid (without for loops).

    //Three By Three Layout With Nine Panels

    import java.awt.Color;
    import java.awt.Container;
    import java.awt.GridLayout;

    import javax.swing.*; //Allows access to the class JFrame

    public class Three_By_Three_With_Nine_Panels {

    public static void main(String[] args) {
    JFrame theGUI = new JFrame();
    theGUI.setTitle("Three By Three Grid");
    class on the 'theGUI' object*/
    theGUI.setSize(300,200); //Setting the window size in pixels of the width and height respectively
    theGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLO SE);

    JPanel panel1 = new JPanel();
    panel1.setBackground(Color.white);

    JPanel panel2 = new JPanel();
    panel2.setBackground(Color.blue);

    JPanel panel3 = new JPanel();
    panel3.setBackground(Color.pink);

    JPanel panel4 = new JPanel();
    panel4.setBackground(Color.black);

    JPanel panel5 = new JPanel();
    panel5.setBackground(Color.white);

    JPanel panel6 = new JPanel();
    panel6.setBackground(Color.black);

    JPanel panel7 = new JPanel();
    panel7.setBackground(Color.white);

    JPanel panel8 = new JPanel();
    panel8.setBackground(Color.black);

    JPanel panel9 = new JPanel();
    panel9.setBackground(Color.white);


    Container pane = theGUI.getContentPane();
    pane.setLayout(new GridLayout(3,3));

    pane.add(panel1);
    pane.add(panel2);
    pane.add(panel3);
    pane.add(panel4);
    pane.add(panel5);
    pane.add(panel6);
    pane.add(panel7);
    pane.add(panel8);
    pane.add(panel9);


    theGUI.setVisible(true);

    }

    }

    I was wondering if it was possible to put the red colored code above into a for loop to make it more efficient. Here is my attempt to do so:

    //Checkerboard with For Loops

    import java.awt.Color;
    import java.awt.Container;
    import java.awt.GridLayout;

    import javax.swing.*; //Allows access to the class JFrame
    import java.util.Scanner;

    public class Nine_By_Nine_Checkerboard {

    public static void main(String[] args) {

    JFrame theGUI = new JFrame(); //Creating an object for the JFrame class
    theGUI.setTitle("Nine By Nine Checkerboard"); /*Using member functions of the Jframe
    class on the 'theGUI' object*/
    theGUI.setSize(300,200); //Setting the window size in pixels of the width and height respectively
    theGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLO SE);
    Scanner reader = new Scanner(System.in);
    System.out.print("Enter length in number of boxes: ");
    int length = reader.nextInt();
    System.out.print("Enter width in number of boxes: ");
    int width = reader.nextInt();

    for(int i = 1; i<=length; i++){
    JPanel x = new JPanel();
    x.setBackground(Color.red);
    for(int j = 1; j<=width;j++){
    JPanel y = new JPanel();
    y.setBackground(Color.black);
    }
    }

    Container pane = theGUI.getContentPane();
    pane.setLayout(new GridLayout(length,width));

    for(int i = 1; i<=length; i++){
    pane.add(x);
    for(int j = 1;j <= width ; j++){
    pane.add(y);
    }
    }

    theGUI.setVisible(true);
    }

    }


    I am currently getting the Errors:

    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    x cannot be resolved
    y cannot be resolved

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    x cannot be resolved
    y cannot be resolved

    x and y have not been declared
    The declarations are local variables, ie, they are hidden inside the for loops, here, no one outside the scope of the for loops can see them:
    Java Code:
    for(int i = 1; i<=length; i++){
        JPanel x = new JPanel();
        x.setBackground(Color.red);
        for(int j = 1; j<=width;j++){
            JPanel y = new JPanel();
            y.setBackground(Color.black);
        }
    }
    Referring to them in these for loops is causing the problem:
    Java Code:
    for(int i = 1; i<=length; i++){
        pane.add(x);
        for(int j = 1;j <= width ; j++){
            pane.add(y);
        }
    }
    Consider:
    Java Code:
    Container pane = theGUI.getContentPane();
    pane.setLayout(new GridLayout(length,width));
    
    for(int i = 1; i<=length; i++){
    //    JPanel x = new JPanel();
    //    x.setBackground(Color.red);
        // for each row i, make j columns:
        for(int j = 1; j<=width;j++){
            JPanel y = new JPanel();
            y.setBackground(Color.black);
            pane.add(y);
        }
    }

  3. #3
    New2Java is offline Member
    Join Date
    Jul 2009
    Location
    Reading, Pennsylvania
    Posts
    6
    Rep Power
    0

    Default

    I got it to the point where i can get as many vertical columns as i want, but i can't seems in incorporate any horizontal rows, i don't know if the grid layout is wrong or what. I also changed some of the variables to make it more readable.

    code:
    Container pane = theGUI.getContentPane();
    pane.setLayout(new GridLayout(row,column));
    //i=row
    //j=column
    for(int i = 1; i<=row; i++){
    JPanel x = new JPanel();
    JPanel z = new JPanel();
    x.setBackground(Color.black);
    pane.add(x);
    for(int j = 1; j<=column;j++){
    JPanel y = new JPanel();
    JPanel m = new JPanel();
    y.setBackground(Color.red);
    pane.add(y);
    }
    }

    My ultimate goal with this project is to develop an 8x8 grid, or checkerboard.

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    First general ideas.
    Think about a grid of 2 rows with 2 columns.
    You will create 2 * 2 = 4 components for this grid.
    You can count these grid cells in two easy ways:
    1 count the rows: index 0 and 1
    For each of the rows there will be 2 columns. So if you use the first/outer loop to simply count rows you could use the inner loop to create_and_add a new component for each column in the row.
    For row 0: you create a component for column 0 and another for column 1.
    Total component count so far: 2.
    Then increment the row index in the outer loop to 1:
    For this row there will be 2 columns so you loop from 0 to 1 in the inner for loop and create_and_add a new component for each of these two columns.
    Total component count now: 4.

    2 the other easy way to count these grid cells is to do the opposite: make an outer for loop to count across the columns, ie, from column index 0 to index 1.
    Then for each column make an inner for loop that will count along the rows, since there are two rows we count from 0 to 1, making_and_adding a new component for each row. That's two components being created_and_added for each row. And since the outer for loop is counting across the columns, ie, from index 0 to 1, we end up with 2 * 2 components = 4.

    You want to count in such a way that you visit every grid cell one time. And the time that you visit/count it you also create and add a new component to the grid, ie, your parent components (GridLayout) layout manager.

    Second: comments about your code.
    Java Code:
    for(int i = 1; i<=row; i++){        // outer loop - counting rows
        // In this outer loop we are only counting the rows
        // so there is no need to create any components here.
        // An observation: panel "x" is being added to your "pane";
        // panel z will be discarded/forgotten since it is a local
        // variable and is not added to a parent container, ie,
        // once this outer [i]for[/i] loop moves to the next "i"
        // increment the panel "z" will be discarded. This is okay
        // but it is not needed for what you want to do here.
        // Let's comment these out:
    //    JPanel x = new JPanel();
    //    JPanel z = new JPanel();
    //    x.setBackground(Color.black);
    //    pane.add(x);
        for(int j = 1; j<=column;j++){  // inner loop - add panels
        // Now we are counting across the columns.
        // We do this for each row: row 0, 1, 2, ...8 
            // Only one component is needed for this grid cell.
            // For every row i we add j components.
            // This is cell[i, j], make and add one JPanel:
            JPanel y = new JPanel();
            // This panel m is not needed.
    //        JPanel m = new JPanel();
            y.setBackground(Color.red);
            pane.add(y);
        }
    }

Similar Threads

  1. Replies: 1
    Last Post: 07-22-2009, 11:10 PM
  2. Creating a New Method for Square Root Loop
    By SapphireSpark in forum New To Java
    Replies: 14
    Last Post: 02-25-2009, 02:21 PM
  3. two-dimensional array
    By kHim in forum New To Java
    Replies: 4
    Last Post: 11-16-2008, 08:21 PM
  4. How to reverse two dimensional
    By masaka in forum New To Java
    Replies: 4
    Last Post: 05-19-2008, 11:02 AM
  5. 2 dimensional Lists
    By gapper in forum New To Java
    Replies: 4
    Last Post: 01-20-2008, 10:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •