Advertisement
Guest User

Untitled

a guest
Feb 13th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.38 KB | None | 0 0
  1. package coloronmouse;
  2. import java.awt.AWTException;
  3. import java.awt.BorderLayout;
  4. import java.awt.Color;
  5. import java.awt.Component;
  6. import java.awt.GridLayout;
  7. import java.awt.MouseInfo;
  8. import java.awt.Point;
  9. import java.awt.Robot;
  10. import java.awt.event.ActionEvent;
  11. import java.awt.event.ActionListener;
  12. import java.awt.event.MouseEvent;
  13. import java.awt.event.MouseMotionListener;
  14. import java.io.BufferedWriter;
  15. import java.io.File;
  16. import java.io.FileWriter;
  17. import java.io.IOException;
  18. import java.io.PrintWriter;
  19. import java.util.HashSet;
  20. import java.util.Set;
  21. import javax.swing.JButton;
  22. import javax.swing.JFileChooser;
  23. import javax.swing.JFrame;
  24. import javax.swing.JLabel;
  25. import javax.swing.JPanel;
  26. import javax.swing.JTextField;
  27. import javax.swing.SwingUtilities;
  28. import javax.swing.Timer;
  29. import javax.swing.UIManager;
  30. import javax.swing.UnsupportedLookAndFeelException;
  31. import javax.swing.filechooser.FileNameExtensionFilter;
  32.  
  33. /**
  34. * This class checks the position every #DELAY milliseconds and
  35. * informs all registered MouseMotionListeners about position updates.
  36. */
  37. public class NewClass {
  38. /* the resolution of the mouse motion */
  39. private static final int DELAY = 100;
  40. private static JTextField red,blue,green,mousex,mousey,desc;
  41. private static JButton save;
  42. private static String filechosen;
  43.  
  44.  
  45. private Component component;
  46. private Timer timer;
  47. private Set<MouseMotionListener> mouseMotionListeners;
  48.  
  49. protected NewClass(Component component) {
  50. if (component == null) {
  51. throw new IllegalArgumentException("Null component not allowed.");
  52. }
  53.  
  54. this.component = component;
  55.  
  56. /* poll mouse coordinates at the given rate */
  57. timer = new Timer(DELAY, new ActionListener() {
  58. private Point lastPoint = MouseInfo.getPointerInfo().getLocation();
  59.  
  60. /* called every DELAY milliseconds to fetch the
  61. * current mouse coordinates */
  62. public synchronized void actionPerformed(ActionEvent e) {
  63. Point point = MouseInfo.getPointerInfo().getLocation();
  64.  
  65. if (!point.equals(lastPoint)) {
  66. fireMouseMotionEvent(point);
  67. }
  68.  
  69. lastPoint = point;
  70. }
  71. });
  72. mouseMotionListeners = new HashSet<MouseMotionListener>();
  73. }
  74.  
  75. public Component getComponent() {
  76. return component;
  77. }
  78.  
  79. public void start() {
  80. timer.start();
  81. }
  82.  
  83. public static void lookAndFeel()
  84. {
  85. try {
  86. UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  87. } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
  88. }
  89. }
  90.  
  91. public void stop() {
  92. timer.stop();
  93. }
  94.  
  95. public void addMouseMotionListener(MouseMotionListener listener) {
  96. synchronized (mouseMotionListeners) {
  97. mouseMotionListeners.add(listener);
  98. }
  99. }
  100.  
  101. public void removeMouseMotionListener(MouseMotionListener listener) {
  102. synchronized (mouseMotionListeners) {
  103. mouseMotionListeners.remove(listener);
  104. }
  105. }
  106.  
  107. protected void fireMouseMotionEvent(Point point) {
  108. synchronized (mouseMotionListeners) {
  109. for (final MouseMotionListener listener : mouseMotionListeners) {
  110. final MouseEvent event =
  111. new MouseEvent(component, MouseEvent.MOUSE_MOVED, System.currentTimeMillis(),
  112. 0, point.x, point.y, 0, false);
  113.  
  114. SwingUtilities.invokeLater(new Runnable() {
  115. public void run() {
  116. listener.mouseMoved(event);
  117. }
  118. });
  119. }
  120.  
  121. }
  122. }
  123.  
  124. public static File openFile(){
  125. File file = null;
  126. JFileChooser fileChooser = new JFileChooser();
  127. fileChooser.setDialogTitle("Select a .txt file to save to");
  128. FileNameExtensionFilter filter = new FileNameExtensionFilter(
  129. ".txt", "txt");
  130. fileChooser.setFileFilter(filter);
  131. int returnValue = fileChooser.showOpenDialog(null);
  132. if(returnValue == JFileChooser.APPROVE_OPTION) {
  133. file = fileChooser.getSelectedFile();
  134. filechosen = fileChooser.getSelectedFile().getAbsolutePath();
  135. }
  136. return file;
  137. }
  138.  
  139. public static void saveOutput()
  140. {
  141. try(PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(filechosen, true)))) {
  142. out.println(desc.getText());
  143. out.println("X: "+mousex.getText()+" Y: "+mousey.getText()+" Red: "
  144. +red.getText()+" Green: "+green.getText()+
  145. " Blue"+blue.getText()+"\n");
  146. }catch (IOException e) {
  147. }
  148. }
  149. /**
  150. * adds east panel
  151. * @return
  152. */
  153. public static JPanel addEast()
  154. {
  155. JPanel p = new JPanel();
  156. p.setLayout(new GridLayout(3,1));
  157. JPanel p1 = new JPanel();
  158. save = new JButton("save");
  159. p1.add(save);
  160.  
  161.  
  162. class ExitListener implements ActionListener
  163. {
  164. public void actionPerformed(ActionEvent e)
  165. {
  166. saveOutput();
  167. }
  168. }
  169. save.addActionListener(new ExitListener());
  170. desc = new JTextField(10);
  171. JPanel p2 = new JPanel();
  172. JPanel space = new JPanel();
  173. p2.add(desc);
  174. p.add(space);
  175. p.add(p2);
  176. p.add(p1);
  177.  
  178.  
  179. return p;
  180. }
  181.  
  182. /**
  183. * adds the fields
  184. * @return
  185. */
  186. public static JPanel addFields()
  187. {
  188. JPanel p = new JPanel();
  189. p.setLayout(new GridLayout(4,1));
  190.  
  191. JPanel p1 = new JPanel();
  192. JLabel l1 = new JLabel("red");
  193. red = new JTextField(3);
  194. p1.add(l1);
  195. p1.add(red);
  196. p.add(p1);
  197.  
  198. JPanel p2 = new JPanel();
  199. JLabel l2 = new JLabel("green");
  200. green = new JTextField(3);
  201. p2.add(l2);
  202. p2.add(green);
  203. p.add(p2);
  204.  
  205. JPanel p3 = new JPanel();
  206. JLabel l3 = new JLabel("blue");
  207. blue = new JTextField(3);
  208. p3.add(l3);
  209. p3.add(blue);
  210. p.add(p3);
  211.  
  212. JPanel p4 = new JPanel();
  213. p4.setLayout(new GridLayout(2,1));
  214. JPanel p5 = new JPanel();
  215. JPanel p6 = new JPanel();
  216. JLabel l4 = new JLabel("mouse location");
  217. mousex = new JTextField(4);
  218. mousey = new JTextField(4);
  219. p5.add(l4);
  220. p6.add(mousex);
  221. p6.add(mousey);
  222. p4.add(p5);
  223. p4.add(p6);
  224. p.add(p4);
  225.  
  226. return p;
  227. }
  228.  
  229.  
  230. /* Testing the ovserver */
  231. public static void main(String[] args) throws AWTException {
  232. NewClass.lookAndFeel();
  233. JFrame main = new JFrame("Color Finder");
  234. main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  235. main.setSize(275,300);
  236. main.setVisible(true);
  237. main.setLayout(new BorderLayout());
  238. main.add(addFields(),BorderLayout.WEST);
  239. main.add(addEast(),BorderLayout.EAST);
  240. main.getRootPane().setDefaultButton(save);
  241. NewClass.openFile();
  242. main.setVisible(true);
  243. Robot robot = new Robot();
  244.  
  245. NewClass mo = new NewClass(main);
  246. mo.addMouseMotionListener(new MouseMotionListener() {
  247. public void mouseMoved(MouseEvent e) {
  248. mousex.setText(new Integer(e.getX()).toString());
  249. mousey.setText(new Integer(e.getY()).toString());
  250. Color c = robot.getPixelColor(new Integer(e.getX()),
  251. new Integer(e.getY()));
  252. red.setText(new Integer(c.getRed()).toString());
  253. blue.setText(new Integer(c.getBlue()).toString());
  254. green.setText(new Integer(c.getGreen()).toString());
  255.  
  256. }
  257.  
  258. public void mouseDragged(MouseEvent e) {
  259. System.out.println("mouse dragged: " + e.getPoint());
  260. }
  261. });
  262.  
  263. mo.start();
  264. }
  265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement