7 Copyright (c) 2008, Ryan M. Hope
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
;
52 Dimension screen_size
;
55 public Trial(Model model
, Dimension screen_size
, EventHandler evh
) {
57 addWindowListener(evh
);
58 addMouseListener(evh
);
59 addMouseMotionListener(evh
);
62 this.screen_size
= screen_size
;
63 setPreferredSize(screen_size
);
65 setLocationRelativeTo(null);
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();
76 public void setupTrial() {
82 private void drawTarget(Target t
, boolean isMasked
) {
83 bufferGraphics
.setColor(model
.target_color
);
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() {
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
) )
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
;
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
,
145 screen_size
.width
/ 2 - tl
.getBounds().getCenterX()
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)
166 } catch (InterruptedException e
) {
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()
178 if (model
.stage
== Model
.TrialStage
.FIND_TARGET
)
182 g2
.drawImage(offscreen
, 0, 0, this);
185 public void update(Graphics g
) {
189 public void clearScreen() {
190 bufferGraphics
.clearRect(0, 0, screen_size
.width
,
194 protected void stopTrial() {