small change
[rmh3093.git] / motsim / MOTSIM_Controller.java
blobdc342e35c6b041d3a8238121c7c076da455ac692
1 /**
2 * MOTSIM_Controller.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.Font;
35 import java.awt.event.ActionEvent;
36 import java.awt.event.ActionListener;
37 import java.util.HashSet;
38 import java.util.Random;
40 public class MOTSIM_Controller {
42 MOTSIM_Model model;
43 MOTSIM_View view;
45 TrialDataDynamic tdd;
47 Thread targetMoverThread;
49 public MOTSIM_Controller(MOTSIM_Model model, MOTSIM_View view) {
50 this.model = model;
51 this.view = view;
52 tdd = new TrialDataDynamic(view.screen_size);
55 public void registerActionListeners() {
56 view.cp.addStartListener(new StartListener());
59 class TargetMover implements Runnable {
60 public void run() {
61 while(model.move_targets == true) {
62 if (model.borderstyle == "Spawn") {
63 int spawn = 0;
64 HashSet<Target> remove = new HashSet<Target>();
65 for (Target t : model.targets) {
66 if (((t.x + t.velX) > (view.screen_size.width -
67 t.string_width)) || ((t.x + t.velX) < 0) ||
68 ((t.y + t.velY) > view.screen_size.height) ||
69 ((t.y + t.velY - (2*t.font_size/3)) < 0)) {
70 remove.add(t);
71 spawn++;
72 } else {
73 t.x += t.velX * t.velocityMOD;
74 t.y += t.velY * t.velocityMOD;
77 model.targets.removeAll(remove);
78 tdd.spawnTargetOnBorder(model.targets, spawn);
79 view.tp.repaint();
80 try {
81 Thread.sleep(20);
82 } catch (InterruptedException e) {
84 } else {
85 for (Target t : model.targets) {
86 if (((t.x + t.velX) > (view.screen_size.width -
87 t.string_width)) || ((t.x + t.velX) < 0)) {
88 t.velX = t.velX * -1;
90 if (((t.y + t.velY) > view.screen_size.height) ||
91 ((t.y + t.velY - (2*t.font_size/3)) < 0)) {
92 t.velY = t.velY * -1;
94 t.x += t.velX * t.velocityMOD;
95 t.y += t.velY * t.velocityMOD;
97 view.tp.repaint();
98 try {
99 Thread.sleep(20);
100 } catch (InterruptedException e) {
107 class StartListener implements ActionListener {
108 Random rand = new Random();
109 public void actionPerformed(ActionEvent e) {
110 model.targets = tdd.generate_targets((Integer)view.cp.
111 targets_spinner.getValue());
112 model.defaultvelocities =
113 view.cp.velocity_combobox.getSelectedItem().toString();
114 view.startTrial();
115 model.target_font = new Font(view.tp.getFont().getName(),
116 view.tp.getFont().getStyle(),
117 (Integer)view.cp.fontsize_spinner.getValue());
118 view.tp.bufferGraphics.setFont(model.target_font);
119 for (Target t : model.targets) {
120 t.velocityMOD = (Double)view.cp.speed_spinner.getValue();
121 if (model.defaultvelocities == "Gaussian") {
122 t.velocityMOD = t.velocityMOD * (1+Math.round(Math.pow(rand.nextGaussian(),2)))/2;
124 t.string_width = view.tp.bufferGraphics.getFontMetrics().
125 stringWidth(t.callsign);
126 t.font_size = view.tp.bufferGraphics.getFontMetrics().
127 getHeight();
128 int max;
129 max = view.screen_size.width - t.string_width;
130 if (t.x > max)
131 t.x = max;
132 max = view.screen_size.height - t.font_size;
133 if (t.y > max)
134 t.y = max;
135 if (t.y < t.font_size)
136 t.y = t.font_size;
138 model.move_targets = true;
139 model.borderstyle =
140 view.cp.borderstyle_combobox.getSelectedItem().toString();
141 targetMoverThread = new Thread(new TargetMover());
142 targetMoverThread.start();