Search code examples
javaswingjframepaintcomponent

How to make a simple mini map for a Java rpg game


I have an array that holds the names and then displays the appropriate graphics using:

public void paintComponent(Graphics g){

for(int i = 0; i<20; i++)
{
    for(int j = 0; j<20; j++)
    {   
        if(testMap.getData(i+(rowX-7), j+(columnY-7)).indexOf("rock") >= 0)
        {
            g.drawImage(image3, j*50 + 5*add, i*50 + 5*add2, 50, 50, this); 
        }   
    }
}

This works well displaying the actual graphics of my game. However, for my minimap, I do the same thing, with different values and increased numbers in the 'i' and 'j' in the for loop. This creates a smaller version of the map with a bigger scope, which leaves me with this. (Note, I only included one of the drawImage() methods, I removed the rest here to make the code more readable):

my rpg game

This is pretty much my desired effect (aside from the positioning, which I can change easily), however, this only shows a smaller version of what I already see on the screen. Any larger than about 20 x20, though, and the game begins to lag heavily -- probably something to do with the terrible way that I coded it.

I have tried replacing the images with squares using fillRect, but it does not help the issue.

Here is the code of my main class if anybody would like to take a look: http://pastebin.com/eEQWs2DS

The for loop within the paintComponent method begins at around line 3160, the loop for the main display is around 2678. I set up the Jframe at around 1260.

So, with all that said, basically my question is this:

Is there a better, more efficient way to create my minimap? My idea was to generate an image at the beginning of the program so that the game wouldn't have to recalculate every time the frame refreshes. I could create the map in advance, but I would have to manually update that every time I changed the map, which is definitely a hassle. I am having trouble researching how to do the former. My other idea is to slow the refresh rate of the minimap only, but I also do not know how to do that.

Other than that, if there are any simple fixes to the lag issue, please enlighten me. I apologize for the very, very messy code -- This is my first time programming anything with a display so I sort of just...hacked at it until it worked.


Solution

  • I don't know how easy this would be with your implementation, but instead of drawing an image, perhaps you could draw a square of a certain color based on what type of tile it should be?

    For instance if you're looping through your list of tiles and you find a grass tile, you would first draw the grass tile at its proper location, then draw a smaller green square on the minimap.

    The downside to this is that first you'd have to define what colors to use for the tiles, or perhaps when you load the tiles you can compute an average color for each one, and then just use that. Another issue is that the houses may not translate well on to the minimap, since the tiles have much more detail. You could draw some kind of house icon on the minimap instead of actually drawing any of the house tiles, though.

    Basically, you want to use simpler representations of the objects in your map on the minimap, since it's smaller and less detail can be drawn anyway.