File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Applets and the fly likes Applet with getSize().width, getSize().height Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "Applet with getSize().width, getSize().height" Watch "Applet with getSize().width, getSize().height" New topic
Author

Applet with getSize().width, getSize().height

Nenad Bulatovic
Greenhorn

Joined: Sep 28, 2013
Posts: 21
I just came across example - tutorial in book (Sams Teach Yourself Java 2 in 21 Days) which I don't quite understand.

So here is the code (from bonus chapters from CD)



My questions are:

1. What is purpose of this declaration (line 16):

code works even without it?
To be more precise Graphics2D object is declared later with same object name (screen2D) later on in paint() (line 26).

2. What is purpose of in this line (33):

I mean, how can it even get size of newly created object when size is not defined (prior to) that object creation?

Moreover any of following (substitution) lines of code works well, instead of that one:



or



BUT, here comes question

3. If
is substituted with

or


Then background color is NOT black as defined in init() line 20

but it is some shade of gray. Why? What do I miss here?

One more note: I am using Eclipse IDE Kepler if it matters in this case anyhow (I know that default applet size is 200 x 200, which could be modified in Run configuration -> parameters )
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42906
    
  69
1. What is purpose of this declaration (line 16): Graphics2D screen2D; code works even without it?

The author left it in inadvertently. The Graphics object passed into the paint method should not be used (or stored) outside of it, so there's no point in having it.


2. What is purpose of getSize().width, getSize().height in this line (33):
I mean, how can it even get size of newly created object when size is not defined (prior to) that object creation?

The size is defined, it should be the size of the JApplet. If you read the javadocs of the fill method, it should make a big difference whether the Shape covers 0x0 pixels, or the entire applet, don't you think?
Nenad Bulatovic
Greenhorn

Joined: Sep 28, 2013
Posts: 21
Yes, you are right. Meanwhile I figured out that Eclipse predefined (by default) JApplet size to 200 x 200, so there is where gets size. I forgot about it so I thought it would be zero anyway. So, I recon if fill is set on 0x0 as in then it does nothing but if set on 200x200 which gets from then it does what it is supposed to do.

Thank you for heads up!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Applet with getSize().width, getSize().height