> I am writing a subclass of JPanel(subclass name: JPanelClickagble). This
> new class has a linkedlist, which contains objects of class Rectangle.
>
> Suppose right now, the linkedlist containes two objects of Rectangle and
> the panel displays the two rectangles(flowlayout manager). If I made
> each rectangle 10 times higher, 10 times wider in a method:
You can't directly display a Rectangle as it isn't a Component. I assume
you mean rectangular components of some description that have the panel
as their parent.
> public void enLarge() {
> while (i.hasNext()) {
> i.next().height *= 10; //rectangle’s height increase by 10
> i.next().width *= 10; //width increases by 10 times
> } //end of while loop
You are incrementing the iterator twice in the loop. And apparently not
creating a new one. The enhanced for loop makes these sort of mistakes
less likely:
for (RectangularComponent comp : componentList) {
comp.width *= 10;
comp.height *= 10;
}
If you have to use Iterators directly, do one thing at a time. Don't
embed the next within a larger statement:
for (
Iterator<RectangularComponent> iter = componentList.iterator();
iter.hasNext();
) {
final RectangularComponent comp = iter.next();
comp.width *= 10;
comp.height *= 10;
}
If you've just poked some unencapsulated variables, then the component
has no way to know that it should be redrawn. A more conventional way to
operate would be to get (a copy of) the component's preferred size, and
set it back updated:
Dimension size = comp.getPreferredSize();
size.width *= 10;
size.height *= 10;
comp.setPreferredSize(size);
The component now know it is invalid (comp.isValid() will return false).
Also it will have informed the panel it is invalid, and the panels
parent, all the way up.
Once you done all that, call revalidate() on the panel. This will,
shortly after, relayout and then repaint the panel appropriately.
Tom Hawtin

Signature
Unemployed English Java programmer
http://jroller.com/page/tackline/
Oliver Wong - 30 May 2006 17:04 GMT
>> I am writing a subclass of JPanel(subclass name: JPanelClickagble). This
>> new class has a linkedlist, which contains objects of class Rectangle.
[quoted text clipped - 6 lines]
> you mean rectangular components of some description that have the panel as
> their parent.
No, I think the OP really means rectangles. He's probably working on the
project mentioned here:
http://groups.google.com/group/comp.lang.java.programmer/browse_frm/thread/bafc3
7eb87d19f41/ccf966509fde945f
I have no idea how he managed to get layout managers entangled into all
of this though.
>> public void enLarge() {
>> while (i.hasNext()) {
[quoted text clipped - 22 lines]
> comp.height *= 10;
> }
To the OP: Assuming you really are working with Rectangles, and not with
GUI widget/components, this above advice is relevant to you, but the rest
isn't. Instead, you'll probably want to do custom painting directly onto a
JPanel, and not involve other components or layout managers or anything like
that. Read
http://java.sun.com/docs/books/tutorial/uiswing/14painting/index.html
- Oliver