这个短程序应该显示一个绿色的全屏背景和中间有一个400 x 400像素的红色正方形。然而,红色方块位于左上角[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_size);
Screen('Flip', myscreen);
KbWait;
Screen('CloseAll');
catch
Screen('CloseAll');
end
而且,程序不会在按键盘上的按钮时关闭。
知道是什么问题吗?
谢谢,最好的J
您使用了错误的变量。你画的是一个指向square_size
的矩形,而不是你想要的指向square_rect
的矩形——因此很明显,这个矩形是指向[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
此外,您可以使用Screen('OpenWindow')
提供的rect
变量使其更短,并与您使用的每个监视器兼容。
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