0

这个短程序应该显示一个绿色的全屏背景和一个 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

4

1 回答 1

1

您使用了错误的变量。您正在绘制一个 rect,square_size而不是square_rect您想要的 - 因此很明显 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

此外,您可以rect使用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
于 2015-07-13T09:00:58.587 回答