Back to Demos

HelloPsychophysicist

This demo opens a Full Screen Exclusive Mode Window and displays one line of text and two images on an otherwise entirely blank (black) screen.

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

    Pure Java version is in HelloPsychophysicist.java file.

    First seven common steps
  1. Import the necessary classes/packages
  2. First place import lines in the beginning of your Java code, including the necessary classes from psychWithJava package.
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    
    import psychWithJava.FullScreen;
    Alternatively you can import all classes from a package
    import psychWithJava.*;
    After importing classes you will no more need to explicitly type the full path of these classes in your code.

    Declare a public class and create a main method The name of the public class must be the same as the first part (the part before .java) of the file name

    public class HelloPsychophysicist extends FullScreen implements Runnable {
    
      public static void main(String[] args) {
    To execute a Java file, you must have a main method in a public class, placed in a file of the same name as the class. For example definition of HelloPsychophysicist class is placed in HelloPsychophysicist.java The syntax of the main method is important, it has to be exactly as shown above. extends and implements keywords are used for inheritance. This syntax means that HelloPsychophysicist is a class which inherits from another class called FullScreen. FullScreen is the class, which provides many of the methods needed for the Psychophysics experiments. By inheriting from that class HelloPsychophysicist becomes a FullScreen class and more. That is, it has all the methods of FullScreen available. HelloPsychophysicist also inherits from an interface (a sort of class with no method definitions) called Runnable. We do this because a new Thread can be created by using a Runnable object. Threads are useful because multiple of them can run concurrently by the CPU.

  3. Create an instance of the HelloPsychophysicist class
  4. In the main method, create a HelloPsychophysicist object fs as follows
    HelloPsychophysicist fs = new HelloPsychophysicist();
    Recall that HelloPsychophysicist is FullScreen, therefore constructing a HelloPsychophysicist is nearly equivalent to constructing a FullScreen object. For more information on the constructor of FullScreen see Chapter 2: Hello Psychophysicist and Chapter 10: Managing (multiple) displays.

  5. Initialize certain aspects of your object.
  6. Here set the number of video buffers to 2. By default the number of video buffers is 1.
    fs.setNBuffers(2);

  7. Create a new Thread and start it
  8. Thread experiment = new Thread(fs);
    experiment.start();
    Threads are explained in the Guide in Chapter 3 and in Chapter 5 (coming soon!). You can construct a Thread with a Runnable object. Remember that HelloPsychophysicist is a Runnable object, as well as a FullScreen object. A class that implements Runnable interface must have a public method called run(). This method is automatically invoked when you start your Thread.

  9. Provide the run() method
  10. public void run()
    the method should be declared exactly as above.

  11. Place the experiment/animation inside run(), use try-catch-finally
  12. 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
        closeScreen();
      }
    Always invoke closeScreen() method inside finally{}. See Chapter 2: Hello Psychophysicist and Chapter 10: Managing (multiple) displays for the details of closeScreen() method.

    Above 7 steps are going to be nearly identical in all demos, 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.

    Matlab version is in HelloPsychophysicist.m file.

    Common steps
  1. Import psychWithJava and other packages
  2. Normally you would have to write the entire path to a class in your code, but instead you can import the classes you need and eliminate the long names. Place the following line near the top of your program
    import psychWithJava.*;
    Alternatively you could import only a single class from a package
    import psychWithJava.FullScreen;
    Note that psychWithJava must be in your javaclasspath. See instructions on setting up the system for Matlab.
  3. Create a Java object from within Matlab
  4. fs = FullScreen();
    place this inside HelloPsychophysicist.m file, don't try to directly execute it, your system may hang (press ESC if it does). Once a java object is created, one can use methods (functions) of that java object just as one would do in a normal pure java program. See below how to invoke methods of java objects.

  5. Place your code inside try-catch clause
  6. Matlab has an error handling mechanism, similar to Java. It only lacks the finally{} part. This causes minor inconvenience.
    try
      fs = FullScreen();
      % Your animation here
      % ....
      fs.closeScreen();
    catch
      fs.closeScreen();
      rethrow(lasterror);
    end
    
    Always put closeScreen() both at the end of try and inside the catch part. This way if your program crashes it is less likely to hang your entire system. rethrow(lasterror) prints out a report from the exception.

  7. Use the methods of FullScreen
  8. You can invoke any of the FullScreen methods, the syntax is as follows
    fs.setNBuffers(2);
    
    fs.displayText(...
        'Hello Psychophysicist (from within Matlab)');
    fs.updateScreen();
    pause(2);
    
    fs.blankScreen();
    fs.hideCursor();
    
    Note how pause() replaced Thread.sleep() of pure java version. pause() is a Matlab function, but of course you could invoke the Thread.sleep() method instead. setNBuffers(2) sets the total number of video buffers to 2 (one back, one front buffer), displayText() method displays a text at the center of the back video buffer, updateScreen() brings the back buffer front. blankScreen() clears the screen so that new images do not paint over the old screen. hideCursor() hides the cursor.

    Coming soon!