"sditta" wrote...
> I was wondering if you could help me. I am currently working on a program
> and I'm having a few problems with getting the gui components to work
> properly. I have two windows, one that displays a picture to the screen
> one hundred times at random positions (win1). The code for this is shown
> here:
[snipped code]
> Whenever the win2 is created when I run the program it sits on top of win1
> and when I move it away the program automatically redraws the 100 images
> again at new random positions. It there anyway I can stop this from
> happening so that the contents of win1 remain constant even when win2
> moves over it?
There are several different solutions to this...
One solution follows:
Move the "creation" of the coordinates out of the method paintComponent,
into e.g. the constructor, where you don't actually paint them, but store
them into a List of some kind.
class EvacPanel extends JPanel
{
ArrayList points = new ArrayList();
public EvacPanel()
{
int n = 0;
while(n < 100)
{
int a = (int) (Math.random()*500);
int b = (int) (Math.random()*500);
points.add(new Point(a, b);
n++;
}
}
...
In the paintComponent method you then simply iterate through that list to
paint the icons. In that way you get them drawn at the same positions every
time.
> Also I would like the radio buttons in win2 to be able to close win1 at
> any time and to make it appear on screen again with the contents of win1
> looking the same before it was closed. Is there anyway this can be done?
With the solution suggested above, the contents will be the same every time
that window is redrawn.
To make it dissapear and appear again, through the changing of the
radiobutton, there are more things to think of...
First, you need to implement some listener (e.g. an ActionListener) for the
radiobuttons.
Secondly, you'll need a reference to the window you want to hide/show from
that ActionListener.
One suggestion could be to change the constructor og your RadioPanelFrame,
so it takes an EvacFrame as argument, and hence pass it upon instantiation.
EvacFrame window = new EvacFrame();
RadioPanelFrame new1 = new RadioPanelFrame(window);
I hope these hints at least can get you into the right direction.
// Bjorn A
Bjorn Abelli - 20 Mar 2005 11:05 GMT
"Bjorn Abelli" wrote...
> points.add(new Point(a, b);
Sorry, i noticed that I missed a parenthesis there... ;-)
points.add( new Point(a, b) );
// Bjorn A