3

I have an array of dimension of[20][20], filled with values of 0 & 1's. I would like to plot a graph similar to that of a weather image. Where 1's represents an activity with some colour and zero with no activity... What are essential things that I would require to begin plotting

Thank You Jeet

2
  • 0 & 1's? You mean boolean values? Commented Mar 9, 2011 at 7:46
  • Updated my answer (with int-matrix and colors). Commented Mar 9, 2011 at 7:56

1 Answer 1

5

The code (below) is a basic example what you want to do. It 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);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.