drawing and filling a grid with jpanel and jlabel
Hi,
I am just starting to play with ocr. I have a 5x7 grid of boolean values that represent my downsampled character maps. I want to draw these onscreen so so that i can look at them and see if they are correct (or what I expect anyway). I am simply wondering how to go about this. If the value in the 2 dimentional array is true, the square should be black, otherwise white. Just never done this before and don't know where to start.
JPanel p =new JPanel(new GridLayout(5,7));
the tricky bit goes here!
JFrame f =new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(p);
f.pack();
f.setResizable(false);
f.setLocationRelativeTo(null);
f.setVisible(true);
Ideas are as welcome as code :)
Many thanks, Ron
[1011 byte] By [
cakea] at [2007-11-27 1:55:58]

# 1
import java.awt.*;
import java.util.Random;
import javax.swing.*;
public class Feedback {
private JPanel getContent() {
JPanel panel = new JPanel(new GridLayout(5,7));
Random seed = new Random();
Dimension d = new Dimension(40,40);
for(int j = 0; j < 5*7; j++) {
boolean b = seed.nextBoolean();
JPanel p = new JPanel();
p.setPreferredSize(d);
p.setBackground((b ? Color.black : Color.white));
panel.add(p);
}
// You could also do
//JOptionPane.showMessageDialog(null, panel, "",
//JOptionPane.PLAIN_MESSAGE); // -1
return panel;
}
public static void main(String[] args) {
Feedback test = new Feedback();
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(test.getContent());
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}