Graphics in Java
I am trying to create a 20x20 grid and superimpose it on a 256x256 image. I have written some code that creates the grid but when i place it on the image it turns black! Another problem I have is coordinating with the pixel positions on the grid and image so I need to somehow transform the grid so that it corresponds to the image... here is the code i have written so far...
int SIZE = 20;
JLabel cell [] [] = null;
JFrame f = new JFrame();
f.getContentPane().setLayout(new GridLayout(SIZE, SIZE,0,0));
cell = new JLabel[SIZE][SIZE];
for(int x = 0; x< SIZE; x++)
{
for(int y=0; y<SIZE; y++)
{
cell[x][y] = new JLabel();
cell[x][y].setPreferredSize(new Dimension(20,20));
cell[x][y].setBorder(BorderFactory.createLineBorder(Color.black));
if(x==SIZE/2 && y==SIZE/2)
cell[x][y].setForeground(Color.white);
f.getContentPane().add(cell[x][y]);
}
}
f.setForeground(Color.white);
f.pack();
f.setVisible(true);
f.setTitle("Grid");
//f.setComposite(makeComposite(0));
System.out.println("width: " + f.getWidth() + "height : " + f.getHeight());
BufferedImage im = new BufferedImage(f.getWidth(), f.getHeight(),
BufferedImage.TYPE_3BYTE_BGR);
//cp.paint(image.getGraphics());
try {
ImageIO.write(im, "bmp", new File("jframe.bmp"));
}
catch(Throwable thr)
{ thr.printStackTrace();
}
System.exit(0);
BufferedImage awtImage = new BufferedImage(f.getWidth(),f.getHeight(),BufferedImage.TYPE_INT_RGB);
BufferedImage large = ImageIO.read(new File("brainSlice.bmp"));
int imgCols = large.getWidth();
int imgRows = large.getHeight();
int type = BufferedImage.TYPE_INT_RGB;
BufferedImage image = new BufferedImage(imgCols, imgRows, type);
Graphics2D g2 = image.createGraphics();
g2.drawImage(large, 0, 0, null);
g2.drawImage(awtImage,0,0,null);
g2.dispose();
ImageIO.write(image, "jpg", new File("withGrid.jpg"));
Could you please let me know where I am going wrong? I am trying to create a temperature map of the brain so I need to be able to acces the pixel positions in the grid to colour them accordingly. The 20x20 grid is needed as the software used to gain signals from the brain uses a 20x20 image.
Thanking you in advance.>

