使用Java数组的热图图像

发布于 2024-11-19 02:00:30 字数 269 浏览 5 评论 0 原文

我有一个多维 int 数组,其中包含“0”或“1”。我想创建一个类似于热图的图像。具有“0”的元素将具有一种颜色,而具有“1”的元素将具有另一种颜色。 例如,

int [][] test = {{0,0,1}, {1,1,0}, {1,1,1}}

我会得到“3 x 3”的图像,有点像这样。

wwr
rrw
rrr

其中white表示白色,r表示红色。

感谢您的任何建议。

I have a multidimensional int array that has either a '0' or a '1'. I would like to create an image that resembles a heat map. The elements that have a '0' would be of one color and those of '1' would be of another color.
For instance

int [][] test = {{0,0,1}, {1,1,0}, {1,1,1}}

I would get an image of "3 x 3", kind of like this.

wwr
rrw
rrr

where white denotes white and r red.

Thanks for any suggestions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心的位置 2024-11-26 02:00:30

setRGB()BufferedImage 对此效果很好。 此处引用的示例使用SwingWorker,以及这个 示例使用 Runnable 线程。

image

The setRGB() or getRaster() methods of BufferedImage work well for this. The examples cited here use SwingWorker, and this example uses a Runnable thread.

image

人生戏 2024-11-26 02:00:30

查看 Java2D

基本上你想为像素颜色创建一个 2d int 数组并将它们绘制到图像上。查看 Graphics 和 Graphics2D 对象以及 BufferedImage 等。然后使用 Java ImageIO 将图像写入文件。

Have a look at Java2D.

Basically you want to create a 2d int array for the pixel colors and draw those to an image. Look at the Graphics and Graphics2D objects as well as BufferedImage and the like. Then use Java ImageIO to write the image to a file.

π浅易 2024-11-26 02:00:30

既然你的值都是 1 和 0,为什么不使用二维布尔数组呢?这将节省空间并使 if 语句更简单。

如果您愿意,您可以使用 Java 的 Graphics2D 包来绘制这些点!

这就是我喜欢设置 Graphics2D 实例的方式:

private static BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
private static Graphics2D g = image.createGraphics();

然后通过执行以下操作绘制图像:

g.drawLine(x1, y1, x2, y2);

并使用如下方法保存文件:

private static void saveToFile(){
        try {
            ImageIO.write(image, "png", new File("map.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

Seeing as your values are all 1's and 0's, why don't you use a 2-dimensional boolean array? This would save space as well as make the if statements simpler.

You can then use Java's Graphics2D package to draw these dots if you would like to!

This is how I like to set up my Graphics2D instance:

private static BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
private static Graphics2D g = image.createGraphics();

Then draw to the image by doing:

g.drawLine(x1, y1, x2, y2);

And save the file by using a method like this one:

private static void saveToFile(){
        try {
            ImageIO.write(image, "png", new File("map.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文