2

The scenario: I have a UI that contains a JPanel (call it topGrid) with a grid layout in a JFrame at the top level. Within topGrid, I have placed another JPanel (midGrid) with grid layout. Inside midGrid, is another JPanel (bottomGrid) that has a JLabel that I populate with images depending on an array and what their instance is within that array.

The goal: I would like the topGrid to center its view on a specific object found in bottomGrid. (Picture a game that as the player icon moves, the game's grid moves to center on that icon and also when the game is started it is already centered for the user.)

I've considered getting the Point from bottomGrid and trying to pass it over to topGrid but doesn't seem to pull the correct information. The only way i know to find where the player is, is to iterate through all the components and check instances. this would have to be done once for the topGrid and again for midGrid to find the player at bottomGrid. then pass the Point data. Then use setLocation() on the appropriate JPanel minus the distance from the center.

Has anyone else tried this and have a more effective or elegant way to go about it? What other options could I explore?

Thanks for any feedback.

Creating the grid within topGrid's JPanel:

public void createTopGrid()
{
    int rows = galaxy.getNumRows();
    int columns = galaxy.getNumColumns();

    pnlGrid.removeAll();
    pnlGrid.setLayout(new GridLayout(rows, columns));

    for (int row = 0; row < rows; row++)
    {
        for (int col = 0; col < columns; col++)
        {
            Position pos = new Position(galaxy, row, col);
            Sector sector = galaxy.getSector(pos);
            GalaxySector sectorUI = new GalaxySector(sector);
            pnlGrid.add(sectorUI);
        }
    }
}

Creating the grid within midGrid's JPanel:

public void createOccupantIcons()
{
    pnlGridOccupants.removeAll();

    Occupant[] occs = sector.getOccupantsAsArray();

    for ( Occupant occ : occs )
    {
         GalaxyOccupant occupant = new GalaxyOccupant(occ, sector);
         pnlGridOccupants.add(occupant);
    }
}

The Image icons for each occupant in the midGrid are pulled from an IconRep String in the model in the bottomGrid class' JPanel and added into a JLabel as needed in FlowLayout.

For visual reference:

image

Where green square is topGrid JPanel, red squares are midGrid JPanel, and the black square is the bottomGrid JPanel with the white circle for the player image inside a JLabel. The blue circle represents a viewport the user will see the game through and is where I want the player icon to be centered to. Currently the user can move the grid's using very inelegant buttons in the area around the viewport. That might be sufficient but at the start of the game the player has to move the grid around until they can locate their icon.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
fakataha
  • 785
  • 6
  • 31

2 Answers2

3

You might also look at JScrollNavigator, examined here. It would allow you to navigate on a thumbnail image of your entire world, seen at full size in an adjacent scroll pane.

image

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • WOW, that's actually really a nice option! I could see using that as a mini-map type implementation. unfortunately, I've already submitted the current UI and just need to be able to center the game's grid on the panel containing the player to be finished with this program. – fakataha Oct 13 '12 at 01:03
  • Oh, I understood that you wanted "other options." Please edit your question to include an [sscce](http://sscce.org/) and screenshot that illustrates what options are permitted at this stage of your development. – trashgod Oct 13 '12 at 01:13
  • @Mad: I'm a little stymied by the paucity of clear requirements. If you see an opportunity, please don't hesitate to offer a competing answer. – trashgod Oct 13 '12 at 01:34
  • very sorry if I wasn't clear enough. This is my first time posting to this site. I've updated the original post with more info. – fakataha Oct 13 '12 at 20:29
1

Off the top of my head, I would store all the references you want to in some kind of model.

You could use this model to update the views based on selection requirements.

This allows the you to centralise the logic for finding and updating the elements without knowing or caring out the other UI elements

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • +1 for loose coupling. My first thought was be to make each `bottomGrid` element a button whose [`Action`](http://docs.oracle.com/javase/tutorial/uiswing/misc/action.html) updates the `topGrid`, but we really don't have enough information to visualize the approach. – trashgod Oct 13 '12 at 08:25