??? Switching Between "Map Screens" and Layering GUI Layouts

Hello~!

I have a new question. ^^ ;D

Question 1: Switch between Map Screens
What is the most effective way to switch through “Screen Types” (eg: Area Map, World Map, Battle Screen)?

I was thinking of using something similar to a JTabbedPane, but without the tabs.

Instead, switching between the “tabs” would be initiated through invisible events that executed when the player is standing on the tile.
psedocode-ish==> “event.setLocation( mapType, mapID, playerLocation[][] )”
Where mapType would be “Area Map”, “World Map”, “Menu Screen”, or “Battle Screen” etc
mapID would call the Map class to paint the map on the current “tab”
and set the avatar at playerLocation[ x ][ y ] on the map

What should I be using to achieve “screen switching”? I don’t really think a JTabbedPane would be my best option, considering I’d most likely need another JTabbedPane-ish inside the Menu Screen.

Question 2: Layout Layering
What is the best way to lay out each Screen in each “tab”?
I was thinking of using a MixedLayout in order to achieve a layering effect.
Or is there an easier way to create layers?

For Example:
Layer 1 - Background Layer containing the tiled map
Layer 2 - Objects on the tiled map (eg: a box, a table, a bed)
Layer 3 - Player and Events (eg: treasure, NPCs, exit-map-events)
Layer 4 - Player Information (eg: health bar, help menu, etc)

Q1: You could use CardLayout to switch multiple components in place

Q2: Usually you would layer them yourself. You would have to override piantComponent() on a JPanel, and then use the provided Graphics instance to draw your layers. Stay away form layouting game content as swing components using swing layout managers. Whole panels are ok, but for sprites and such, swing is inappropriate.

For a smooth experience, you should ask about “active rendering” and BufferStrategy. I can’t help you with that, cause I am no Java2D guy.

ah I didn’t even think about using a cardLayout - that makes so much sense!
I actually tried using a cardLayout to achieve the layering effect, but that didn’t work (naturally) :,(

ill def. do some more research on active rendering, thanks for the tips~!

For my games, I usually don’t use swing layout managers and instead render to one JPanel.

To switch between maps, I can call a setMap function which would load up the appropriate map.

To achieve the layering, in the draw method I explicitly layer the things being rendered by their drawing order. So I would draw the game map first, then I’d draw the player, npc’s and all other sprites, and finally I would draw the hp,mp bars etc.

Something like this (using double buffering and active rendering):

import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Game extends JFrame {

  private Player player = new Player();
  private GameMap map = new GameMap();
  private HeadsUpDisplay hud = new HeadUpDisplay();

  public Game() {  
    
    ((JPanel)getContentPane()).setPreferredSize(new Dimension(800, 600));
    setResizable(false);    
    pack();
    show();    
    
    createBufferStrategy(2);
    BufferStrategy strategy = getBufferStrategy();
    
     while(true)
     {                  
        Graphics2D g = (Graphics2D) strategy.getDrawGraphics();        
        update();
        draw(g);
        strategy.show();       

        try 
        {
          Thread.sleep(20);  
        }
        catch(InterruptedException ex){}
     }
    
    }

    private void update()
    {
         player.update();

         if (player.onTile(map.EXIT_TILE))
              map.setMap(map.OverworldMap);
    }

    private void draw(Graphics2D g)
    {
          g.setColor(Color.BLACK);
          g.fillRect(0,0,800,600);

          map.draw(g);
          player.draw(g);
          hud.draw(g);
    }

  }

}