why is height and width when declared as a constan

2019-08-06 02:55发布

When I use the height and width as a constant I was wondering why it's giving me the value 100? Is it because size(); was not declared above? How do I set it to the size of the canvas? Because the following prints 100

Any help would do!!

Here is a copy of my code:

final int SIZE = height;

void setup() {
 size (1000,1000);
 println(SIZE); 

}

3条回答
男人必须洒脱
2楼-- · 2019-08-06 03:21

See the processing documentation for size():

... If size() is not used, the window will be given a default size of 100 x 100 pixels.

and height:

... The value of height defaults to 100 if size() is not used in a program.

This means as long size() was not called, the values for height and width are initialized to 100.

If you read the variables before, as you do it, then they will return 100.

You have to initialize SIZE after calling size():

void setup() {
    size (1000,1000);
    SIZE = height;
}
查看更多
走好不送
3楼-- · 2019-08-06 03:26

The width and height variables are set when you call the size() function. You're using them outside of a function, which means they're happening before the size() function is called, which is why they still have their default values.

To fix this, you need to move the variable initialization to be after size() is called:

int SIZE;

void setup() {
 size (1000,1000);
 SIZE = height
 println(SIZE); 
}
查看更多
再贱就再见
4楼-- · 2019-08-06 03:48

For primitive types the value itself is stored in a variable. So when you call final int SIZE = height; SIZE will have the value which height had immediately prior to that assignment. All subsequent changes to height will not affect SIZE.

查看更多
登录 后发表回答