Results 1 to 2 of 2
  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); /*Setting the boolean argument of the set.Visible function to true
    indicating the window will be visible*/
    }

    }

    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 Error:

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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. 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
  2. two-dimensional array
    By kHim in forum New To Java
    Replies: 4
    Last Post: 11-16-2008, 08:21 PM
  3. Multi dimensional Array
    By Preethi in forum New To Java
    Replies: 1
    Last Post: 07-09-2008, 04:34 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

Tags for this Thread

Posting Permissions

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