矩阵到图形表示 - java

Matrix to graphical representation - java

我有一个 7x16 矩阵,其中包含标准化双精度值,我想对其进行图形表示。

0.00    0.00    0.00    0.01    0.01    0.01    0.01    0.01    0.01    0.01    0.01    0.01    0.00    0.00    0.00    0.00    
0.01    0.02    0.04    0.07    0.07    0.06    0.03    0.02    0.03    0.06    0.07    0.05    0.02    0.02    0.01    0.01    
0.01    0.06    0.09    0.16    0.23    0.17    0.09    0.07    0.33    0.41    0.43    0.13    0.05    0.02    0.04    0.01    
0.01    0.05    0.14    0.15    0.21    0.26    0.11    0.11    0.43    1.00    0.44    0.16    0.05    0.02    0.05    0.01    
0.01    0.05    0.08    0.15    0.16    0.13    0.09    0.08    0.35    0.33    0.32    0.10    0.04    0.00    0.04    0.00    
0.01    0.03    0.05    0.11    0.18    0.17    0.17    0.17    0.17    0.17    0.17    0.11    0.05    0.01    0.03    0.00    
0.01    0.01    0.03    0.06    0.06    0.04    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00 

类似的东西

但我不知道该怎么做。如果有人可以提供帮助。提前谢谢:)

我建议您阅读如何进行渲染,尤其是使用 JFrames 和 JPanel(我认为这是完成您想做的事情的最简单方法)。我已经编写了一些代码来为您提供与您正在寻找的内容非常相似的输出,但带有灰度。 http://prntscr.com/f9pdef 是 5 个示例输出。顶部的一个是您提供的原始数据,底部的 4 个是原始数据但模糊,每个使用不同的乘数来增加亮度。一旦您了解了如何使用 JFrames 和 JPanel,您将很容易修改我所写的内容以获得您想要的内容。我写的示例代码如下:

import javax.swing.*;
import java.awt.*;
import java.awt.geom.Rectangle2D;

/**
 * Created by ----- on 5/19/2017.
 */
public class Window extends JFrame{
    private final double[][] gradientOriginal = new double[][]{
            {0.00,    0.00,    0.00,    0.01,    0.01,    0.01,   0.01,    0.01,    0.01,    0.01,    0.01,    0.01,   0.00,    0.00,    0.00,    0.00},
            {0.01,    0.02,    0.04,    0.07,    0.07,    0.06,   0.03,    0.02,    0.03,    0.06,    0.07,    0.05,   0.02,    0.02,    0.01,    0.01},
            {0.01,    0.06,    0.09,    0.16 ,   0.23 ,   0.17  ,  0.09   , 0.07  ,  0.33   , 0.41  ,  0.43  ,  0.13   , 0.05   , 0.02 ,   0.04  ,  0.01},
            {0.01 ,   0.05  ,  0.14 ,   0.15  ,  0.21  ,  0.26  ,  0.11  ,  0.11  ,  0.43  ,  1.00   , 0.44  ,  0.16 ,   0.05 ,   0.02  ,  0.05  ,  0.01},
            {0.01  ,  0.05  ,  0.08  ,  0.15  ,  0.16   , 0.13  ,  0.09  ,  0.08   , 0.35  ,  0.33 ,   0.32  ,  0.10  ,  0.04  ,  0.00  ,  0.04  ,  0.00},
            {0.01    ,0.03 ,   0.05  ,  0.11    ,0.18  ,  0.17  ,  0.17   , 0.17  ,  0.17  ,  0.17 ,  0.17  ,  0.11  ,  0.05  ,  0.01    ,0.03 ,   0.00},
            {0.01  ,  0.01  ,  0.03  ,  0.06  ,  0.06  ,  0.04   , 0.00 ,   0.00 ,   0.00  ,  0.00   , 0.00 ,   0.00  ,  0.00  ,  0.00 ,   0.00   , 0.00},
    };

    private final int width = gradientOriginal[0].length; //width of our array
    private final int height = gradientOriginal.length; //height of our array
    private final int multiplier = 3; //multiplier to make the output a little brighter
    private final double[][] gradient = new double[height][width]; //our averaged vector
    private final int size = 32; //the size of one of our squares
    private JPanel panel;

    public Window(){
        init();
    }

    private void init(){
        for(int y = 0; y < height; y++){ //loop through every position in the old array, average the surrounding squares, and put it in the new array
            for(int x = 0; x < width; x++){
                int surrounding = 0;
                double total = 0;
                for(int yy = -1; yy <= 1; yy++){
                    for(int xx = -1; xx <= 1; xx++){
                        if(y + yy >= 0 && y + yy < height && x + xx >= 0 && x + xx < width){
                            surrounding++;
                            total += gradientOriginal[y + yy][x + xx];
                        }
                    }
                }
                gradient[y][x] += total / surrounding;
                System.out.println(surrounding);
            }
        }

        panel = new JPanel(){
            @Override
            protected void paintComponent(Graphics g){ //this gets called when we call repaint() later
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D)g;
                for(int y = 0; y < height; y++){
                    for(int x = 0; x < width; x++){
                        Rectangle2D rect = new Rectangle2D.Double(x * size, y * size, size, size); //create a rectangle to render
                        int shade = Math.min((int)(gradient[y][x] * 255 * multiplier), 255); //set the color of the rectangle
                        g2d.setColor(new Color(shade, shade, shade)); //also set color of rectangle
                        g2d.fill(rect); //fill in the rectangle
                    }
                }
            }
        };
        panel.setPreferredSize(new Dimension(width * size, height * size)); //some initialization associated with the JPanel and JFrame
        add(panel);
        setResizable(false);
        pack();
        setTitle("Gradient");
        setLocationRelativeTo(null);

    }

    public static void main(String[] args){
        Window myWindow = new Window(); //create a window and set it to be visible  
        myWindow.setVisible(true);
    }
}

我建议使用有关 Frames 和 Swing 的 Java 教程 -- https://docs.oracle.com/javase/tutorial/uiswing/components/frame.html,以及有关 Graphics 和 Graphics2D 的 Java 文档。如果您还有其他问题,请随时给我留言。