Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

AWT Frame with constant height-width ratio



(I want to post this for the archives. It's an answer to a question someone may someday search for.)


I just programmed an awt Frame which resizes itself to conform to a certain aspect ratio. It is a media popup window which displays either an image or a movie. I wanted a frame which would constrain it's dimensions to the original ratio.


I tried a number of approaches and the one that finally worked for me was by setting a component listener on the frame, listening for resize events; I would then reset the size to conform to the aspect ratio. The problem is that this didn't work -- the window would zoom back to the drag-position size (*almost* always). I discovered thru experimentation that sleeping the thread caused MY resize to take priority over (what I assume to be) OSX's resize. Twenty milliseconds does it for me (ten *ususally* works) but that is probably variable.

If there is a prescribed way to do this, I'd be keen to know.


peace Nicholas


mediaPopup.addComponentListener( new ComponentAdapter() {
private int RESIZE_SLEEP_DELAY = 10;
private float aspect = 1.0f;
private boolean isInitialized = false;
public void componentResized( final ComponentEvent e ) {
if( ! isInitialized ) {
Dimension d = mediaPopup.getSize();
aspect = ((float)d.width) / ((float)d.height);
isInitialized = true;
}


                        try {
                            Thread.sleep( RESIZE_SLEEP_DELAY );
                        } catch( Exception exc ) {

                        }

Rectangle r = e.getComponent().getBounds();
mediaPopup.setSize( (int) (r.height * aspect), r.height );
}
} );


_______________________________________________
Do not post admin requests to the list. They will be ignored.
Java-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/java-dev/email@hidden

This email sent to email@hidden


Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.