Back to Demos

HelloPsychophysicist in NormalWindow

This demo opens a normal window (as opposed to FSEM window of the earlier version) and displays one line of text and two images on an otherwise entirely blank (black) background.

Files Needed for the Demo
Compiling and running the Demo
Description of the demo
Jar package
See also

    Pure Java version is in HPWindow.java file.

    Most of the code is similar to the FullScreen version of HelloPsychophysicist. The differences are highlighted below.

    First seven common steps
  1. Import the necessary classes
  2. In this version NormalWindow class is imported
    import psychWithJava.NormalWindow;
    Alternatively you can import all classes from a package
    import psychWithJava.*;
    More information on import.

  3. Declare a public class and create a main method
  4. The difference from FullScreen version of HelloPsychophysicist is that HPWindow inherits from NormalWindow
    public class HPWindow extends NormalWindow implements Runnable {
    
        public static void main(String[] args) {
    This syntax means that HPWindow is a class which inherits from another class called NormalWindow. NormalWindow provides the normal window version of FullScreen class. FullScreen works in Full Screen Exclusive Mode, NormalWindow works in a normal window environment. More information on class decleration.

  5. Create an instance of the HPWindow class
  6. In the main method, create an HPWindow object
    HPWindow nw = new HPWindow();
    Recall that HPWindow is NormalWindow, therefore constructing a HPWindow is nearly equivalent to constructing a NormalWindow object. For more information on the constructor of NormalWindow see Chapter 11: Applets, normal window applications, packaging and sharing your work. More information on object creation

  7. Create a JFrame
  8. This is the most significantly different part from the FullScreen version of HelloPsychophysicist. We need a lower level container to place our NormalWindow object. The suitable container for this purpose is a JFrame. Create a JFrame, set its dimensions and other properties and place the HPWindow object (nw) inside of it.
    mainFrame = new JFrame();
    mainFrame.setBounds(XO,YO,W,H);
    mainFrame.add(nw);
    mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    // Next two lines are optional 
    mainFrame.setResizable(false);
    mainFrame.setTitle("Hello Psychophysicist");
    // you must set it visible
    mainFrame.setVisible(true);

  9. Create a new Thread and start it
  10. This step is the same as in FullScreen version of HelloPsychophysicist
    Thread experiment = new Thread(nw);
    experiment.start();
    More information on creating a thread

  11. Provide the run() method
  12. This step is the same as in FullScreen version of HelloPsychophysicist
    public void run()
    the method should be declared exactly as above.

  13. Place the experiment/animation inside run(), use try-catch-finally
  14. This step is nearly identical to FullScreen version of HelloPsychophysicist
    public void run(){
      
      try{
        \\your methods for animation 
      } catch(){
        \\catch the exceptions, that is, if something goes wrong
      } finally {
        \\in the end clean up and return the resources used
        mainFrame.dispose();
      }
    The crucial difference is that you should invoke mainFrame.dispose() method inside finally{}, instead of closeScreen() method of FullScreen class.

    Above 7 steps are going to be nearly identical in all NormalWindow implementations, except the names of files and corresponding public classes. If you use an IDE, such as Eclipse, it will assist you with most of the steps. For instance you won't need to remember which classes to import. IDEs usually determine your import needs and place them in the beginning of your file. They will also warn you that you should have a public method called run() since your class inherits from Runnable interface. They will assist for catching the Exceptions, as well. Note: Unlike in FullScreen applications, you don't have to set number of video buffers manually (See HelloPsychophysicist for more on setting number of video buffers in FullScreen). NormalWindow always uses double buffering (one back, one front buffer).

Matlab version is located at HPWindow.m file.

The Matlab code is to a great extend the same as the FullScreen version of HelloPsychophysicist. The differences are highlighted below.

  1. Import psychWithJava.NormalWindow and javax.swing.JFrame
  2. Instead of FullScreen, NormalWindow is imported.
    import javax.swing.JFrame;
    import psychWithJava.NormalWindow;
    More information on import in Matlab

  3. Create a Java object from within Matlab
  4. Instead of a FullScreen object, a NormalWindow object is created
    nw = NormalWindow();
    More information on creating Java objsects in Matlab

  5. Create a JFrame to place your NormalWindow object
  6. This is the only substantial difference from the FullScreen version of HelloPsychophysicist. We need a lower level container to place our NormalWindow object. The suitable container for this purpose is a JFrame. Create a JFrame, set its dimensions and other properties and place the HPWindow object (nw) inside of it
    mainFrame = JFrame();
    mainFrame.setBounds(XO,YO,W,H);
    mainFrame.add(nw);
    mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    % next two lines are optional
    mainFrame.setResizable(false);
    mainFrame.setTitle('Hello Psychophysicist');
    % you must set it visible
    mainFrame.setVisible(true);

  7. Place all your code in try-catch clause
  8. This is similar to FullScreen version of HelloPsychophysicist
    try
      nw = NormalWindow();
      mainFrame = JFrame()
      % place nw in mainFrame etc.
      % Your animation follows
      mainFrame.dispose;
    catch
      mainFrame.dispose();
      rethrow(lasterror);
    end
    
    The difference is that mainFrame.dispose() replaces closeScreen() method. More information on putting your animation inside try-catch clause

  9. Use the methods of NormalWindow
  10. You can now invoke the NormalWindow methods, the syntax is as follows
    nw.displayText(...
        'Hello Psychophysicist (from within Matlab)');
    nw.updateScreen();
    pause(2);
    
    nw.blankScreen();
    nw.hideCursor();
    
    More information on invoking methods of Java objects in Matlab. Note that this is nearly identical to FullScreen version of HelloPsychophysicist except the name of the object changed from fs to nw. This is possible because the method signitures are identical between FullScreen and NormalWindow. For instance displayImage(BufferedImage) method of FullScreen class displays an image at the center of the screen, whereas displayImage(BufferedImage) method of NormalWindow class displays an image at the center of the normal window. This makes converting FullScreen experiments into NormalWindow applications very simple. The only crucial difference is as explained above, creating a low level frame to place the NormalWindow object in the normal window mode.

    See Chapter 11: Applets, normal window applications, packaging and sharing your work.

    See the entire code of HPWindow.m.

    Coming soon!