Applet size in Google Chrome issue:

Before I begin, I’m using Ubuntu 10.10. This problem doesn’t exist in Firefox, just in Chrome. To check the width and height of the applet I’ve used these two methods:


screenWidth = (int)getSize().getWidth();
screenHeight = (int)getSize().geHeight();
or
screenWidth = ((Dimension)getSize()).width;
screenHeight = ((Dimension)getSize()).height;

Even though the actual measurement of the applet is correct (set by the html code), screenWidth and screenHeight are always equal to 1000.
Anyone else have this problem, or know of a work around?

JGO is just becoming my Rubber Duck Debugger ;D I figured out after submitting my question that maybe the init() was being ran before Chrome actually applied the size of the applet. So I just added a componentListener to catch a resize event, and applied the values there too.

Edit: I don’t know why I didn’t do this anyways… actually the importance was never stressed to me… but now it makes allowing the user to resize the applet (Ctrl + scrollwheel) even cooler.

Why do you cast at all in your code?

Eclipse says that .getWidth and .getHeight return Doubles, screenWidth and screenHeight are ints, so I thought I had to. As for the (Dimension), yea, I don’t know. I’m still new to Java.

getWidth(), getHeight(), getSize().getWidth(), and getSize().getHeight() all return ints.

If I do this:

int a = getSize().getHeight();

I get “Type mismatch: cannot convert from double to int”

… just sayin’.

Oh! Dimension has public int fields “width” and “height” but its getWidth() and getHeight() methods return doubles…strangest API I have ever seen.

java.awt.Rectangle is the same way - int fields but the getters for them return doubles. Comes from the abstract classes they extend. I think the design was to allow different subclasses to have different precision for the actual members (int, float, double, …).

It kind of does make it annoying to use them. Casting an int to a double only to cast it back down again isn’t going to net you much performance, so the only thing you can save is memory usage. The whole thing is complicated to solve in the end. >_<