% 
% chapter Demos: GaborDetectionWindow.m
% 
% opens a normal window and displays a Gabor patch on 
% either side of the screen, waits for observer response, and gives an 
% auditory feedback. It employs a 3 down 1 up adaptive procedure.
%

import psychWithJava.*;
import javax.swing.JFrame;
import java.awt.event.KeyEvent;

XO = 100;
YO = 100;
W = 612;
H = 480;

MEAN = 0.5;
CPP = 0.05;
PHASE = 0;
ORIENTATION = 45;
SIGMA = 32;

INITIAL_CONTRAST = 0.5;
DELTA_CONTRAST = 0.05;

NOISE_AMPLITUDE = 0.2;

LEFT = 0;
RIGHT = 1;
  
try
    nw = NormalWindow();
     
    %The only addition/change is here:
    mainFrame = JFrame();
    mainFrame.setBounds(XO,YO,W,H);
    mainFrame.add(nw);
    mainFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    % next two lines are optional
    mainFrame.setResizable(false);
    mainFrame.setTitle('Hello Psychophysicist');
    % you must set it visible
    mainFrame.setVisible(true);
    % up to here
    
    nw.displayText(10,50, 'Indicate the location of Gabor patch');
    nw.displayText(10,100,'left arrow for LEFT, right arrow for RIGHT');
    nw.displayText(10,150,'press q to quit');
    nw.displayText(10,300,'now press any key to continue');
    nw.updateScreen();
    
    width = nw.getWidth();
    height = nw.getHeight();
    bg =  ones(height,width) * MEAN ;
       
    % getKeyPressed(-n) (n > 0, integer) waits until observer presses a key
    % getKeyPressed(0) or getKeyPressed() returns immediately with the
    % last key the observer pressed, if nothing is pressed returns null
    % getKeyPressed(ms) (ms > 0) returns the last key pressed immediately,
    % if there is nothing in the que, it waits for ms milliseconds then 
    % returns observer's response or null if still nothing in the que.
    nw.getKeyPressed(-1);
    nw.setBackground(java.awt.Color(MEAN,MEAN,MEAN));
    nw.blankScreen();
    nw.updateScreen();
       
    contrast = INITIAL_CONTRAST;
    
    correctCount = 0;
    
    while true
        
        stimulus = bg;
        
        order = round(unifrnd(0,1));
        
        sprintf('contrast = %f ', contrast)
        
        % I'm not sure this is the best way to generate a Gabor patch 
        % in Matlab. Though it is not a factor to slow down the program.
        gbr = gabor(MEAN,CPP,contrast,PHASE,SIGMA,ORIENTATION);
        if order == LEFT
            x = round(nw.getWidth()/4 - size(gbr,2)/2);
        else
            x = round(nw.getWidth()*3/4 - size(gbr,2)/2);
        end
        y = round( nw.getHeight()/2 - size(gbr,1)/2);
        
        stimulus(y+1:y+size(gbr,1),x+1:x+size(gbr,2))=gbr(:,:);
        stimulus=stimulus+normrnd(0,1,height,width)*NOISE_AMPLITUDE/2;
         
        %!!!!!im2java2d is the bottleneck!!!!! it takes the longest time
        nw.displayImage(im2java2d(stimulus));   
        nw.updateScreen();
        
        % clear the keyPressed que
        nw.flushKeyPressed();
        
        response = nw.getKeyPressed(-1);
        if(response==KeyEvent.VK_Q)
            break;
        elseif( (response==KeyEvent.VK_LEFT && order==LEFT) || ...
            (response==KeyEvent.VK_RIGHT && order==RIGHT) )
            correctCount = correctCount + 1;
            if correctCount == 3
                contrast = max(contrast - DELTA_CONTRAST,0);
                correctCount = 0;
            end
            sound(beepSound(1000,100));
            sprintf('succes')
        else
            contrast = min(contrast + DELTA_CONTRAST,1);
            correctCount = 0;
            sound(beepSound(10000,100));
            sprintf('fail')
        end
        nw.blankScreen();
        nw.updateScreen();
        pause(0.1);
    end
    mainFrame.dispose();
catch
    mainFrame.dispose();
    rethrow(lasterror);
end