How to show a rectangle after its size changed?

L

Leo Smith

Dear All,

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:

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
????
}

How can I make my panel display the enlarged two rectangles? Should I
remove all the old two rectangles first? I know how to add an object to
a panel, I don't know how to remove an object from a panel. Or something
repaint() method will update its size?

Thank you.
 
T

Thomas Hawtin

Leo said:
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
 
O

Oliver Wong

Thomas Hawtin said:
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.

No, I think the OP really means rectangles. He's probably working on the
project mentioned here:
http://groups.google.com/group/comp..._frm/thread/bafc37eb87d19f41/ccf966509fde945f

I have no idea how he managed to get layout managers entangled into all
of this though.
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;
}

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top