简体   繁体   中英

Java applet with getSize().width, getSize().height

I just came across example - tutorial in book which I don't quite understand.

So here is the 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();
    }
}

My questions are:

  • What is purpose of this declaration (line 16) here on this place in code:

    Graphics2D screen2D;

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).

  • What is purpose of

getSize().width, getSize().height in this line (33):

background = new Rectangle2D.Float(0F, 0F, getSize().width, getSize().height);

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?

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

background = new Rectangle2D.Float(0F, 0F, 0F, 0F);

or

background = new Rectangle2D.Float();

BUT, here comes another question

  • If background = new Rectangle2D.Float(0F, 0F, getSize().width, getSize().height); is substituted with

background = new Rectangle2D.Float(0F, 0F, 0F, 0F);

or

 background = new Rectangle2D.Float();

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

setBackground(Color.black);

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 )

In this example there are two objects called screen2D. The first which is declared on line 16 is a member variable that is package private (can be accessed by all classes in the given package). As you have pointed out this variable doesn't seem to be used at all.

The second screen2D is a local variable declared on line 26 and used only in the paint function.

Likely this is a typo and only one of these variables is needed for the example.

the function getSize() is inherited from the superclass JApplet so getSize.width() is getting the applets width not the width of a newly created object.

So when you change the background to have dimensions of 0 then set its color you are setting the color of an object that doesn't have any size. Since this object doesn't have any size it doesn't matter what color you set it to since you wont actually be seeing it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM