简体   繁体   English

带有getSize()。width,getSize()。height的Java Applet

[英]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: 此代码在此位置的声明(第16行)的目的是什么:

    Graphics2D screen2D; 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). 更精确地说,稍后将在paint()中使用相同的对象名称(screen2D)声明Graphics2D对象(第26行)。

  • What is purpose of 目的是什么

getSize().width, getSize().height in this line (33): 这行(33)中的getSize().width, getSize().height

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? 好的,我确认,由于Eclipse给出的初始值为200 x 200,这可能是有道理的,因此getSize()。width,getSize()。height可以得到那些值吗? 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); 如果background = new Rectangle2D.Float(0F, 0F, getSize().width, getSize().height); is substituted with 被替换为

background = new Rectangle2D.Float(0F, 0F, 0F, 0F); 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 然后背景颜色不是init()第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 ) 另一个注意事项:在这种情况下,无论如何我都使用Eclipse IDE Kepler(我知道默认的applet大小为200 x 200,可以在Run configuration-> parameters中对其进行修改)

In this example there are two objects called screen2D. 在此示例中,有两个对象称为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). 在第16行声明的第一个变量是包私有的成员变量(可以由给定包中的所有类访问)。 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. 第二个screen2D是在第26行声明的局部变量,仅在paint函数中使用。

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. 函数getSize()是从超类JApplet继承的,因此getSize.width()获取的是applet的宽度,而不是新创建对象的宽度。

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. 因此,当您将背景的尺寸更改为0,然后设置其颜色时,便是在设置没有任何尺寸的对象的颜色。 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. 由于此对象没有任何大小,因此设置为哪种颜色都没有关系,因为您实际上不会看到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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