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



我已经有一个主组件和一个组件来绘制。请帮我修正这个方法。

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。我尝试了这么多,它从来没有打印出整个板8x8(0和1)在arrayMethod中像我声明的

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

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

相关内容

  • 没有找到相关文章

最新更新