将图形绘制成数组
Drawing graphics into an Array
假设我有给定的数组:
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 中获取表示为整数值的像素数组并将其转换为二维数组。我更新了 draw 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();
}
唯一可以提高性能的地方是完全不将其转换为二维数组并像我之前提到的那样访问值。但是,如果您想 "paint" 使用数字 1 而不是默认值 -1,则无论如何都必须遍历像素数组以将 -1s 替换为 1s。
假设我有给定的数组:
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 中获取表示为整数值的像素数组并将其转换为二维数组。我更新了 draw 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();
}
唯一可以提高性能的地方是完全不将其转换为二维数组并像我之前提到的那样访问值。但是,如果您想 "paint" 使用数字 1 而不是默认值 -1,则无论如何都必须遍历像素数组以将 -1s 替换为 1s。