some fixes
[rmh3093.git] / motsim / Trial.java
blobac46a5f1809b98dfe8b66ebeda7cc47d7c787a38
1 /**
2 * Trial.java
3 * motsim
4 */
6 /*
7 Copyright (c) 2008, Ryan M. Hope
8 All rights reserved.
10 Redistribution and use in source and binary forms, with or without modification,
11 are permitted provided that the following conditions are met:
13 * Redistributions of source code must retain the above copyright notice,
14 this list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright notice,
16 this list of conditions and the following disclaimer in the documentation
17 and/or other materials provided with the distribution.
18 * Neither the name of the project nor the names of its contributors may be
19 used to endorse or promote products derived from this software without
20 specific prior written permission.
22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
23 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
26 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
29 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 import java.awt.Color;
35 import java.awt.Dimension;
36 import java.awt.Graphics;
37 import java.awt.Graphics2D;
38 import java.awt.Image;
39 import java.awt.font.FontRenderContext;
40 import java.awt.font.TextLayout;
42 import javax.swing.JFrame;
44 @SuppressWarnings("serial")
45 class Trial extends JFrame {
47 Graphics2D bufferGraphics;
48 FontRenderContext frc;
49 Image offscreen;
50 int mh;
51 Model model;
52 Dimension screen_size;
53 EventHandler evh;
55 public Trial(Model model, Dimension screen_size, EventHandler evh) {
56 this.evh = evh;
57 addWindowListener(evh);
58 addMouseListener(evh);
59 addMouseMotionListener(evh);
60 addKeyListener(evh);
61 this.model = model;
62 this.screen_size = screen_size;
63 setPreferredSize(screen_size);
64 setResizable(false);
65 setLocationRelativeTo(null);
66 setUndecorated(true);
67 setAlwaysOnTop(true);
68 model.gd.setFullScreenWindow(this);
69 setBackground(Color.black);
70 offscreen = createImage(screen_size.width, screen_size.height);
71 bufferGraphics = (Graphics2D)offscreen.getGraphics();
72 frc = bufferGraphics.getFontRenderContext();
73 setupTrial();
76 public void setupTrial() {
77 model.startTime = 0;
78 model.stopTime = 0;
79 model.duration = 0;
82 private void drawTarget(Target t, boolean isMasked) {
83 bufferGraphics.setColor(model.target_color);
84 TextLayout tl;
85 String text;
86 if (isMasked)
87 text = "XXXXXX";
88 else
89 text = t.callsign;
90 tl = new TextLayout(text, model.target_font, frc);
91 t.bounds = tl.getBounds();
92 tl.draw(bufferGraphics, (float)t.x, (float)t.y);
95 private void processTargets() {
97 Target t;
98 boolean isMasked = false;
100 for (int i=0; i<model.targets.size(); i++) {
102 t = model.targets.get(i);
104 if (model.stage == Model.TrialStage.FIND_TARGET) {
105 if ((model.mouse_x>=t.x) &&
106 (model.mouse_x<=(t.x+t.bounds.getWidth())) &&
107 (model.mouse_y>=(t.y-t.bounds.getHeight())) &&
108 (model.mouse_y<=t.y) )
109 isMasked = false;
110 else
111 isMasked = true;
114 if ((model.mouse_x_clk>=t.x) &&
115 (model.mouse_x_clk<=(t.x+t.bounds.getWidth())) &&
116 (model.mouse_y_clk>=(t.y-t.bounds.getHeight())) &&
117 (model.mouse_y_clk<=t.y) ) {
118 if (model.targets.get(model.target)==t) {
119 model.stage = Model.TrialStage.STOP_SEARCH;
120 //stopTrial();
124 drawTarget(t, isMasked);
130 public void drawFrame() {
131 bufferGraphics.setColor(model.target_color);
132 bufferGraphics.drawRect(0, 0, screen_size.width-1,
133 screen_size.height-1);
134 bufferGraphics.drawRect(0, screen_size.height-1-mh,
135 screen_size.width-1, screen_size.height-1);
138 public void drawQuery() {
139 bufferGraphics.setColor(model.query_color);
140 TextLayout tl = new TextLayout("Click on target: " + "\"" +
141 model.targets.get(model.target).callsign + "\"",
142 model.query_font, frc);
143 tl.draw(bufferGraphics,
144 (int)Math.round(
145 screen_size.width / 2 - tl.getBounds().getCenterX()
147 (int)Math.round(
148 screen_size.height - mh / 2 +
149 tl.getBounds().getCenterY() / 2
152 bufferGraphics.setColor(model.target_color);
155 public void drawTrialSplash() {
156 bufferGraphics.setColor(model.query_color);
157 TextLayout tl = new TextLayout("Trial " + model.trialCount,
158 model.trial_splash_font, frc);
159 tl.draw(bufferGraphics,
160 (int)Math.round(screen_size.width/2 - tl.getBounds().getWidth()/2),
161 (int)Math.round(screen_size.height/2)
163 repaint();
164 try {
165 Thread.sleep(2000);
166 } catch (InterruptedException e) {
167 e.printStackTrace();
171 public void paint(Graphics g) {
172 Graphics2D g2 = (Graphics2D)g;
173 if ((model.stage.ordinal() == Model.TrialStage.MOVE_TARGETS.ordinal()) ||
174 model.stage.ordinal() == Model.TrialStage.FIND_TARGET.ordinal()
176 clearScreen();
177 drawFrame();
178 if (model.stage == Model.TrialStage.FIND_TARGET)
179 drawQuery();
180 processTargets();
182 g2.drawImage(offscreen, 0, 0, this);
185 public void update(Graphics g) {
186 paint(g);
189 public void clearScreen() {
190 bufferGraphics.clearRect(0, 0, screen_size.width,
191 screen_size.height);
194 protected void stopTrial() {
195 setVisible(false);