将图形绘制到数组中

时间:2016-06-09 13:20:30

标签: java arrays rendering

假设我有给定的数组:

int[] array = {
        0, 0, 0, 0, 0,
        0, 0, 0, 0, 0,
        0, 0, 0, 0, 0,
        0, 0, 0, 0, 0,
        0, 0, 0, 0, 0
    };

有没有办法将图形绘制到该数组中?例如,假设我们有一种通过x和y坐标访问此数组中的数据的方法,我们可以创建一个方法,通过此数组依赖于2个坐标。代码看起来像这样:

public void drawLine(int x1, int y1, int x2, int y2) {
    ...     
}

会转移这样的东西:

int[] array = {
        0, 0, 0, 0, 0,
        0, 0, 0, 0, 0,
        0, 0, 0, 0, 0,
        0, 0, 0, 0, 0,
        0, 0, 0, 0, 0
    };

进入这个:

int[] array = {
        1, 0, 0, 0, 0,
        0, 1, 0, 0, 0,
        0, 0, 1, 0, 0,
        0, 0, 0, 1, 0,
        0, 0, 0, 0, 1
    };

您必须能够传入任何坐标集,并通过数组放置一条计算线。我该如何实现呢?

调用drawLine(1,0,3,4)会产生如下内容:

int[] array = {
    0, 1, 0, 0, 0,
    0, 0, 1, 0, 0,
    0, 0, 1, 0, 0,
    0, 0, 0, 1, 0,
    0, 0, 0, 1, 0
};

另外,如果你有这样的方法,我可以指定任意数量的点并让它们全部连接然后填写? (不,我不想使用任何图书馆。)

1 个答案:

答案 0 :(得分:2)

非常讨厌的方法(没有自己实现绘图逻辑)将使用BufferedImage与数组的尺寸并绘制它。绘制完所需的线后,您将迭代BufferedImage的像素并检查绘制的像素。

private static void drawToArray(int[][] array2d, int x1, int y1, int x2, int y2) {
    int width = array2d[0].length; // width is columns and columns are second
    int height = array2d.length; // height is rows and rows are first

    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = image.createGraphics();
    g2d.setBackground(Color.WHITE);
    g2d.fillRect(0, 0, width, height);  // paint background white
    g2d.setColor(Color.BLACK);
    BasicStroke bs = new BasicStroke(1); // set black brush to size 1 (1 pixel)
    g2d.setStroke(bs);

    g2d.drawLine(x1, y1, x2, y2); // paint line on image

    // fill array with values, check entire image
    for (int row = 0; row < height; row++) {
        for (int column = 0; column < width; column++) {
            int clr = image.getRGB(row,column); // get color of pixel at position
            if (clr == Color.WHITE.getRGB()) { // white is -1
                array2d[row][column] = 0;
            } else {
                array2d[row][column] = 1;
            }
        }
    }
    g2d.dispose();
    // returning array is not necesery I am editing the array2d variable passed in
}

用法

int[][] arr = new int[5][5];
drawToArray(arr, 0, 0, 2, 5);

此示例假定您的数组是二维的,并且每行的长度相同。如果要使用一维数组,则必须自己定义其宽度和高度。而不是

array2d[row][column] = 0;

你会有

array1d[row*width + column] = 0;

编辑1:编辑我的答案更为通用

编辑2:考虑性能

我怀疑我可以改进drawLine方法,所以只有改进的地方才能转换为2d数组。可以从Image中获取表示为整数值的像素数组,并将其转换为2d数组。我更新了绘制ToArray方法,并在那里留下注释掉的行作为解释。

private static void drawToArray(int[][] array2d, int x1, int y1, int x2, int y2) {
    int width = array2d[0].length; // width is columns and columns are second
    int height = array2d.length; // height is rows and rows are first

    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = image.createGraphics();   // default color of image is 0 no need to paint background, just draw with color diferent than 0
    //g2d.setColor(Color.WHITE);               // default brush color is different than 0, expicitly setting is unnecesery
    //BasicStroke bs = new BasicStroke(1);
    //g2d.setStroke(bs);                       // default is 1 pixel expicitly setting is unnecesery

    g2d.drawLine(x1, y1, x2, y2); // paint line on image

    int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
    for (int i = 0, row = 0, col = 0; i < pixels.length; i++) {
        array2d[row][col] = pixels[i] == 0 ? 0 : 1; // no performance difference vs if/else just readability
        //array2d[row][col] = pixels[i]; // if you write it like this you will be "painting" with '-1's instead of '1's and save one if/else
        col++;
        // if is more readable here no performance difference vs ternary
        if (col == width) {
            col = 0;
            row++;
        }
    }
    g2d.dispose();
}

只有提高性能的其他地方才是根本不将其转换为2d数组并访问我之前提到的值。但是如果你想用数字1“绘制”而不是默认-1,那么无论如何你都必须循环通过像素数组来用-1替换-1s。