Mercurial > hg > mep
diff ExperimentGui.java @ 0:4031cbb02f08
Initial import.
Ignore-this: 87317e384f22bde48db996355191fa5f
author | Marcus Pearce <m.pearce@gold.ac.uk> |
---|---|
date | Tue, 18 May 2010 11:37:10 +0100 |
parents | |
children | 6108a8aa9d82 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ExperimentGui.java Tue May 18 11:37:10 2010 +0100 @@ -0,0 +1,222 @@ +/*============================================================================= + * File: ExperimentGui.java + * Author: Marcus Pearce <m.pearce@gold.ac.uk> + * Created: <2007-02-14 16:42:31 marcusp> + * Time-stamp: <2010-05-18 11:12:05 marcusp> + *============================================================================= + */ + +import java.awt.*; +import javax.swing.*; +import java.util.ArrayList; +import java.util.Iterator; + +public class ExperimentGui extends JFrame implements Runnable { + + /* the Experiment */ + private Experiment exp; + + /* The visual display indicating probe positions */ + private Clock clock; + + /* The UI components */ + private JPanel mainPanel; + private InstructionsPanel instructionsPanel; + private StimulusPanel stimulusPanel; + private SubjectDataPanel subjectDataPanel; + private InterBlockPanel interBlockPanel; + + /* Whether we are accepting responses */ + private Boolean acceptingResponses; + + /* accessors */ + public Boolean getAcceptingResponses() { return acceptingResponses; } + public void setAcceptingResponses(Boolean b) { acceptingResponses = b; } + public Experiment getExperiment() { return exp; } + public InstructionsPanel getInstructionsPanel() { return instructionsPanel; } + public StimulusPanel getStimulusPanel() { return stimulusPanel; } + public SubjectDataPanel getSubjectDataPanel() { return subjectDataPanel; } + public InterBlockPanel getInterBlockPanel() { return interBlockPanel; } + + /* Constructor */ + public ExperimentGui(Experiment experiment) { + + // initialise experiment + exp = experiment; + acceptingResponses = false; + + // set up the clock + clock = new Clock(); + + // construct the frame + JFrame.setDefaultLookAndFeelDecorated(true); + this.getContentPane().setBackground (Color.black); + this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); + this.setLayout (new BorderLayout()); + + // The different cards + instructionsPanel = new InstructionsPanel(this); + stimulusPanel = new StimulusPanel(this, clock); + interBlockPanel = new InterBlockPanel(exp); + subjectDataPanel = new SubjectDataPanel(this, exp.getSubjectResults()); + + // The Controller + ExperimentController ec = new ExperimentController(this); + + // Show it all + CardLayout cl = new CardLayout(); + mainPanel = new JPanel(cl); + mainPanel.add(instructionsPanel, "instructions"); + mainPanel.add(interBlockPanel, "interblock"); + mainPanel.add(stimulusPanel, "stimulus"); + mainPanel.add(subjectDataPanel, "subject"); + + this.add(mainPanel, BorderLayout.CENTER); + } + + /* + * Methods for changing displayed card + */ + + public void showCard(String card) { + CardLayout cl = (CardLayout)(mainPanel.getLayout()); + cl.show(mainPanel, card); + } + + public void nextCard() { + CardLayout cl = (CardLayout)(mainPanel.getLayout()); + cl.next(mainPanel); + } + + /* Advance clock by 1 minute and redisplay. */ + public void tick(int n) { + clock.tick(n); + clock.repaint(); + } + + /* Show the Clock */ + public void showClock() { + clock.showClock = true; + clock.showFullClock = false; + clock.repaint(); + } + + /* Show the Fixation Point */ + public void showFixationPoint() { + clock.showClock = false; + clock.showFullClock = false; + clock.repaint(); + } + + /* Run clock for r revolutions at a rate of 1 minute (6 degrees) + * every n milliseconds. + */ + public void runClock(int r, long n) { + clock.reset(); + showClock(); + for (int i = 0; i < (60 * r); i++) { + try { Thread.sleep (n); } catch (InterruptedException e) {} + clock.tick(1); + clock.repaint(); + } + try { Thread.sleep (1000); } catch (InterruptedException e) {} + showFixationPoint(); + } + + /* Run method for this thread */ + public void run() { + + //showFixationPoint(); + clock.reset(); + showClock(); + + int clockUnits = exp.getClockUnits(); + int numUnits = exp.getNumUnits(); + long tatum = exp.getCurrentBlock().getTatum(); + long tatumInMilliseconds = tatum / 1000; + int nMinutes = 60 / numUnits; + + ArrayList onsets = exp.getCurrentBlock().getOnsets(); + ArrayList probes = exp.getCurrentBlock().getProbePositions(); + ArrayList clockStartTimes = exp.getCurrentBlock().getClockStartTimes(); + + Iterator oi = onsets.iterator(); + Iterator pi = probes.iterator(); + Iterator ci = clockStartTimes.iterator(); + + ProbeID probe = ProbeID.NOT_PROBE; + + long currentOnset = 0; + long nextEventOnset = ((Long)(oi.next())).longValue(); + long nextClockStartTime = ((Long)(ci.next())).longValue(); + + int clockUnit = 0; + boolean clockTicking = false; + + while(oi.hasNext()) { + //System.out.println("Ticking = " + clockTicking + + // "; clockUnit = " + clockUnit); + if (clockTicking == true && clockUnit == 0) + tick(nMinutes); + + if (currentOnset == nextClockStartTime) { + //new Thread(clock).start(); + clock.reset(); + showClock(); + clockTicking = true; + if (ci.hasNext()) + nextClockStartTime = ((Long)(ci.next())).longValue(); + } + + if (currentOnset == nextEventOnset) { + // Manipulate display depending on probe identifier + switch (probe) { + case NOT_PROBE: + // if (clock.showClock == true) + // tick(nMinutes); + break; + case START_CLOCK: + //clock.reset(); + //showClock(); + //tick(nMinutes); + break; + case BEFORE_PROBE: + //System.out.println("BEFORE_PROBE: acceptingResponses = " + + // acceptingResponses); + if (acceptingResponses == true) + exp.getCurrentBlock().addResponse(0, System.nanoTime()); + else + acceptingResponses = true; + //tick(nMinutes); + clockTicking = false; + break; + case PROBE: + case PROBE_EX: + case PROBE_UNEX: + //System.out.println("PROBE_{UN,}EX: acceptingResponses = " + // + acceptingResponses); + clock.showFullClock = false; + clock.repaint(); + break; + case AFTER_PROBE: + //clock.showFullClock = false; + //clock.repaint(); + //showFixationPoint(); + break; + default: + System.out.println("Unexpected probe id: " + probe); + break; + } + // Update probe identifier and onset + probe = (ProbeID)pi.next(); + nextEventOnset =((Long)(oi.next())).longValue(); + } + // sleep for a tatum + try { Thread.sleep(tatumInMilliseconds); } + catch (InterruptedException e) {} + currentOnset += tatum; + clockUnit = (clockUnit + 1) % clockUnits; + } + showFixationPoint(); + } +}