Home > > Fit image to canvas at load

Fit image to canvas at load

I have subclassed JPanel in order to make my own image canvas. I have implemented zooming in and out, and, since the images I'm going to be using are generally in the dimensions of 3000x2000, I'd like an image to fit to the space available when it's loaded. This would be done with zoomfactor=min(canvasw/imagew,canvash/imageh), but here's the catch: getWidth and getHeight return 0. Unless, as I soon learned from our friend Google, getWidth and getHeight are called in paintComponent. However, I need a zoomfactor to paint, and I need to paint in order to to obtain my zoomfactor. So I'm trapped, I guess. What do I do?
On a side note, what's the deal with getWidth/Height? Can they only be called in paintComponent? And, why, although put in a JScrollPane, and drawing an image much larger than the open screen space, my widget doesn't scroll?
Components don't have a size until the GUI is visible.
I don't see the problem. I would suspect your paintComponent() method paints the image so the width and height are available at that time.
Your application should set the zoom factor and then invoke repaint() to cause the panel to repaint the image.
So at startup time you just set the zoom factor to minimum and the paintComponent() method should know how to interpret that value.
Read the other 2 answers
Hi All, I want to fit an image inside a canvas. Making x and y to 0 of image is not working. My problem is I am rotating image at 90 and I want to save that image. If i save normally it is not taking rotation. So I am adding that image in a canvas an
Hello! I have started quite a complicated Xcelsius dashboard (15 pages - each page contains one radio button with 2 options that link to a page with 4-6 charts each). When I continue to build the additional pages, there comes a point when the file do
Hey Muse Forum, I've built a site that uses a 4500 px wide canvas, taking advantage of parallax scrolling. All my content is on this one page, and users will float around with some strategically placed anchors and navigation "menus." I'm trying
Hi, It is my blog: http://www.networkarte.com/blog And those are the mobile and tablet versions: blog & blog. As you will notice, the image is too large for mobile sites, and too little for tablet sites. Can you help me? Thanks in advance, Diego.Hi G
I'm currently tasked with reformatting 3,000 product images (thumbnail, normal, and large sizes for each) for a new online store. To do that, I'm trying to create a Photoshop (CS6) action that can automate the process as much as possible because I ha
Hey guys! Just a quick question. I'm taking an intro Java course this semester and for one of our labs, we have to create a voting system that counts mouse clicks as votes. We're instructed to have at least 12 different candidates, but 12 boxes won't
Where does connector class loader fit in the OC4J class loader tree. Is it a parent to the EJB Classloader (as in SunOne) or does it use its own classloader to load classes (similar to weblogic). Becos it can impact visibility into resource adapter's
Hi All, I am developing my first form. It is a page that requires several pages. I have created a tab canvas and put the pages on (there are 10 pages). When i run the form, i can see the first 4 page headers, and cannot see the other headers. Is ther
Hi all, I am having a form containing a tabbed canvas consisting of 7 tab pages.There are approximately 8 blocks containing nearly 300 items in total(mainly display items and buttons and no image items) and nearly 10 form level program units and 2 li
Hi. Fairly new to Flash. I'm putting together my first full Flash site. The tutorial I'm following along with for some assistance is written for Flash8, but I'm using CS3. To try to minimize differences, I'm doing my files in Actionscript 2. I've use
I created a bare bones iPhone/iPad app with a UIWebView (Scales Page to Fit = YES, shouldAutorotateToInterfaceOrientation = YES) and loaded a webpage, e.g. http://stackoverflow.com/ - (void)viewDidLoad { [super viewDidLoad]; NSURLRequest *request = [
So I was using a UILoader to load my swf, but was having issues with the component, so I got rid of it, and am now using the following AS to load the file instead: var Xpos:Number=0; var Ypos:Number=0; var swf:MovieClip; var loader:Loader=new Loader(
I am trying to create an action that takes the image in question and fits it with a specific pixel dimension. Some of the images are more horizontal in orientaion (like a school bus) and some are more on the vertical end (Like a tall building) They a
I'm calling centerView method on handleLoadComplete event (ImageViewer.as) so the image will be adjusted to fit the content area when load is complete, but I'm having problems when testing this on Internet Explorer. Using Firefox it works fine, but o
Hi, I have added 3 columns in a stacked canvas which is in a tabbed canvas. but the new items i have added are overlapping on the existing columns. Not able to adjust the alignment properly. Please help me resolve this issue. Thanks & regards, Pavan
Hi, I'm working on a pal anamorphic project in which I'd like to import a photoshop file, with a gap inside to use video in. I made the file using the photoshop cs3 video preferences. Saved it as a psd 1024x576 not anamorphic. The sequence has the sa
Hi I have a webpage that I want to display on an iPad without the user having to zoom out when they first load it. It's a 1920x1080 page -- that is, it has a jpeg background image 1920x1080 and it has text elements overlayed in various places on it.
Hello, We are building FLex and Flash apps running on Flash 10.1.  In our situation, the load time for a 1080x768 swf is slower when building in Flex than in flash.  We have tried useing the preinitialize event to set at least a loading message, but
Hi Guys, I've went thru the thread and noticed some had already asked the following question, yet I didn't see any satisfying answer. I'm using mxml components. Some use an external configuration when being initialize (it's external so I can change p