0

I'm learning Java at the moment and am playing around with the Swing GUI as well as trying to understand how some of the larger roguelike dungeon builder algorithms work.

I came across this example in java for the algorithm -

[link] http://roguebasin.roguelikedevelopment.org/index.php?title=Java_Example_of_Dungeon-Building_Algorithm

And I was interested in modifying this code to display image tiles to a Swing JFrame -

//used to print the map on the screen
public void showDungeon(){
    for (int y = 0; y < ysize; y++){
        for (int x = 0; x < xsize; x++){
            //System.out.print(getCell(x, y));
            switch(getCell(x, y)){
            case tileUnused:
                System.out.print(" ");
                break;
            case tileDirtWall:
                System.out.print("+");
                break;
            case tileDirtFloor:
                System.out.print(".");
                break;
            case tileStoneWall:
                System.out.print("O");
                break;
            case tileCorridor:
                System.out.print("#");
                break;
            case tileDoor:
                System.out.print("D");
                break;
            case tileUpStairs:
                System.out.print("<");
                break;
            case tileDownStairs:
                System.out.print(">");
                break;
            case tileChest:
                System.out.print("*");
                break;
            };
        }
        if (xsize <= xmax) System.out.println();
    }
}

I was wondering what the best method would be to achieve this - by creating a new grid in the JFrame? Can someone provide me with a sample code?

Thanks.

2 Answers2

1

Suppose all your cell images have the same size.

Set GridLayout to your container and add JLabels in the grid for each tile.

OR just override paintComponent(Graphics g) method of JPanel and paint each image with proper shift x=width*column, y=height*row.

StanislavL
  • 56,971
  • 9
  • 68
  • 98
0

As long as you wish to display characters on screen, you might as well fill a single textarea with the dungeon map. Or write the map to System.out.

The moment you start displaying images is when Swing becomes handy.

Don't be shy to put a couple extra classes in, such as a class Map, with a double array of Room objects, Room being an abstract class that has concrete subclasses DirtWall, StoneFloor, etc.

And a MapView which fill a GridLayout in a JFrame with ImageIcons.

Or if you wish to stick with roguelike interface, take a look at the curses library, it's a way to position characters in a terminal window. You can create a JNI binding from Java. Or there's http://www.pitman.co.za/projects/charva/ which creates a swing-like api on top of such a binding.

flup
  • 26,937
  • 7
  • 52
  • 74