How to move around on a map with mouse and/or keys (RTS-like game)
As the title suggests, I'm making a开发者_StackOverflow社区 game where there is a map, the map is of course bigger than could fit on the screen as most RTS games :) So, any suggestions on how to go about this? The map is a big JFrame with images drawn on it.
I highly doubt drawing the WHOLE map in memory and just choosing the part to display is scalable. You want to only draw what the user sees instead, in window-coordinates.
I don't know how big your map is, but you could add the map to a JScrollPane. Hide the vertical/horizontal scrollbars. Then you can position the map by using the viewport of the scrollpane.
Now I changed my map (former JFrame) to a JPanel, adding it to a JScrollPane that is added to the contentPane of the JFrame, see code below:
public Main() //extends JFrame
{
setDefaultCloseOperation(this.EXIT_ON_CLOSE);
Board b = new Board(db); //extends JPanel
setSize(400, 200); //size of the JFrame, JPanels size is set in its own constructor
jsp = new JScrollPane(b); //adding the Board to the JScrollPane
getContentPane().add(jsp); //adding the JScrollPane to the contentPane
setVisible(true);
}
Now what happens is the Board shows up, with all cells at the right places, I can interact with the map as usual, but no scrolling to be seen anywhere. Pointers or solution? <3
精彩评论