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