PageRenderTime 74ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/src/gui/CamOptions.java

https://bitbucket.org/Matthew23/kck_eyemouse
Java | 210 lines | 173 code | 33 blank | 4 comment | 44 complexity | 73b48764a272091b5037678e6f0b9d51 MD5 | raw file
  1. package gui;
  2. import java.awt.BorderLayout;
  3. import java.awt.Dimension;
  4. import java.awt.Font;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.awt.event.ItemEvent;
  8. import java.awt.event.ItemListener;
  9. import java.awt.event.MouseEvent;
  10. import java.awt.event.MouseListener;
  11. import javax.swing.BoxLayout;
  12. import javax.swing.ButtonGroup;
  13. import javax.swing.JButton;
  14. import javax.swing.JCheckBox;
  15. import javax.swing.JFrame;
  16. import javax.swing.JLabel;
  17. import javax.swing.JPanel;
  18. import javax.swing.JRadioButton;
  19. import javax.swing.JSlider;
  20. import javax.swing.JTabbedPane;
  21. import javax.swing.event.ChangeEvent;
  22. import javax.swing.event.ChangeListener;
  23. import cv.Config;
  24. class MyRadio extends JRadioButton {
  25. public MyRadio(String name) {
  26. this(name, false);
  27. }
  28. public MyRadio(String name, Boolean selected) {
  29. super(name, selected);
  30. setActionCommand(name);
  31. }
  32. }
  33. public class CamOptions extends JFrame implements ItemListener, MouseListener,
  34. ActionListener {
  35. public static Boolean laserDetection = false, showHSVValues = false,
  36. frameDelay = false, calibrationPointsDetection = false,
  37. threshold = false, dilate = true;
  38. public static JLabel hsvValues = new JLabel("(-1, -1, -1)");
  39. public static String whatToThreshold = "TARGET_LASER";
  40. protected JCheckBox ld, showHSV, fDelay, cpd, thres, dilateErode;
  41. protected JButton saveBtn;
  42. protected JTabbedPane tabs;
  43. protected MySlider cs_hw_factor, laser_area, cs_area;
  44. protected ButtonGroup radioGroup;
  45. public CamOptions() {
  46. super("Cam Options");
  47. this.setLayout(new BoxLayout(this.getContentPane(), BoxLayout.PAGE_AXIS));
  48. this.setMinimumSize(new Dimension(350, 600));
  49. this.ld = new JCheckBox("Laser detection");
  50. this.ld.addItemListener(this);
  51. this.cpd = new JCheckBox("Calibration points detection");
  52. this.cpd.addItemListener(this);
  53. this.showHSV = new JCheckBox("Show HSV Values");
  54. this.showHSV.addItemListener(this);
  55. this.fDelay = new JCheckBox("Video frame delay");
  56. this.fDelay.addItemListener(this);
  57. this.thres = new JCheckBox("Threshold");
  58. this.thres.addItemListener(this);
  59. this.dilateErode = new JCheckBox("Erode & Dilate");
  60. this.dilateErode.setSelected(true);
  61. this.dilateErode.addItemListener(this);
  62. this.saveBtn = new JButton("Save");
  63. this.saveBtn.addMouseListener(this);
  64. this.cs_hw_factor = new MySlider("CS width-height factor", 0, 100,
  65. "MAX_CS_HEIGHT_WIDTH_FACTOR");
  66. this.laser_area = new MySlider("Min laser area", 0, 300,
  67. "MIN_LASER_AREA");
  68. this.cs_area = new MySlider("min CS Area", 0, 8000, "MIN_CS_AREA");
  69. this.tabs = new JTabbedPane();
  70. String[] hsvKeys = { "HUE", "SATURATION", "VALUE" };
  71. String[] ranges = { "MIN", "MAX" };
  72. String[] items = { "LASER", /*"BOTTOM_RIGHT_CS", "TOP_RIGHT_CS",
  73. "BOTTOM_LEFT_CS", "TOP_LEFT_CS" */};
  74. String delimiter = "_";
  75. this.radioGroup = new ButtonGroup();
  76. for (String item : items) {
  77. JPanel panel = new JPanel();
  78. panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
  79. MyRadio radio = new MyRadio(item, false);
  80. radio.addActionListener(this);
  81. if (radioGroup.getButtonCount() == 0) {
  82. radio.setSelected(true);
  83. CamOptions.whatToThreshold = item;
  84. }
  85. radioGroup.add(radio);
  86. panel.add(radio);
  87. for (String hsvKey : hsvKeys) {
  88. int min = 0, max = 255;
  89. if (hsvKey == "HUE") {
  90. max = 180;
  91. }
  92. for (String range : ranges) {
  93. MySlider slider = new MySlider(range + delimiter + hsvKey,
  94. min, max, range + delimiter + item + delimiter
  95. + hsvKey);
  96. panel.add(slider);
  97. }
  98. }
  99. this.tabs.addTab(item, panel);
  100. }
  101. this.add(this.ld);
  102. this.add(this.cpd);
  103. this.add(this.thres);
  104. this.add(this.dilateErode);
  105. //this.add(this.cs_hw_factor);
  106. this.add(this.laser_area);
  107. //this.add(this.cs_area);
  108. // this.add(CamOptions.hsvValues);
  109. // this.add(this.fDelay);
  110. this.add(this.tabs);
  111. this.add(this.saveBtn);
  112. }
  113. @Override
  114. public void itemStateChanged(ItemEvent e) {
  115. Object source = e.getItemSelectable();
  116. if (source == this.ld) {
  117. if (e.getStateChange() == ItemEvent.SELECTED) {
  118. CamOptions.laserDetection = true;
  119. } else {
  120. CamOptions.laserDetection = false;
  121. }
  122. } else if (source == this.cpd) {
  123. if (e.getStateChange() == ItemEvent.SELECTED) {
  124. CamOptions.calibrationPointsDetection = true;
  125. } else {
  126. CamOptions.calibrationPointsDetection = false;
  127. }
  128. } else if (source == this.showHSV) {
  129. if (e.getStateChange() == ItemEvent.SELECTED) {
  130. CamOptions.showHSVValues = true;
  131. } else {
  132. CamOptions.showHSVValues = false;
  133. }
  134. } else if (source == this.fDelay) {
  135. if (e.getStateChange() == ItemEvent.SELECTED) {
  136. CamOptions.frameDelay = true;
  137. } else {
  138. CamOptions.frameDelay = false;
  139. }
  140. } else if (source == this.thres) {
  141. if (e.getStateChange() == ItemEvent.SELECTED) {
  142. CamOptions.threshold = true;
  143. } else {
  144. CamOptions.threshold = false;
  145. }
  146. } else if (source == this.dilateErode) {
  147. if (e.getStateChange() == ItemEvent.SELECTED) {
  148. CamOptions.dilate = true;
  149. } else {
  150. CamOptions.dilate = false;
  151. }
  152. }
  153. }
  154. @Override
  155. public void mouseClicked(MouseEvent e) {
  156. Object source = e.getSource();
  157. if (source == this.saveBtn) {
  158. Config.save();
  159. }
  160. }
  161. @Override
  162. public void mouseEntered(MouseEvent arg0) {
  163. }
  164. @Override
  165. public void mouseExited(MouseEvent arg0) {
  166. }
  167. @Override
  168. public void mousePressed(MouseEvent arg0) {
  169. }
  170. @Override
  171. public void mouseReleased(MouseEvent arg0) {
  172. }
  173. @Override
  174. public void actionPerformed(ActionEvent e) {
  175. CamOptions.whatToThreshold = e.getActionCommand();
  176. }
  177. }