gpt4 book ai didi

java - 将图形绘制到数组中

转载 作者:行者123 更新时间:2023-12-02 03:33:34 24 4
gpt4 key购买 nike

假设我有给定的数组:

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

另外,如果您愿意的话,有没有一种方法可以指定任意数量的点并将它们全部连接起来然后填写? (不,我不想使用任何库)。

最佳答案

非常欺骗的方法(无需自己实现绘图逻辑)是将 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方法,所以唯一需要改进的地方就是转换为二维数组。可以从 Image 获取表示为整数值的像素数组,并将其转换为二维数组。我更新了绘制 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();
}

唯一提高性能的地方就是根本不将其转换为二维数组并访问我之前提到的值。但如果你想用数字 1 而不是默认的 -1 来“绘制”,你就必须循环遍历像素数组,将 -1 替换为 1。

关于java - 将图形绘制到数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37727195/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com