Adding panels to a central panel.
I have to a lot of sub-panels to a larger 17, 17 gridlayout panel. However a lot of the panels I want to add, are the same. I have gotten the code to work, but only when a new instance of the same subpanel is created for every entrance in the main panel.
Code work below is working as intended, but is very slow.
Code java:
for(int i = 0; i < 17; i++){
for(int y = 0; y < 17; y++){
if((i - 8 + heroX < 0) || (y - 8 + heroY < 0) ||
(i - 8 + heroX >= 100) || (y - 8 + heroY >= 100)){
gfxOOBPanel = new JPanel(); // I want to move this construct.
gfxOOBPanel.setBackground(new Color(0,0,0)); // same as above
boardPanel.add(gfxOOBPanel); // this should be the only code
// inside the loops
}
else if{
... // Similar constructs as above, adding more panels
}
}
}
}
Naturally I want to pull out the object creation out of the loops, so i dont create 17x17 objects everytime the method is run.
By pulling it out however the resulting main panel consist of only 1 of each type subpanel, instead of 17x17 grid consisting of the same 4 subpanels used several times.
TLDR: Adding the same object reference to a panel twice, seems to result in only one added component. (HashMap clash maybe)
Hope the code explains my problem. The whole gui code is to long to insert here.
Re: Adding panels to a central panel.
Quote:
Adding the same object reference to a panel twice, seems to result in only one added component
A component can only have ONE parent.
Why do you create the 17x17 grid every time you call the method? Can their creation be moved to a method that is only called once?
Re: Adding panels to a central panel.
Quote:
Originally Posted by
Norm
A component can only have ONE parent.
Why do you create the 17x17 grid every time you call the method? Can their creation be moved to a method that is only called once?
The 17x17 panels represents part of a gameworld, which is made up of a large 2d array of Tiles.
So it reads from the large array, if a certain tile holds a Monster or a water or whatever.
If the tile contains a monster for example, the corresponding space in the 17x17 gui grid inserts an icon with an image.
Since everytime you move around the gameworld, the gui grid has to be updated the heavy object creation is not feasable, even though it works as intended(to slow though).
The Graphics/Graphics2D framework is proberly the way to go here, I just havent worked with it before, so was looking for a quickfix, due to not wanting to read up on it.
With the one parent rule, Im gonna have to it seems. Thanks for looking into it though
Re: Adding panels to a central panel.
I don't understand why you need to create the 17x17 grid more than once.
Once it is created you should be able to change the individual panels as needed.