如何用drawString JAVA在JFrame上打印二维数组

如何用drawString JAVA在JFrame上打印二维数组,java,arrays,multidimensional-array,jframe,drawstring,Java,Arrays,Multidimensional Array,Jframe,Drawstring,我已经有一个主要的和一个组成部分来绘制。请帮我修正这个方法 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,

我已经有一个主要的和一个组成部分来绘制。请帮我修正这个方法

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);
    }
}

伙计们,请帮我使用g2.drawString。我努力了,但它从来没有像我声明的那样以阵列方式打印出整个电路板8x8(0和1)。

我相信您没有使用loop打印。你应该试试:

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));
            }
        }

for(int i=0;i最完整的
draw
method.^akhil\u mittal它仍然只打印出0而不是全部8x8@LeoKao:实际上我的错。我们还应该更改坐标。现在试试。它仍然打印出黑点,而不是整个8x8板=(实际上问题在于您的其他代码。替换
int[][]数组=新的int[8][8]
ArrayViewer
with
private int[]array={{0,0,0,0,0,0,0,0,0,0,0},{1,0,1,0,1,0,1,0},{0,1,0,0,1,1},{1,0,1,0,0,1},{0,1,0,1,0,1},{1,0,1,0,1,1,0,1,0},{0,1,0,0,1,1,0,0,1},{0,0,0,0,0,0};
等等,我想主要将二维数组声明为int[][]=new int[8][8]并将其传递给arrayMethod tho,因为我必须编写一个方法,使第一行为0,最后一行为0,它之间的行为1,0,就像这样,然后我必须在jframe上打印出来。我无法更改main tho中的数组变量。