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

how to print 2 dimensional array on JFrame with drawString JAVA

我已经有一个主体和一个要绘制的组件。请帮我修复这个方法。

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 我已经很努力了,但它从来没有像我声明的那样在 arrayMethod 中打印出整个板 8x8(0 和 1)。

我相信你没有使用循环打印。你应该试试:

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