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

时间:2015-10-02 05:08:40

标签: 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,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)。

1 个答案:

答案 0 :(得分:0)

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

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