0

I have a 2D Tile array map that is 100 x 100. I can draw the map out and add it to a JFrame but the frame is very large and the map doesn't fit on the frame. I want to have the map in a window and as you move the mouse around, the frame moves and you see more of the map. So I am trying to add a JPanel to a JScrollPane to accomplish this. The Panel would have the whole map and the JScrollPane would show a section of it and change as you moved around. However I am not sure how to add my map image to the JPanel, nor how to add the ScrollPane to the Frame. Or if I am even setting up the ScrollPane correctly. Right now Im getting a null pointer exception when I try to set the dimensions. Here my class where I am trying to test all the drawing stuff.

package view;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Observable;
import java.util.Observer;
import java.util.Timer;
import java.util.TimerTask;

import model.*;

import javax.swing.*;
public class DrawMap extends JPanel implements Observer{
    private static  Map map = Map.getMap(); 
    private static Tile[][] field = map.getField();
    private BufferedImage img = map.getMapImage();
    private JScrollPane wrapper;
    private JPanel panel;
    /**
     * Test for drawing the Map
     */
    public DrawMap() {
        init();


    }
    public void init(){
        map.getMapImage();
        panel = new JPanel();
        for(int i = 0; i < 100; i ++){
            for(int j = 0; j < 100; j++){
                field[i][j].addObserver(this);
            }
        }
        panel.setSize(new Dimension(32 * 100, 32 * 100));
        panel.setPreferredSize(new Dimension(32 * 100, 32 * 100));

        wrapper.setSize(new Dimension(600, 400));
        wrapper.setPreferredSize(new Dimension(600, 400));
        wrapper.setViewportView(new JViewport().add(panel));
        this.add(wrapper);
    }
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        g.drawImage(img,0,0,null);

    }

    public static void main(String[] args){
        JFrame frame = new JFrame("GameMap");
        DrawMap draw = new DrawMap();
        frame.add(draw);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(3200,3200);
        frame.setVisible(true);

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        field[1][1].setAgent();
    }
    @Override
    public void update(Observable o, Object arg) {
        System.out.println("About to update tile");
        Graphics2D g = (Graphics2D) img.getGraphics();
        ((Tile) arg).drawTile(g, ((Tile) arg).getXChord() * 32, ((Tile) arg).getYChord() * 32);
        //map.drawMap();
        //img = map.getMapImage();
        repaint();
        System.out.println("Tile was changed");

    }

}

Any help would be appreciated, thanks!

Shoomba
  • 1
  • 1
  • 2
  • "*However I am not sure how to add my map image to the JPanel, nor how to add the ScrollPane to the Frame.*" `JScrollPane sp = new JScrollPane(mapPanel)` will add the panel on which you draw the map to a scroll pane. `frame.add(sp)` will add the scroll pane to the frame. – user1803551 May 04 '15 at 00:14
  • 1
    You are calling `setSize` when you shouldn't. Let the layout manager handle this for you when i comes to the components. For the frame, call `pack()` instead. – user1803551 May 04 '15 at 00:16
  • 1
    *"Right now Im getting a null pointer exception when I try to set the dimensions"* -> That's because you never initialise `wrapper`. You shouldn't be setting the `preferredSize` of the `JScrollPane`, see [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi) for more details. Instead, you should be using the `Scrollable` interface to allow the `JScrollPane` to make better decisions about how to display your component – MadProgrammer May 04 '15 at 00:32
  • `wrapper.setViewportView(new JViewport().add(panel));` is wrong and should just be `wrapper.setViewportView(panel)` – MadProgrammer May 04 '15 at 00:32

0 Answers0