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.
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.