Depends on what you're doing. Seems like we just have to play around with it a little to see how it's working. I tried to make up a small example below.
You could do it that way. Or you could get all of the image data in a single rgbArray and use some clever indexing to access the data you want from the array, storing it in a smaller block-size array as you go.
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
public class BlockTest {
BufferedImage image;
JLabel left;
JLabel right;
int cols = 9;
int rows = 9;
public void setImage(BufferedImage image) {
right.setIcon(new ImageIcon(image));
}
private JPanel getContent() {
left = getLeft();
new CellSelector(this);
right = new JLabel((ImageIcon)null, JLabel.CENTER);
right.setPreferredSize(left.getPreferredSize());
JPanel panel = new JPanel(new GridBagLayout());
panel.setBackground(Color.black);
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(5,5,5,5);
gbc.weightx = 1.0;
panel.add(left, gbc);
panel.add(right, gbc);
return panel;
}
private JLabel getLeft() {
int w = 360;
int h = 360;
int type = BufferedImage.TYPE_INT_RGB;
image = new BufferedImage(w, h, type);
Graphics2D g2 = image.createGraphics();
g2.setPaint(new GradientPaint(0,0,Color.green.darker(),
w/4,h/4,Color.red, true));
g2.fillRect(0,0,w,h);
g2.setPaint(Color.blue);
for(int j = 0; j <= rows; j++) {
int y = j*(w/rows);
g2.drawLine(0,y,h,y);
}
for(int j = 0; j <= cols; j++) {
int x = j*(h/rows);
g2.drawLine(x,0,x,h);
}
g2.dispose();
return new JLabel(new ImageIcon(image), JLabel.CENTER);
}
public static void main(String[] args) {
BlockTest test = new BlockTest();
JFrame f = new JFrame("Click any cell");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(test.getContent());
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
class CellSelector extends MouseAdapter {
BlockTest view;
JLabel target;
int[] rgbArray;
CellSelector(BlockTest bt) {
view = bt;
target = view.left;
target.addMouseListener(this);
// Create rgbArray with same size as left image.
int w = view.image.getWidth();
int h = view.image.getHeight();
rgbArray = new int[w*h];
}
public void mousePressed(MouseEvent e) {
Point p = e.getPoint();
// Locate origin of centered image inside left label.
int targetWidth = target.getWidth();
int targetHeight = target.getHeight();
int imageWidth = view.image.getWidth();
int imageHeight = view.image.getHeight();
int x0 = (targetWidth - imageWidth)/2;
int y0 = (targetHeight - imageHeight)/2;
// Make sure mouse selection is over image.
// User may have changed the layout or something.
Rectangle r = new Rectangle(x0, y0, imageWidth, imageHeight);
if(!r.contains(p))
return;
// Size of each cell in grid.
int cellWidth = imageWidth/view.cols;
int cellHeight = imageHeight/view.rows;
// Indices of row and column selected by the user.
int cellColIndex = (p.x - x0)/cellWidth;
int cellRowIndex = (p.y - y0)/cellHeight;
//System.out.printf("cellRowIndex = %d cellColIndex = %d%n",
// cellRowIndex, cellColIndex);
// Selected location in image coordinate system - not used.
int imageX = p.x - x0;
int imageY = p.y - y0;
// Origin of selected cell.
int x = cellColIndex * cellWidth;
int y = cellRowIndex * cellHeight;
// Collect the rgb data for the selected
// cell and write it into rgbArray at the
// cell location (offset) within the array.
// ******** Two options: ********
// Write directly into the rgbArray.
writeToArray(x, y, cellWidth, cellHeight, imageWidth);
// Get the data and transfer it into the rgbArray.
//transferToArray(x, y, cellWidth, cellHeight, imageWidth);
// ********************************
// Set rgbArray data into a new image and
// mount the image in the right label.
BufferedImage image = new BufferedImage(imageWidth, imageHeight,
view.image.getType());
image.setRGB(0, 0, imageWidth, imageHeight,
rgbArray, 0, imageWidth);
view.setImage(image);
}
/** Write data directly into rgbArray. */
private void writeToArray(int x, int y, int w, int h, int imageWidth) {
// Calculate offset into rgbArray to begin
// writing the rgb data of this cell.
int offset = y*imageWidth + x;
int scansize = imageWidth;
view.image.getRGB(x, y, w, h, rgbArray, offset, scansize);
}
/** Extract cell block data and write it into rgbArray. */
private void transferToArray(int x, int y, int w, int h, int imageWidth) {
int offset = 0;
int scansize = w;
int[] rgbs = view.image.getRGB(x, y, w, h, null, offset, scansize);
// Write the cell data into rgbArray.
int count = 0;
for(int j = y; j < y+h; j++) {
for(int k = x; k < x+w; k++) {
int index = j*imageWidth + k;
rgbArray[index] = rgbs[count++];
}
}
}
}