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 / General / May 2006

Tip: Looking for answers? Try searching our database.

How to show a rectangle after its size changed?

Thread view: 
Leo Smith - 30 May 2006 15:08 GMT
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.
Thomas Hawtin - 30 May 2006 15:24 GMT
> 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


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.