working again
[rmh3093.git] / motsim / Trial.java
blobd6988c72cb57b90a2a200842fe6e5a0accded84f
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 model.gd.setFullScreenWindow(this);
68 //model.gd.setDisplayMode(model.display_mode_preferred);
69 setBackground(Color.black);
70 offscreen = createImage(screen_size.width, screen_size.height);
71 bufferGraphics = (Graphics2D)offscreen.getGraphics();
72 frc = bufferGraphics.getFontRenderContext();
73 model.startTime = 0;
74 model.stopTime = 0;
77 private void drawTarget(Target t, boolean isMasked) {
78 bufferGraphics.setColor(model.target_color);
79 TextLayout tl;
80 String text;
81 if (isMasked)
82 text = "XXXXXX";
83 else
84 text = t.callsign;
85 tl = new TextLayout(text, model.target_font, frc);
86 t.bounds = tl.getBounds();
87 tl.draw(bufferGraphics, (float)t.x, (float)t.y);
90 private void processTargets() {
92 Target t;
93 boolean isMasked = false;
95 for (int i=0; i<model.targets.size(); i++) {
97 t = model.targets.get(i);
99 if (model.stage == Model.TrialStage.FIND) {
100 if ((model.mouse_x>=t.x) &&
101 (model.mouse_x<=(t.x+t.bounds.getWidth())) &&
102 (model.mouse_y>=(t.y-t.bounds.getHeight())) &&
103 (model.mouse_y<=t.y) )
104 isMasked = false;
105 else
106 isMasked = true;
109 if ((model.mouse_x_clk>=t.x) &&
110 (model.mouse_x_clk<=(t.x+t.bounds.getWidth())) &&
111 (model.mouse_y_clk>=(t.y-t.bounds.getHeight())) &&
112 (model.mouse_y_clk<=t.y) ) {
113 if (model.targets.get(model.target)==t) {
114 model.stopTime = System.currentTimeMillis();
115 stopTrial();
116 //System.out.println(model.stopTime - model.startTime);
120 drawTarget(t, isMasked);
126 public void drawFrame() {
127 bufferGraphics.setColor(model.target_color);
128 bufferGraphics.drawRect(0, 0, screen_size.width-1,
129 screen_size.height-1);
130 bufferGraphics.drawRect(0, screen_size.height-1-mh,
131 screen_size.width-1, screen_size.height-1);
134 public void drawQuery() {
135 bufferGraphics.setColor(model.query_color);
136 TextLayout tl = new TextLayout("Click on target: " + "\"" +
137 model.targets.get(model.target).callsign + "\"",
138 model.query_font, frc);
139 tl.draw(bufferGraphics,
140 (int)Math.round(
141 screen_size.width / 2 - tl.getBounds().getCenterX()
143 (int)Math.round(
144 screen_size.height - mh / 2 +
145 tl.getBounds().getCenterY() / 2
148 bufferGraphics.setColor(model.target_color);
151 public void paint(Graphics g) {
152 Graphics2D g2 = (Graphics2D)g;
153 bufferGraphics.clearRect(0, 0, screen_size.width,
154 screen_size.height);
155 drawFrame();
156 if (model.stage == Model.TrialStage.FIND)
157 drawQuery();
158 processTargets();
159 g2.drawImage(offscreen, 0, 0, this);
162 public void update(Graphics g) {
163 paint(g);
166 protected void stopTrial() {
167 model.stage = Model.TrialStage.END;
168 dispose();
169 if (!model.waiting) {
170 synchronized (model.trialThread) {
171 model.trialThread.notifyAll();