Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2005

Tip: Looking for answers? Try searching our database.

Gui Component Problems

Thread view: 
sditta - 20 Mar 2005 01:21 GMT
Hi,

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:

class EvacPanel extends JPanel
{
public EvacPanel()
{
}

// do custom painting in the paintComponent method
public void paintComponent(Graphics g)
{
// paint background
super.paintComponent(g);
int n = 0;
ImageIcon smile;
smile = new ImageIcon("smile.jpg");

while(n < 100)
{
int a = (int) (Math.random()*500);
int b = (int) (Math.random()*500);
smile.paintIcon(this,g,a,b);
n++;
}
}
}

class EvacFrame extends JFrame
{
// constructor
public EvacFrame()
{
// panel for custom painting
EvacPanel painting = new EvacPanel();

// Add panel to content pane
getContentPane().add(painting);

}
}

I have a second window (win2) that includes two radio buttons whose code
is as follows:

class RadioPanel extends JPanel

{
public RadioPanel()
{

// initialise components
JRadioButton[] radioButtons = new JRadioButton[2];

final ButtonGroup group = new ButtonGroup();

radioButtons[0] = new JRadioButton("<html>On</html>");
radioButtons[1] = new JRadioButton("<html>Off</html>");

for(int i = 0; i < 2; i++)
{
group.add(radioButtons);
}

radioButtons[0].setSelected(true);

// add components to the frame
setLayout(new GridLayout(3,2));

add(radioButtons[0]);
add(radioButtons[1]);

}

}

class RadioPanelFrame extends JFrame
{

// constructor
public RadioPanelFrame()
{
// Create panel
RadioPanel contents = new RadioPanel();

// Panel is not added directly to top-level container
// Instead, it is added to the content pane
getContentPane().add(contents);

setLocation(300,300);
// this code enables you to close the window
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}
}

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?

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?

I enclose the main class code as follows:

class Simulation
{
public static void main (String [] args)
{
EvacFrame window = new EvacFrame();
RadioPanelFrame new1 = new RadioPanelFrame();
// set size of GUI
window.setSize(500,500);

// and make it visible
window.setVisible(true);
new1.pack();
new1.setVisible(true);
}
}

Any help you give will be greatly appreciated.

sditta :)
Bjorn Abelli - 20 Mar 2005 11:01 GMT
"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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.