0

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?

  • 3
    If you don't get help soon, consider creating and posting a [Minimal, Complete, and Verifiable Example Program](http://stackoverflow.com/help/mcve) where you condense your code into the smallest bit that still compiles and runs, has no outside dependencies (such as need to link to a database or images), has no extra code that's not relevant to your problem, but still demonstrates your problem. As a side note -- consider using BufferedImages more. – Hovercraft Full Of Eels Mar 28 '15 at 13:02
  • Have you overridden `getPreferredSize()` to return the appropriate dimensions? – kiheru Mar 28 '15 at 13:03
  • Will do. Could I try using BufferedImages instead of g.fillRect? – ArcaneEnforcer Mar 28 '15 at 13:08
  • *"Could I try using BufferedImages instead of g.fillRect?"* Yes, you sure could. Put the image in an `ImageIcon`, put the image icon into a `JLabel` and add the label to the `JScrollPane`. – Andrew Thompson Mar 28 '15 at 13:16
  • The only problem with that is that I am generating height values 0 - 10000+ and creating a jpanel that has 4097 * 4097 1x1 squares colored according to the height value. I don't have any pre-generated images. – ArcaneEnforcer Mar 28 '15 at 14:09
  • You could pre-generate the current region of view and its surrounds, and then create other views on the fly as you approach them. – Hovercraft Full Of Eels Mar 28 '15 at 14:15
  • I think a `b = new BufferedImage(4097,4097,BufferedImage.TYPE_INT_ARGB)` that is then filled with `b.setRGB(...)` should also be fine. Concerning the actual question: You have to provide more code. E.g.: Does your panel have a preferred size? Try calling `panel.setPreferredSize(new Dimension(4097,4097));` for a test (better: Override the `getPreferredSize` method - too much code for a comment...) – Marco13 Mar 28 '15 at 14:18
  • Shouldn't you be resetting `left` and `top` at the start of your drawTile method? – VGR Mar 28 '15 at 14:20
  • I am calling setPreferredSize(new Dimension(terrainSize * x, terrainSize * y); in the constructor of the JPanel. I tested with overriding getPreferredSize and it returned the correct result. – ArcaneEnforcer Mar 28 '15 at 14:21
  • @VGR That was it! Thank you for catching that. I just now realized that they will keep their values from the last call. – ArcaneEnforcer Mar 28 '15 at 14:25
  • Also consider `TexturePaint `, suggested [here](http://stackoverflow.com/a/24746585/230513). – trashgod Mar 28 '15 at 14:33

0 Answers0