title: Java applet with getSize().width, getSize().height
body: <p>I just came across example - tutorial in book which I don't quite understand.</p>
  
  <p>So here is the code</p>
  
  <pre><code>import java.awt.Color;
  import java.awt.Font;
  import java.awt.Graphics;
  import java.awt.Graphics2D;
  import java.awt.RenderingHints;
  import java.awt.geom.Rectangle2D;
  import java.util.GregorianCalendar;
  
  import javax.swing.JApplet;
  
  public class Watch extends JApplet {
      private final Color butterscotch = new Color(255, 204, 102);
      Rectangle2D.Float background;
  
      // Whats is purpose of following line, here on this place? Applet works well even without it?
      Graphics2D screen2D;
  
      @Override
      public void init() {
          setBackground(Color.black);
      }
  
      @Override
      public void paint(Graphics screen) {
          super.paint(screen);
          Graphics2D screen2D = (Graphics2D) screen;
          Font type = new Font("Monospaced", Font.BOLD, 20);
          screen2D.setFont(type);
          screen2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
          if (background == null) {
              // set up the background rectangle
              // Whats is purpose of following line? Applet works well even without parameters?
              background = new Rectangle2D.Float(0F, 0F, getSize().width, getSize().height);
              // But if previous line is omit then background color is not set - why?
              // background = new Rectangle2D.Float(0F, 0F, 0F, 0F);
              // background = new Rectangle2D.Float();
          }
          screen2D.fill(background);
          GregorianCalendar day = new GregorianCalendar();
          String time = day.getTime().toString();
          screen2D.setColor(butterscotch);
          screen2D.drawString(time, 5, 25);
          try {
              Thread.sleep(1000);
          } catch (InterruptedException e) {
              // do nothing
          }
  
          repaint();
      }
  }
  </code></pre>
  
  <p>My questions are:</p>
  
  <ul>
  <li><p>What is purpose of this declaration (line 16) here on this place in code:</p>
  
  <p>Graphics2D screen2D;</p></li>
  </ul>
  
  <p>code works even without it? To be more precise Graphics2D object is of course declared later with same object name (screen2D) later on in paint() (line 26).</p>
  
  <ul>
  <li>What is purpose of </li>
  </ul>
  
  <p><code>getSize().width, getSize().height</code> in this line (33):</p>
  
  <pre><code>background = new Rectangle2D.Float(0F, 0F, getSize().width, getSize().height);
  </code></pre>
  
  <p>I mean, how can it even get size of newly created object when size is not defined (prior to) that object creation? OK, I recon that since Eclipse gives initial value of 200 x 200 this might makes sense, so that getSize().width, getSize().height gets those values? Am I right about that one?</p>
  
  <p>Moreover any of following (substitution) lines of code works well, instead of that one:</p>
  
  <pre><code>background = new Rectangle2D.Float(0F, 0F, 0F, 0F);
  </code></pre>
  
  <p>or</p>
  
  <pre><code>background = new Rectangle2D.Float();
  </code></pre>
  
  <p>BUT, here comes another question</p>
  
  <ul>
  <li>If <code>background = new Rectangle2D.Float(0F, 0F, getSize().width, getSize().height);</code>
  is substituted with </li>
  </ul>
  
  <p>background = new Rectangle2D.Float(0F, 0F, 0F, 0F);</p>
  
  <p>or </p>
  
  <pre><code> background = new Rectangle2D.Float();
  </code></pre>
  
  <p>Then background color is NOT black as defined in init() line 20</p>
  
  <pre><code>setBackground(Color.black);
  </code></pre>
  
  <p>but it is some shade of gray. Why? What do I miss here?</p>
  
  <p>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 )</p>