Advertisement
Guest User

Thread problem

a guest
Aug 18th, 2012
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.10 KB | None | 0 0
  1. Here is the main class and below is the thread class
  2.  
  3.  
  4. MAIN CLASS
  5.  
  6. /*
  7. * To change this template, choose Tools | Templates
  8. * and open the template in the editor.
  9. */
  10. package uav;
  11.  
  12. import java.awt.MouseInfo;
  13. import java.awt.PointerInfo;
  14. import java.awt.event.ActionEvent;
  15. import java.awt.event.ActionListener;
  16. import java.util.logging.Level;
  17. import java.util.logging.Logger;
  18. import javax.swing.JButton;
  19. import javax.swing.JFrame;
  20. import javax.swing.JLabel;
  21. import javax.swing.JPanel;
  22.  
  23. /**
  24. *
  25. * @author User
  26. */
  27. public class tester {
  28.  
  29. static int startTime = (int) System.currentTimeMillis();
  30. static boolean trigger = false;
  31. static JLabel label = new JLabel();
  32. static JLabel status = new JLabel();
  33. static mouseLocate msl = new mouseLocate();
  34. static JButton startButton = new JButton("Begin tracking");
  35. static JButton stopButton = new JButton("Stop Tracker");
  36. static Thread myThread = new Thread(new mouseLocate());
  37.  
  38. public static void main(String[] args) {
  39. JFrame frame = new JFrame("Mouse Grabber");
  40. frame.setVisible(true);
  41. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  42. frame.setSize(500, 100);
  43. JPanel panel = new JPanel();
  44. frame.add(panel);
  45.  
  46.  
  47. panel.add(startButton);
  48. startButton.addActionListener(new startAction());
  49.  
  50. panel.add(stopButton);
  51. stopButton.addActionListener(new stopAction());
  52.  
  53. panel.add(label);
  54. panel.add(status);
  55.  
  56.  
  57. myThread.start();
  58.  
  59. }
  60.  
  61. static class startAction implements ActionListener {
  62.  
  63. public void actionPerformed(ActionEvent e) {
  64.  
  65. msl.setIsShutingDown(false);
  66. System.out.println(msl.isShutingDown);
  67.  
  68. }
  69. //RUN
  70. //myThread.start();
  71. }
  72.  
  73. static class stopAction implements ActionListener {
  74.  
  75. public void actionPerformed(ActionEvent e) {
  76.  
  77. msl.setIsShutingDown(true);
  78. System.out.println(msl.isShutingDown);
  79. }
  80. }
  81. }
  82.  
  83.  
  84.  
  85.  
  86. ***********************************************************************
  87. ***********************************************************************
  88. ***********************************************************************
  89.  
  90. THREAD CLASS
  91.  
  92.  
  93.  
  94. /*
  95. * To change this template, choose Tools | Templates
  96. * and open the template in the editor.
  97. */
  98. package uav;
  99.  
  100. import java.awt.MouseInfo;
  101. import java.awt.PointerInfo;
  102.  
  103. /**
  104. *
  105. * @author User
  106. */
  107. public class mouseLocate implements Runnable {
  108.  
  109. private boolean trigger = false;
  110. private int startTime = (int) System.currentTimeMillis();
  111. static String status = "";
  112. public volatile boolean isShutingDown;;
  113.  
  114. public void run() {
  115. int x, y;
  116. //isShutingDown = true;
  117. while (!isShutingDown) {
  118.  
  119. try {
  120. PointerInfo mouseLocation = MouseInfo.getPointerInfo();
  121. x = mouseLocation.getLocation().x;
  122. y = mouseLocation.getLocation().y;
  123. System.out.println("X:" + x + " Y:" + y + " Elapsed time: "
  124. + (((int) System.currentTimeMillis() - startTime) / 100));
  125.  
  126. } catch (Exception e) {
  127. System.out.println("Exception caught : " + e);
  128. }
  129. }
  130.  
  131. }
  132.  
  133. public void shutDown()
  134. {
  135. isShutingDown = true;
  136. }
  137.  
  138. public int getStartTime() {
  139. return startTime;
  140. }
  141.  
  142. public void setStartTime(int startTime) {
  143. this.startTime = startTime;
  144. }
  145.  
  146. public boolean isTrigger() {
  147. return trigger;
  148. }
  149.  
  150. public void setTrigger(boolean trigger) {
  151. this.trigger = trigger;
  152. }
  153.  
  154. public static String getStatus() {
  155. return status;
  156. }
  157.  
  158. public static void setStatus(String status) {
  159. mouseLocate.status = status;
  160. }
  161.  
  162. public boolean isIsShutingDown() {
  163. return isShutingDown;
  164. }
  165.  
  166. public void setIsShutingDown(boolean isShutingDown) {
  167. this.isShutingDown = isShutingDown;
  168. }
  169.  
  170.  
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement