 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Guest
|
Posted: Mon Aug 21, 2006 4:18 am Post subject: Please help the GridLayout problem |
|
|
Hi all,
I am tring to add several small JPanel to a big JPanel (using
GridLayout), and display image on these small JPanel (By overiding the
paintComponent method), but I can't get any images displayed. The brief
codes are showing below:
public class smallPanel extends JPanel{
public smallPanel(Image aImage){
super();
this.image = aImage;
this.repaint();
}
......
public void paintComponent(Graphics g){
g.drawImage(this.image, 0, 0, this)
}
}
Now, I added instance of smallPanel to a bigPanel with GridLayout:
public class frame extends JFrame{
.....
JPanel bigPanel = new JPanel(new GridLayout(1, 2, 2, 2));
this.add(bigPanel);
bigPanel.add(new smallPanel(aImage));
bigPanel.add(new smallPanel(bImage));
.....
this.repaint();
}
Is there something with these codes? Why JPanel weren't dispalying
those images, even the paintComponent() method was never been invoked
once.
BTW, the size of smallPanels are null after the smallPanel has been
initialized.
Please help.
Many thanks. |
|
| Back to top |
|
 |
Knute Johnson Guest
|
Posted: Mon Aug 21, 2006 4:43 am Post subject: Re: Please help the GridLayout problem |
|
|
shangguanlinqing (AT) hotmail (DOT) com wrote:
| Quote: | Hi all,
I am tring to add several small JPanel to a big JPanel (using
GridLayout), and display image on these small JPanel (By overiding the
paintComponent method), but I can't get any images displayed. The brief
codes are showing below:
public class smallPanel extends JPanel{
public smallPanel(Image aImage){
super();
this.image = aImage;
this.repaint();
}
.....
public void paintComponent(Graphics g){
g.drawImage(this.image, 0, 0, this)
}
}
Now, I added instance of smallPanel to a bigPanel with GridLayout:
public class frame extends JFrame{
.....
JPanel bigPanel = new JPanel(new GridLayout(1, 2, 2, 2));
this.add(bigPanel);
bigPanel.add(new smallPanel(aImage));
bigPanel.add(new smallPanel(bImage));
.....
this.repaint();
}
Is there something with these codes? Why JPanel weren't dispalying
those images, even the paintComponent() method was never been invoked
once.
BTW, the size of smallPanels are null after the smallPanel has been
initialized.
Please help.
Many thanks.
|
Well you've got a couple of problems here. The biggest is that the
layout manager is setting the size of you JPanel to 0x0. It won't paint
if it isn't visible or the size is 0x0, hence no calls to paintComponent.
Your second problem requires asking another question. Do you need to
use the ImageObserver/Producer pattern for your images or can you use a
MediaTracker or even better the ImageIO classes in 1.4? If you can you
will be able to figure out the size of your images before you have to
paint them so you can tell your JPanel what size it should be. While
the cognoscenti will wail about not using layout managers, the sad truth
is that when you use JPanels with most layout managers you need to
specify a preferred size for them or they won't display as you have
found out.
Also remember that JFrame is a container so you don't need another
JPanel to hold the JPanels that have the images. If you are always
going to display the images at the same size you can probably get away
with only one JPanel to hold all of your images.
Another suggestion is to learn to use the GridBagLayout manager as
quickly as you can. It is by far the most powerful and configurable
layout manager. It is also the most difficult to learn though.
--
Knute Johnson
email s/nospam/knute/ |
|
| Back to top |
|
 |
Andrew Thompson Guest
|
Posted: Mon Aug 21, 2006 5:18 am Post subject: Re: Please help the GridLayout problem |
|
|
shangguanlinqing (AT) hotmail (DOT) com wrote:
| Quote: | Hi all,
I am tring to add several small JPanel to a big JPanel (using
GridLayout), and display image on these small JPanel (By overiding the
paintComponent method), but I can't get any images displayed. The brief
codes are showing below:
public class smallPanel extends JPanel{
|
Using common nomenclature, that should be classname
SmallPanel
| Quote: | Is there something with these codes?
|
There is no mention of how you load the images,
ImageTracker?
| Quote: | ..Why JPanel weren't dispalying
those images,
|
Why are you using JPanels for the images?
A JLabel is well suited to displaying images.
| Quote: | ...even the paintComponent() method was never been invoked
once.
|
If you fail to solve the problem, I suggest you post a short
but complete example of what you are doing..
<http://www.physci.org/codes/sscce/>
Andrew T. |
|
| Back to top |
|
 |
Babu Kalakrishnan Guest
|
Posted: Wed Aug 23, 2006 7:56 pm Post subject: Re: Please help the GridLayout problem |
|
|
shangguanlinqing (AT) hotmail (DOT) com wrote:
| Quote: | Hi all,
I am tring to add several small JPanel to a big JPanel (using
GridLayout), and display image on these small JPanel (By overiding the
paintComponent method), but I can't get any images displayed. The brief
codes are showing below:
public class smallPanel extends JPanel{
public smallPanel(Image aImage){
super();
this.image = aImage;
this.repaint();
}
|
The call to repaint() here is unnecessary. When your component becomes
visible, paint() will be automatically by the framework.
| Quote: |
public void paintComponent(Graphics g){
g.drawImage(this.image, 0, 0, this)
}
}
|
As Knute mentioned, JPanels by default don't have a preferred size. So
code to set the preferred size woudl be useful (though not an absolute
must in this case because of the LayoutManagers that you're using - see
below.
| Quote: | Now, I added instance of smallPanel to a bigPanel with GridLayout:
public class frame extends JFrame{
.....
JPanel bigPanel = new JPanel(new GridLayout(1, 2, 2, 2));
this.add(bigPanel);
bigPanel.add(new smallPanel(aImage));
bigPanel.add(new smallPanel(bImage));
.....
this.repaint();
}
|
Your major problem is the second line (this.add(bigPanel) above. One
should never add components directly to a JFrame. You always add it to
its contentPane. So the code should be :
getContentPane().add(bigPanel,BorderLayout.CENTER);
Or alternately, if the you could set the contentPane as your panel
itself - with the code :
setContentPane(bigPanel);
Now for the size issue. The swing framework doesn't know how big any of
your panels should be nor how big the JFrame should be. Normally one
would call pack() on the JFrame to cause the nested LayoutManagers to
determine the optimum size required. But in this case, you need to do
either of the following :
a) Set a preferred size for bigPanel, and call pack() on the JFrame
or
b) explicitly set the size of the JFrame
Now since bigPanel being the CENTER component of the contentPane will
occupy the entire client area of the frame (BorderLayout is the default
layout for the contentPane - the reason why you didn't have to set it
explicitly). And since the GridLayout layoutManager's layout strategy
is to make all its children as big as the size of the grid cell, the
small panels will be set to appropriate sizes (with all cells being of
equal size) - whether or not its preferred size has been set. If you
were using other Layout managers, you would have needed to set
preferred sizes on the smaller panels also - so it is generally a
recommended practice to do so.
And by the way as Andrew pointed out, try to follow Java naming
conventions - names of classes should start with capital letters.
BK |
|
| Back to top |
|
 |
Guest
|
Posted: Wed Aug 23, 2006 10:26 pm Post subject: Re: Please help the GridLayout problem |
|
|
Babu, Knute, Andraw:
Thank all of you. I have been getting this issue since the first day I
invovled in this project. As you can see, I was trying to create a kind
of facility group that can hold different level layout. But the final
aim for me is displaying series images on a container, which can
perform two (or three) levels layout.
i.e.
0. set layout of main form as GridBagLayout.
1. display the image group one (contains 4 images) at (0, 0) position
of the main form, and display the group two (contains 6 images) at (0,
1) postion of the main form.
2. For the image group one, I want to display these 4 images in 2 rows
by 2 columns at (0, 0) position of the main form
3. For the image group two, I want to display these 6 image in 3 rows
by 2 columns at (0, 1) position of the main form
4. The real challenge is the layout format of main form and group
images can be changed by user randomly.
So, I thought initally that it would be easier for me to use several
dynamic crated JPanel, and its associated layout to solve this task,
but the fact was the application didn't perform as it's been designed
for.
Guys, as you mentioned before that my problem is related to the defalut
size of JPanel and size of JPanel before painting. But would there be
alternative way to work around this issue by only setting up the
defalut size, and then let JPanel's properties listener to
set the size of the JPanel (or you have to phisically calculate the
size of Panel and pass it to the JPanel constructor).
The size of image is detectable and associated affineTransform instance
can be crated by different class once the size of JPanel is provided.
Guys, what is your suggestion regards this task.
Many thanks
Linqing |
|
| Back to top |
|
 |
Babu Kalakrishnan Guest
|
Posted: Wed Aug 23, 2006 11:15 pm Post subject: Re: Please help the GridLayout problem |
|
|
shangguanlinqing (AT) hotmail (DOT) com wrote:
| Quote: | Guys, as you mentioned before that my problem is related to the defalut
size of JPanel and size of JPanel before painting. But would there be
alternative way to work around this issue by only setting up the
defalut size, and then let JPanel's properties listener to
set the size of the JPanel (or you have to phisically calculate the
size of Panel and pass it to the JPanel constructor).
|
Look at the API docs for the ImageObserver interface. Swing loads
images in a background thread, and fires events during the load
process, and at some point you should be able to get the size of the
image. (Usually much before the image loads completely). You can always
call setPreferredSize on the panel once you've obtained the required
size. (Also look at the docs of the variants of the drawImage method
that take an additional ImageObserver argument - that's how you can
register yourself as a listener to the Image loading process).
A couple of caveats here -1) If you change the preferredSize of
components after they have been realized, don't expect parent
containers to automagically resize themselves to honour these changes -
you have to "revalidate" these containers explicitly and (2) If your
code acts as the ImageObserver, it is your responsibility to make sure
that you call repaint() on the container. (The default ImageObserver in
drawImage methods that don't take this argument is the Component
itself, and it performs the task of calling repaint()).
BK |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|