Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Nenad Bulatovic
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic