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.>

[2419 byte] By [vinirosi123a] at [2007-11-27 10:55:58]
# 1

First of all, format your code with the code tags so it's readable.

Number b) explain what you're trying to do a bit better. Your code looks quite awful, you're creating a 2D array of JLabels, creating BufferedImages...saving them...

-Kayaman-a at 2007-7-29 12:00:01 > top of Java-index,Java Essentials,Java Programming...
# 2

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");

BufferedImage im = new BufferedImage(f.getWidth(), f.getHeight(),

BufferedImage.TYPE_3BYTE_BGR);

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"));

I want to create a 20x20 pixels grid which the code above does. Then I want to place the grid on top of an image of size 256x256. I have 2 problems: a) the size of grid and image are not the same so I need to somehow transform the grid size to correspond to the image size.

b) when the grid is on top of the image I get an overall image that is black.

The main task is to fill in some square parts of the grid that I get from another software I am using. You see the software I use gives me the pixel position in the 20x20 grid and I have to map it on to the image.

I tried to save the grid as in image and then superimpose it on the brainSlice image, thats why I have done all the Buffering...

I hope this information helps.

Thanking you in advance>

vinirosi123a at 2007-7-29 12:00:01 > top of Java-index,Java Essentials,Java Programming...