2 * MOTSIM_Controller.java
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
;
36 import java
.awt
.event
.ActionEvent
;
37 import java
.awt
.event
.ActionListener
;
38 import java
.util
.HashSet
;
39 import java
.util
.Iterator
;
40 import java
.util
.Random
;
42 public class MOTSIM_Controller
{
51 public MOTSIM_Controller(MOTSIM_Model model
, MOTSIM_View view
) {
55 tdd
= new TrialDataDynamic(view
.screen_size
);
58 public void registerActionListeners() {
59 view
.cp
.addStartListener(new StartListener());
62 private void pickTargetTarget() {
64 int target
= rand
.nextInt(model
.targets
.size());
65 for (Target t
: model
.targets
) {
73 protected synchronized void moveTargets() {
77 HashSet
<Target
> remove
= new HashSet
<Target
>();
78 for (Target t
: model
.targets
) {
80 if (model
.borderstyle
== "Spawn") {
81 if (((t
.x
+ t
.velX
) > (view
.screen_size
.width
- t
.string_width
)) ||
82 ((t
.x
+ t
.velX
) < 0) ||
83 ((t
.y
+ t
.velY
) > view
.screen_size
.height
) ||
84 ((t
.y
+ t
.velY
- (2*t
.font_size
/3)) < 0)) {
90 if (((t
.x
+ t
.velX
) > (view
.screen_size
.width
- t
.string_width
)) ||
91 ((t
.x
+ t
.velX
) < 0)) {
95 if (((t
.y
+ t
.velY
) > view
.screen_size
.height
) ||
96 ((t
.y
+ t
.velY
- (2*t
.font_size
/3)) < 0)) {
101 t
.moveAngle
= t
.moveAngle
+ 180;
105 if (model
.entropy
!= "None") {
106 if (t
.move
== false) {
107 if (model
.entropy
== "Low") {
108 entropy
= tdd
.generateEntropyLow();
109 } else if (model
.entropy
== "Medium") {
110 entropy
= tdd
.generateEntropyMedium();
111 } else if (model
.entropy
== "High") {
112 entropy
= tdd
.generateEntropyHigh();
114 t
.moveAngle
= (t
.moveAngle
+ entropy
[rand
.nextInt(10000)]) % 360;
123 t
.velX
= tdd
.calcAngleMoveX(t
.moveAngle
);
124 t
.velY
= tdd
.calcAngleMoveY(t
.moveAngle
);
126 t
.x
= t
.x
+ t
.velX
* t
.velocityMOD
;
127 t
.y
= t
.y
+ t
.velY
* t
.velocityMOD
;
129 if (model
.borderstyle
== "Spawn") {
130 model
.targets
.removeAll(remove
);
131 tdd
.spawnTargetOnBorder(model
.targets
, spawn
);
135 model
.duration
= model
.duration
+ sleep
;
137 } catch (InterruptedException e
) {
142 private void flipTargetColor() {
149 class TargetMover
implements Runnable
{
153 while (model
.move_targets
) {
154 if (model
.duration
< model
.maxduration
) {
157 model
.move_targets
= false;
158 model
.mask_targets
= true;
161 if (model
.mask_targets
) {
165 } catch (InterruptedException e1
) {
168 model
.blink_targets
= true;
170 while (model
.blink_targets
&& model
.mask_targets
) {
175 } catch (InterruptedException e1
) {
178 synchronized (model
.targetMoverThread
) {
180 model
.targetMoverThread
.wait();
181 } catch (InterruptedException e1
) {
189 class StartListener
implements ActionListener
{
191 public void actionPerformed(ActionEvent e
) {
192 model
.targets
= tdd
.generate_targets((Integer
)view
.cp
.
193 targets_spinner
.getValue());
194 model
.maxduration
= 1000 *
195 (Integer
)view
.cp
.duration_spinner
.getValue();
197 view
.cp
.entropy_combobox
.getSelectedItem().toString();
198 model
.defaultvelocities
=
199 view
.cp
.velocity_combobox
.getSelectedItem().toString();
201 model
.target_font
= new Font(view
.tp
.getFont().getName(),
202 view
.tp
.getFont().getStyle(),
203 (Integer
)view
.cp
.fontsize_spinner
.getValue());
204 view
.tp
.bufferGraphics
.setFont(model
.target_font
);
205 for (Target t
: model
.targets
) {
206 t
.velocityMOD
= (Double
)view
.cp
.speed_spinner
.getValue();
207 if (model
.defaultvelocities
== "Gaussian") {
208 t
.velocityMOD
= t
.velocityMOD
* (1+Math
.round(Math
.pow(rand
.nextGaussian(),2)))/2;
210 t
.string_width
= view
.tp
.bufferGraphics
.getFontMetrics().
211 stringWidth(t
.callsign
);
212 t
.font_size
= view
.tp
.bufferGraphics
.getFontMetrics().
215 max
= view
.screen_size
.width
- t
.string_width
;
218 max
= view
.screen_size
.height
- t
.font_size
;
221 if (t
.y
< t
.font_size
)
224 model
.move_targets
= true;
227 view
.cp
.borderstyle_combobox
.getSelectedItem().toString();
228 if (model
.targetMoverThread
== null) {
229 model
.targetMoverThread
= new Thread(new TargetMover());
230 model
.targetMoverThread
.start();
232 synchronized (model
.targetMoverThread
) {
233 model
.targetMoverThread
.notifyAll();