0

This short program is supposed to show a green full-screen background and a red square of 400 x 400 px in the center. However, the red square is at the top-lef corner [0, 0, 400, 400] instead of centered.

try
    screens = Screen('Screens'); % check for number of screens (currently '0' because only one screen is connected)
    whichscreen = max(screens);
    [myscreen,rect]=Screen('OpenWindow', whichscreen, [0, 255,0]);
    screen_rect = [0, 0, 1366, 768];
    square_size = [0, 0, 400, 400];
    square_rect = CenterRect(square_size, screen_rect);
    Screen('FillRect', myscreen, [255, 0, 0], square_size);
    Screen('Flip', myscreen);
    KbWait;
    Screen('CloseAll');
catch
    Screen('CloseAll');
end

Also, the program doesn't close down on pressing a button on the keyboard.

Any idea what the issue is?

Thanks & best, J

wuschLOR
  • 155
  • 1
  • 6
JoHKa
  • 138
  • 9

1 Answers1

1

You are using the wrong variable. You are drawing a rect to square_size and not to square_rect as you intend - therefore its obvious that the rect is drawn to [0, 0, 400, 400].

try
    screens = Screen('Screens'); % check for number of screens (currently '0' because only one screen is connected)
    whichscreen = max(screens); % 
    [myscreen,rect]=Screen('OpenWindow', whichscreen, [0, 255,0]);
    screen_rect = [0, 0, 1366, 768];
    square_size = [0, 0, 400, 400];
    square_rect = CenterRect(square_size, screen_rect);
    Screen('FillRect', myscreen, [255, 0, 0], square_rect);
    Screen('Flip', myscreen);
    KbWait;
    Screen('CloseAll');
catch
    Screen('CloseAll');
end

Additionally you can make it shorter plus compatible to every monitor your using by using the rect variable provided by the Screen('OpenWindow').

try
    screens = Screen('Screens');
    whichscreen = max(screens);
    [myscreen,rect]=Screen('OpenWindow', whichscreen, [0, 255,0]);
    square_size = [0, 0, 400, 400];
    square_position = CenterRect(square_size, rect);
    Screen('FillRect', myscreen, [255, 0, 0], square_position);
    Screen('Flip', myscreen);
    KbWait;
    Screen('CloseAll');
catch
    Screen('CloseAll');
end
wuschLOR
  • 155
  • 1
  • 6