Width and height return 0

The following code, will always return x and y as 0… if I change displayWidth to width, it still returns 0. I’m using the apde editor on an s8+. How can I find the center of the display?
(The +50 is there so I can see it when it returns 0)

void setup()  {
  fullScreen(P2D);
  background(0);
}

int x = displayWidth/2;
int y = displayHeight/2;

void draw() {
     fill(100);
     ellipse(x+50, y+50, 30, 30);
}
1 Like

From what I know, displayWidth and displayHeight cannot be set to equal another variable. I’m sure someone else can give a better explanation as to WHY that is :smiley: What you would have to do is:

Which is specifically for your fullScreen code:

ellipse( displayWidth/2, displayHeight/2, 30, 30);

Which would then would put your ellipse at the center.

Another way you could do it for ANY sized screen, full screen or not, is by simply doing:

ellipse(width/2,height/2,30,30);

Ellipses are drawn around the center as you know, but you did say why you had the +50.

I think that the reason why you can’t set other variables equal to your width and height variables have to do with the fact that they are system variables. :thinking:

Anyways, I hope that helps you. By the way, please make sure that you format your code :smiley: It makes it look much nicer, and also is easier for us to read it!

You do that by highlighting your code and clicking the “Format” button, or by just highlighting it and pressing Control+Shift+C, Like this:

System.out.println("I am formatted code!");

EnhancedLoop7

You are assigning this value in the global scope of your sketch. You should initialize the values of these variable in setup() as that is the main purpose of this function. If you assign these values in the global scope, it is not guaranteed that Processing has initialized displayWidth/displayHeight properly. Also, you should use width and height instead.

Kf

6 Likes

That worked like a charm… and wow! I never would have guessed that. Thank you for the help.