Plotting in java?

I have an array of dimension [20] [20] filled with values ​​0 and 1. I would like to plot a graph similar to a graph of a weather image. Where 1 is an activity with some color and zero without activity ... What is needed in order to start drawing

Thanks Jeet

+3
source share
1 answer

The code (below) is a basic example of what you want to do. He will produce this image:

screenshot

public static void main(String[] args) {
    JFrame frame = new JFrame("Test");

    final int[][] map = new int[10][10];
    Random r = new Random(321);
    for (int i = 0; i < map.length; i++)
        for (int j = 0; j < map[0].length; j++)
            map[i][j] = r.nextBoolean() ? r.nextInt() : 0;

    frame.add(new JComponent() {
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            int w = getWidth() / map.length;
            int h = getHeight() / map[0].length;

            for (int i = 0; i < map.length; i++) {
                for (int j = 0; j < map[0].length; j++) {
                    if (map[i][j] != 0) {
                        g.setColor(new Color(map[i][j]));
                        g.fillRect(i * w, j * h, w, h);
                    }
                }
            }
        }
    });

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400, 400);
    frame.setVisible(true);
}
+5
source

All Articles