annotate ExperimentGui.java @ 23:9fc8683b8fed

Fixed GUI button presses, had their background change as feedback and added ability to record responses for a probe tone at the end of a sequence
author JShulver
date Wed, 07 Nov 2012 18:22:01 +0000
parents 3dd7636ca811
children c5db34797ff3
rev   line source
m@0 1 /*=============================================================================
m@0 2 * File: ExperimentGui.java
m@0 3 * Author: Marcus Pearce <m.pearce@gold.ac.uk>
m@0 4 * Created: <2007-02-14 16:42:31 marcusp>
m@10 5 * Time-stamp: <2011-11-04 17:41:45 marcusp>
m@0 6 *=============================================================================
m@0 7 */
m@0 8
m@0 9 import java.awt.*;
m@0 10 import javax.swing.*;
m@0 11 import java.util.ArrayList;
m@0 12 import java.util.Iterator;
m@0 13
m@0 14 public class ExperimentGui extends JFrame implements Runnable {
m@0 15
m@0 16 /* the Experiment */
m@0 17 private Experiment exp;
m@0 18
m@0 19 /* The visual display indicating probe positions */
m@0 20 private Clock clock;
m@0 21
m@0 22 /* The UI components */
m@0 23 private JPanel mainPanel;
m@0 24 private InstructionsPanel instructionsPanel;
m@0 25 private StimulusPanel stimulusPanel;
m@0 26 private SubjectDataPanel subjectDataPanel;
m@0 27 private InterBlockPanel interBlockPanel;
m@0 28
m@0 29 /* Whether we are accepting responses */
m@0 30 private Boolean acceptingResponses;
m@0 31
m@0 32 /* accessors */
m@0 33 public Boolean getAcceptingResponses() { return acceptingResponses; }
m@10 34 public void setAcceptingResponses(Boolean b) {
m@10 35 if (exp.getDebug())
JShulver@23 36 System.out.println("\n\nChanging acceptingResponses from " + acceptingResponses + " to " + b);
m@10 37 acceptingResponses = b;
m@10 38 }
m@0 39 public Experiment getExperiment() { return exp; }
m@0 40 public InstructionsPanel getInstructionsPanel() { return instructionsPanel; }
m@0 41 public StimulusPanel getStimulusPanel() { return stimulusPanel; }
m@0 42 public SubjectDataPanel getSubjectDataPanel() { return subjectDataPanel; }
m@0 43 public InterBlockPanel getInterBlockPanel() { return interBlockPanel; }
m@0 44
m@0 45 /* Constructor */
m@0 46 public ExperimentGui(Experiment experiment) {
m@0 47
m@0 48 // initialise experiment
m@0 49 exp = experiment;
m@0 50 acceptingResponses = false;
m@0 51
m@0 52 // set up the clock
m@0 53 clock = new Clock();
m@0 54
m@0 55 // construct the frame
m@0 56 JFrame.setDefaultLookAndFeelDecorated(true);
m@0 57 this.getContentPane().setBackground (Color.black);
m@0 58 this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
m@0 59 this.setLayout (new BorderLayout());
m@0 60
m@0 61 // The different cards
m@0 62 instructionsPanel = new InstructionsPanel(this);
m@0 63 stimulusPanel = new StimulusPanel(this, clock);
m@0 64 interBlockPanel = new InterBlockPanel(exp);
m@0 65 subjectDataPanel = new SubjectDataPanel(this, exp.getSubjectResults());
m@0 66
m@0 67 // The Controller
m@0 68 ExperimentController ec = new ExperimentController(this);
m@0 69
m@0 70 // Show it all
m@0 71 CardLayout cl = new CardLayout();
m@0 72 mainPanel = new JPanel(cl);
m@0 73 mainPanel.add(instructionsPanel, "instructions");
m@0 74 mainPanel.add(interBlockPanel, "interblock");
m@0 75 mainPanel.add(stimulusPanel, "stimulus");
m@0 76 mainPanel.add(subjectDataPanel, "subject");
m@0 77
m@0 78 this.add(mainPanel, BorderLayout.CENTER);
m@0 79 }
m@0 80
m@0 81 /*
m@0 82 * Methods for changing displayed card
m@0 83 */
m@0 84
m@0 85 public void showCard(String card) {
m@0 86 CardLayout cl = (CardLayout)(mainPanel.getLayout());
m@0 87 cl.show(mainPanel, card);
m@0 88 }
m@0 89
m@0 90 public void nextCard() {
m@0 91 CardLayout cl = (CardLayout)(mainPanel.getLayout());
m@0 92 cl.next(mainPanel);
m@0 93 }
m@0 94
m@0 95 /* Advance clock by 1 minute and redisplay. */
m@0 96 public void tick(int n) {
m@0 97 clock.tick(n);
m@0 98 clock.repaint();
m@0 99 }
m@0 100
m@0 101 /* Show the Clock */
m@0 102 public void showClock() {
m@3 103 if (exp.showClock()) {
m@3 104 clock.showClock = true;
m@3 105 clock.showFullClock = false;
m@3 106 clock.repaint();
m@3 107 }
m@0 108 }
m@0 109
m@0 110 /* Show the Fixation Point */
m@0 111 public void showFixationPoint() {
JShulver@23 112 System.out.println("showFixationPoint");
m@0 113 clock.showClock = false;
m@0 114 clock.showFullClock = false;
m@0 115 clock.repaint();
m@0 116 }
m@0 117
m@0 118 /* Run clock for r revolutions at a rate of 1 minute (6 degrees)
m@0 119 * every n milliseconds.
m@0 120 */
m@0 121 public void runClock(int r, long n) {
m@0 122 clock.reset();
m@0 123 showClock();
m@0 124 for (int i = 0; i < (60 * r); i++) {
m@0 125 try { Thread.sleep (n); } catch (InterruptedException e) {}
m@0 126 clock.tick(1);
m@0 127 clock.repaint();
m@0 128 }
m@0 129 try { Thread.sleep (1000); } catch (InterruptedException e) {}
m@0 130 showFixationPoint();
m@0 131 }
m@0 132
JShulver@23 133
m@0 134 /* Run method for this thread */
m@0 135 public void run() {
JShulver@23 136 System.out.println("Run!");
m@0 137 //showFixationPoint();
m@0 138 clock.reset();
m@0 139 showClock();
m@0 140
m@0 141 int clockUnits = exp.getClockUnits();
m@0 142 int numUnits = exp.getNumUnits();
m@0 143 long tatum = exp.getCurrentBlock().getTatum();
m@0 144 long tatumInMilliseconds = tatum / 1000;
m@0 145 int nMinutes = 60 / numUnits;
m@0 146
m@0 147 ArrayList onsets = exp.getCurrentBlock().getOnsets();
m@0 148 ArrayList probes = exp.getCurrentBlock().getProbePositions();
m@0 149 ArrayList clockStartTimes = exp.getCurrentBlock().getClockStartTimes();
m@0 150
m@0 151 Iterator oi = onsets.iterator();
m@0 152 Iterator pi = probes.iterator();
m@0 153 Iterator ci = clockStartTimes.iterator();
m@0 154
m@0 155 ProbeID probe = ProbeID.NOT_PROBE;
m@0 156
m@0 157 long currentOnset = 0;
m@0 158 long nextEventOnset = ((Long)(oi.next())).longValue();
m@0 159 long nextClockStartTime = ((Long)(ci.next())).longValue();
m@0 160
m@0 161 int clockUnit = 0;
m@0 162 boolean clockTicking = false;
m@0 163
JShulver@23 164 do { //using a do-while construct allows the user to enter a value at the end
JShulver@23 165 //this should not really be in the 'view' anyway...
JShulver@23 166 System.out.println("TATUM: " +tatum);
JShulver@23 167 tatumInMilliseconds = tatum/1000;
m@10 168 if (exp.getDebug())
m@10 169 System.out.println("Ticking = " + clockTicking +
m@10 170 "; clockUnit = " + clockUnit +
m@10 171 "; currentOnset = " + currentOnset +
m@10 172 "; nextEventOnset = " + nextEventOnset +
m@10 173 "; nextClockStartTime = " + nextClockStartTime +
m@10 174 "; probe = " + probe);
m@0 175 if (clockTicking == true && clockUnit == 0)
m@0 176 tick(nMinutes);
m@0 177
JShulver@23 178 if (currentOnset >= nextClockStartTime) {
m@0 179 //new Thread(clock).start();
m@0 180 clock.reset();
m@0 181 showClock();
m@0 182 clockTicking = true;
m@0 183 if (ci.hasNext())
m@0 184 nextClockStartTime = ((Long)(ci.next())).longValue();
JShulver@23 185 else
JShulver@23 186 nextClockStartTime = Long.MAX_VALUE;
m@0 187 }
JShulver@23 188 if (currentOnset >= nextEventOnset) {
JShulver@23 189 probe = (ProbeID)pi.next();
m@0 190 // Manipulate display depending on probe identifier
m@0 191 switch (probe) {
m@0 192 case NOT_PROBE:
m@0 193 // if (clock.showClock == true)
m@0 194 // tick(nMinutes);
m@0 195 break;
m@0 196 case START_CLOCK:
m@0 197 //clock.reset();
m@0 198 //showClock();
m@0 199 //tick(nMinutes);
m@0 200 break;
m@0 201 case BEFORE_PROBE:
m@10 202 if (exp.getDebug())
m@10 203 System.out.println("BEFORE_PROBE: acceptingResponses = " +
m@10 204 acceptingResponses);
m@0 205 if (acceptingResponses == true)
m@0 206 exp.getCurrentBlock().addResponse(0, System.nanoTime());
m@0 207 else
m@10 208 setAcceptingResponses(true);
m@0 209 //tick(nMinutes);
m@0 210 clockTicking = false;
m@0 211 break;
m@0 212 case PROBE:
m@0 213 case PROBE_EX:
m@0 214 case PROBE_UNEX:
m@10 215 if (exp.getDebug())
m@10 216 System.out.println("PROBE_{UN,}EX: acceptingResponses = "
m@10 217 + acceptingResponses);
m@0 218 clock.showFullClock = false;
m@0 219 clock.repaint();
m@0 220 break;
m@0 221 case AFTER_PROBE:
m@0 222 //clock.showFullClock = false;
m@0 223 //clock.repaint();
m@0 224 //showFixationPoint();
m@0 225 break;
m@0 226 default:
m@0 227 System.out.println("Unexpected probe id: " + probe);
m@0 228 break;
m@0 229 }
m@0 230 // Update probe identifier and onset
JShulver@23 231
m@10 232 if (exp.getDebug())
m@10 233 System.out.println("Next probe = " + probe);
m@0 234 nextEventOnset =((Long)(oi.next())).longValue();
JShulver@23 235 }
m@0 236 // sleep for a tatum
m@0 237 try { Thread.sleep(tatumInMilliseconds); }
m@0 238 catch (InterruptedException e) {}
m@0 239 currentOnset += tatum;
m@0 240 clockUnit = (clockUnit + 1) % clockUnits;
JShulver@23 241 } while(oi.hasNext());
m@0 242 showFixationPoint();
m@0 243 }
m@0 244 }