2

I'm making an utility to define footholds in a map for my game and i am displaying the preview of the map inside a JPanel that's inside a JScrollPane. I would like to be able to scroll when the map is bigger than the JScrollPane. How should i do that?

Here's a picture

z

Bigger version

The image of the map fits inside of the scrollpane but the image is actually larger so we don't get to see the whole map.

Some code:

The JPanel class which holds the image inside of the scrollPane

public class MapDisplay extends JPanel {

public MapDisplay() {
}

@Override
protected void paintComponent(Graphics g) {
    super.paintComponents(g);
    Graphics2D g2d = (Graphics2D) g;
    g2d.setColor(new Color(0xDFDFDF));
    g2d.fillRect(0, 0, 2000, 2000);
    if (mapinfo != null) {
    if (mapinfo.img != null) {
        g2d.drawImage(mapinfo.img, 0, 0, null);
    }
    }
    repaint();
}
}

the scrollPane's declaration

final JScrollPane scrollPane_1 = new JScrollPane(mapDisplay);

scrollPane_1.setBounds(10, 11, 989, 553);
getContentPane().add(scrollPane_1);
add(scrollPane_1);
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Nicolas Martel
  • 1,641
  • 3
  • 19
  • 34
  • `"How should i do that?"` -- by coding things correctly. It's kind of difficult to know what you're doing wrong though without code. Perhaps you're trying to set the size of the component that is held by the JScrollPane and displays the picture? Perhaps you're not adding the JScrollPane to the GUI? Hard to guess here. – Hovercraft Full Of Eels Sep 22 '12 at 19:47
  • I'm gonna add a bit of code, give me a second. – Nicolas Martel Sep 22 '12 at 19:48
  • Try this [`ImageApp`](http://stackoverflow.com/a/5129757/230513). – trashgod Sep 22 '12 at 19:48
  • 3
    Your call to `setBounds(...)` worries me and suggests that you may not be using layout managers and that is a dangerous thing, and is quite possibly the source of your problem. I suggest that you use nested containers that use appropriate layout managers and I'll bet this will if not solve your problem, help things immensely. Also, don't call `repaint()` from within a drawing method as this is a very bad thing to do. – Hovercraft Full Of Eels Sep 22 '12 at 19:52
  • I'm using absolute layout since i like being able to place things wherever i want. – Nicolas Martel Sep 22 '12 at 20:02
  • 2
    @NicolasMartel: but it's harming your GUI's ability to scroll. You're far better off using the layout managers as they will do the work of laying out for you which makes for much easier GUI modification as well as allows your GUI's to look good in any OS/Platform. – Hovercraft Full Of Eels Sep 22 '12 at 21:44
  • It's only a quick development tool that is only intended for me to use so as long as it looks good on my side, i can care less. But thanks for the tip, i'll try to remember that in case i work on a swing GUI i intend others to use. – Nicolas Martel Sep 23 '12 at 00:51

1 Answers1

3

The MapDisplay has to 'tell' the JScrollPane how big it wants to be. You can do so by implementing the getPreferredSize method of MapDisplay.

public Dimension getPreferredSize() {
    return new Dimension(widthOfMap, heightOfMap);
}

Further, do not call repaint() from inside the paintComponent() method, as this results in an infinite loop.

tobias_k
  • 81,265
  • 12
  • 120
  • 179
  • Thanks, that worked perfectly. One thing though, i assume getPreferredSize is called only once when the program is started. Is there a way to update and recall it later on to recalculate the size ? – Nicolas Martel Sep 22 '12 at 21:31
  • getPrefferedSize is called when ever the aren't container needs to be layout again. A call to invalidate will usually mark the component (and parent) as needing to be updated – MadProgrammer Sep 22 '12 at 21:44