开发者

Heat map image using Java array

开发者 https://www.devze.com 2023-03-17 19:40 出处:网络
I have a multidimensional int array that has either a \'0\' or a \'1\'. I would like to create an image that re开发者_开发技巧sembles a heat map. The elements that have a \'0\' would be of one color a

I have a multidimensional int array that has either a '0' or a '1'. I would like to create an image that re开发者_开发技巧sembles 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.


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

Heat map image using Java array


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消