at end of trial select a random target and make it blink
[rmh3093.git] / motsim / MOTSIM_View.java
blobf0db77518650e2946d7e94d667da202b62a9df52
1 /**
2 * MOTSIM_View.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.DisplayMode;
37 import java.awt.FlowLayout;
38 import java.awt.Graphics;
39 import java.awt.GraphicsEnvironment;
40 import java.awt.GridLayout;
41 import java.awt.Image;
42 import java.awt.Toolkit;
43 import java.awt.event.ActionListener;
44 import java.awt.event.MouseEvent;
45 import java.awt.event.MouseMotionListener;
46 import java.awt.event.WindowEvent;
47 import java.awt.event.WindowListener;
49 import javax.swing.JButton;
50 import javax.swing.JComboBox;
51 import javax.swing.JFrame;
52 import javax.swing.JLabel;
53 import javax.swing.JPanel;
54 import javax.swing.JSpinner;
55 import javax.swing.SpinnerNumberModel;
57 public class MOTSIM_View {
59 MOTSIM_Model model;
60 ControlPanel cp;
61 TrialPanel tp;
62 ActionListener startActionListener;
63 Dimension screen_size;
65 public MOTSIM_View(MOTSIM_Model model) {
66 this.model = model;
67 screen_size = Toolkit.getDefaultToolkit().getScreenSize();
68 int dpi = Toolkit.getDefaultToolkit().getScreenResolution();
69 System.out.println(dpi);
70 System.out.println(screen_size.width/dpi + "x" + screen_size.height/dpi);
71 model.ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
72 model.gd = model.ge.getDefaultScreenDevice();
73 model.display_mode_default = model.gd.getDisplayMode();
74 model.display_mode_available = model.gd.getDisplayModes();
75 for (DisplayMode dm : model.display_mode_available) {
76 if ((dm.getWidth() == 800) && (dm.getHeight() == 600)) {
77 model.display_mode_preferred = dm;
78 break;
83 public void startControlPanel() {
84 cp = new ControlPanel();
85 //cp.fontsize_spinner.setValue(cp.getFont().getSize());
88 public void startTrial() {
89 tp = new TrialPanel();
92 @SuppressWarnings("serial")
93 class ControlPanel extends JFrame {
95 JPanel options_panel;
96 JLabel targets_label;
97 JSpinner targets_spinner;
98 JLabel duration_label;
99 JSpinner duration_spinner;
100 JLabel speed_label;
101 JSpinner speed_spinner;
102 JPanel start_panel;
103 JButton start_button;
104 JLabel borderstyle_label;
105 JComboBox borderstyle_combobox;
106 JLabel entropy_label;
107 JComboBox entropy_combobox;
108 JLabel velocity_label;
109 JComboBox velocity_combobox;
110 JLabel fontsize_label;
111 JSpinner fontsize_spinner;
113 String[] borderStyles = {"Reflect", "Spawn"};
114 String[] entropyStyles = {"None", "Low", "Medium", "High"};
115 String[] defaultvelocities = {"Identical", "Gaussian"};
117 public ControlPanel() {
118 setDefaultCloseOperation(EXIT_ON_CLOSE);
119 setTitle("MOTSIM Control Panel");
120 setResizable(false);
121 setLocationRelativeTo(null);
122 setLayout(new FlowLayout());
123 options_panel = new JPanel(new GridLayout(7,2));
124 add(options_panel);
125 targets_label = new JLabel("Number of targets: ");
126 targets_spinner =
127 new JSpinner(new SpinnerNumberModel(8, 1, 50, 1));
128 targets_spinner.setFocusable(false);
129 options_panel.add(targets_label);
130 options_panel.add(targets_spinner);
131 fontsize_label = new JLabel("Font size: ");
132 fontsize_spinner =
133 new JSpinner(new SpinnerNumberModel(28, 1, 38, 1));
134 fontsize_spinner.setFocusable(false);
135 options_panel.add(fontsize_label);
136 options_panel.add(fontsize_spinner);
137 speed_label = new JLabel("Velocity multiplier: ");
138 speed_spinner =
139 new JSpinner(new SpinnerNumberModel(1, -10, 10, .01));
140 speed_spinner.setFocusable(false);
141 options_panel.add(speed_label);
142 options_panel.add(speed_spinner);
143 duration_label = new JLabel("Trial duration (seconds): ");
144 duration_spinner =
145 new JSpinner(new SpinnerNumberModel(10, 1, 300, 1));
146 options_panel.add(duration_label);
147 options_panel.add(duration_spinner);
148 velocity_label = new JLabel("Default velocities: ");
149 velocity_combobox = new JComboBox(defaultvelocities);
150 options_panel.add(velocity_label);
151 options_panel.add(velocity_combobox);
152 borderstyle_label = new JLabel("Border style: ");
153 borderstyle_combobox = new JComboBox(borderStyles);
154 options_panel.add(borderstyle_label);
155 options_panel.add(borderstyle_combobox);
156 entropy_label = new JLabel("Entropy: ");
157 entropy_combobox = new JComboBox(entropyStyles);
158 options_panel.add(entropy_label);
159 options_panel.add(entropy_combobox);
160 start_panel = new JPanel(new FlowLayout(FlowLayout.CENTER));
161 start_button = new JButton("Start");
162 start_button.setActionCommand("START");
163 start_button.setSelected(true);
164 start_panel.add(start_button);
165 add(start_panel);
166 pack();
169 void addStartListener(ActionListener l) {
170 start_button.addActionListener(l);
175 @SuppressWarnings("serial")
176 class TrialPanel extends JFrame
177 implements MouseMotionListener, WindowListener {
179 Graphics bufferGraphics;
180 Image offscreen;
182 int curX, curY;
184 public TrialPanel() {
185 setPreferredSize(screen_size);
186 setResizable(false);
187 setLocationRelativeTo(null);
188 setUndecorated(true);
189 addWindowListener(this);
190 addMouseMotionListener(this);
191 model.gd.setFullScreenWindow(this);
192 //model.gd.setDisplayMode(model.display_mode_preferred);
193 setBackground(Color.black);
194 offscreen = createImage(screen_size.width, screen_size.height);
195 bufferGraphics = offscreen.getGraphics();
198 public void drawTargets(Graphics g) {
199 bufferGraphics.setColor(model.target_color);
200 for (Target t : model.targets ) {
201 if (model.mask_targets) {
202 if (model.blink && (model.target == t)) {
203 bufferGraphics.setColor(model.target_blink_color);
204 bufferGraphics.drawString("XXXXXX", (int)Math.round(t.x),
205 (int)Math.round(t.y));
206 bufferGraphics.setColor(model.target_color);
207 } else
208 bufferGraphics.drawString("XXXXXX", (int)Math.round(t.x),
209 (int)Math.round(t.y));
210 } else
211 bufferGraphics.drawString(t.callsign, (int)Math.round(t.x),
212 (int)Math.round(t.y));
217 public void paint(Graphics g) {
218 bufferGraphics.clearRect(0, 0, screen_size.width,
219 screen_size.height);
220 drawTargets(g);
221 g.drawImage(offscreen, 0, 0, this);
224 public void update(Graphics g) {
225 paint(g);
228 public void mouseDragged(MouseEvent e) {
231 public void mouseMoved(MouseEvent e) {
234 public void windowActivated(WindowEvent e) {
237 public void windowClosed(WindowEvent e) {
240 public void windowClosing(WindowEvent e) {
243 public void windowDeactivated(WindowEvent e) {
244 model.mask_targets = false;
245 model.blink = false;
246 model.blink_targets = false;
247 model.move_targets = false;
248 //model.gd.setDisplayMode(model.display_mode_preferred);
251 public void windowDeiconified(WindowEvent e) {
254 public void windowIconified(WindowEvent e) {
257 public void windowOpened(WindowEvent e) {