I am trying to get a lot of JButtons to display on top of a background image of the world in a JFrame.
So far, I've tried a lot of things. I tried overwriting the paint function of the JFrame to drawImage the image of the world. But whenever I repainted, the buttons got painted very quickly and then covered by the background image.
I then tried to create a JLayeredPane. Here is an excerpt of the code:
Dimension layeredPaneSize = new Dimension(1025, 740);
JLayeredPane layeredPane = new JLayeredPane();
JButton myWorldButton = new JButton (big_world);
myWorldButton.setBounds (10, 10, 1000, 700);
layeredPane.add(myWorldButton, JLayeredPane.DEFAULT_LAYER, 0);
tmp1 = 0;
tmp2 = 0;
for (int s = 0; s < 5; ++s)
world[s].myButton.setBounds(tmp1,tmp2, 8, 8);
layeredPane.add(world[s].myButton, JLayeredPane.POPUP_LAYER, 0);
tmp1 = tmp1 + 10;
tmp2 = tmp2 + 10;
Checkbox less_than = new Checkbox (" <");
less_than.setBounds(20, 650, 10, 10);
layeredPane.add(less_than, JLayeredPane.MODAL_LAYER, 0);
And the only thing that happens is that the checkbox displays. Does anybody have any idea how I should proceed/what I'm doing wrong? Please let me know. Thanks
The only way to do this (using std. components and overwriting the
paint/update) that I know of is to set the layout for the container (frame) to null,
use the setBounds(x,y,w,h) for each component (button) that is added to the
container, and to overwrite the update method where only the background image is
In general, mixing the default container layout/paint job with ones own
paint/update implementations seems to be a bad idea. Its a very frequent
issue in this forum.
I beleive that the best to do in such cases is to implement ones own buttons,
i.e. coding graphical objects with the desired responsiveness and layout.
Once done, these can be reused.