I have a JPanel painted using paintComponents with many little tiles (4097 * 4097 1x1) painted on it.
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
drawTile(g);
}
public void drawTile(Graphics g) {
for (double[] row : terrainData) {
for (double d : row) {
int v = (int) (20 - d / 500);
if (v < 0) {
v = 0;
} else if (v > 20) {
v = 20;
}
color = v;
g.setColor(colors[color]);
g.fillRect(left, top, x, y);
left += x;
if (left == terrainSize * x) {
left = 0;
top += y;
}
}
}
}
I am adding this JPanel to a JScrollPane in order to move around the large panel. My problem is that whenever the JScrollPane is moved, the JPanel disappears completely. I tried adding a change listener to the scroll pane that, when stateChanged() revalidated and repainted the JPanel, although this did not change the result. How can I either make the JPanel not change at all, or repaint when the JScrollPane is scrolled?