0

I already have a main and a component to draw. Please help me fix this method.

public class ArrayMethod 
{
    private int i=8;
    private int j=8;

    private int[][]arrayMethod = {{0,0,0,0,0,0,0,0},
                                  {1,0,1,0,1,0,1,0},
                                  {0,1,0,1,0,1,0,1},
                                  {1,0,1,0,1,0,1,0},
                                  {0,1,0,1,0,1,0,1},
                                  {1,0,1,0,1,0,1,0},
                                  {0,1,0,1,0,1,0,1},
                                  {0,0,0,0,0,0,0,0}};

    public ArrayMethod(int[][] arrayComponent) 
    {
        //arrayComponent i declared as a int[8][8]
        //passed from main to class component to class arrayMethod.
        arrayMethod = arrayComponent;          
    }

    public void draw(Graphics2D g2) 
    {
        //I tried to draw with drawString but it doesn't work 
        //but I look up on google some people did it and they can print it out.
        g2.drawString(Integer.toString(arrayMethod[i][j]),10,10);
    }
}

Guys please help me with the g2.drawString I tried so hard and it never print out the whole board 8x8 (0 and 1) in the arrayMethod like i declared.

Spartan
  • 1,167
  • 4
  • 20
  • 40
Leo Kao
  • 39
  • 8

1 Answers1

0

I believe you have not used loop to print. You should try:

for(int i=0; i<arrayMethod.length; i++) {
            int[] arrayNested = arrayMethod[i];
            for(int j=0; j< arrayNested.length; j++) {
                g2.drawString(Integer.toString(arrayMethod[i][j]),(i +10) ,(j+10));
            }
        }
akhil_mittal
  • 23,309
  • 7
  • 96
  • 95
  • ^akhil_mittal Its still only print out 0 not the whole 8x8 – Leo Kao Oct 02 '15 at 05:17
  • @LeoKao: My Bad actually. We should also change coordinates. Try now. – akhil_mittal Oct 02 '15 at 05:19
  • Its still print out the black dot not the whole 8x8 board =( – Leo Kao Oct 02 '15 at 05:32
  • Actually problem is with your other code. Replace `int[][] array = new int[8][8]` in `ArrayViewer` with `private int[][]array = {{0,0,0,0,0,0,0,0}, {1,0,1,0,1,0,1,0}, {0,1,0,1,0,1,0,1}, {1,0,1,0,1,0,1,0}, {0,1,0,1,0,1,0,1}, {1,0,1,0,1,0,1,0}, {0,1,0,1,0,1,0,1}, {0,0,0,0,0,0,0,0}}; ` – akhil_mittal Oct 02 '15 at 05:33
  • Wait, I want to declare the 2 dimensional arrays in main as an int[][] = new int [8][8] and pass it to arrayMethod tho because i have to write a method to make the first line 0, the last line 0, and the line between it is 1,0 like that then I have to print it out on jframe. I cant change the array variables in main tho. – Leo Kao Oct 02 '15 at 05:42
  • And its still a black dot when I fix the variable in main http://postimg.org/image/mtgkwcu05/ – Leo Kao Oct 02 '15 at 05:45
  • In that case the array should be in main method. – akhil_mittal Oct 02 '15 at 05:45
  • You should invoke `paintComponent` method in main. – akhil_mittal Oct 02 '15 at 05:48
  • But I want to pass the 2 dimensional array from main to component to the method tho. I used to pass the string or integer number like that and its fine. – Leo Kao Oct 02 '15 at 05:51