Advertisement
Guest User

Untitled

a guest
Oct 28th, 2012
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.awt.Color;
  2. import java.awt.Graphics;
  3. import java.awt.Graphics2D;
  4. import java.awt.Rectangle;
  5. import javax.swing.*;
  6. import java.awt.event.*;
  7. import java.awt.geom.Ellipse2D;
  8. import java.io.FileReader;
  9. import java.io.IOException;
  10. import java.util.Calendar;
  11. import java.util.Random;
  12.  
  13. public class Reflexology1 extends JFrame{
  14.  
  15. private static final long serialVersionUID = -1295261024563143679L;
  16. private Ellipse2D ball = new Ellipse2D.Double(0, 0, 25, 25);
  17. private Timer moveBallTimer;
  18. int _ballXpos, _ballYpos;
  19. JButton button1, button2;
  20. JButton movingButton;
  21. JTextArea textArea1;
  22. int buttonAClicked, buttonDClicked;
  23. private long _openTime = 0;
  24. private long _closeTime = 0;
  25. JPanel thePanel = new JPanel();
  26. JPanel thePlacebo = new TestPanel();
  27. final JFrame frame = new JFrame("Reflexology");
  28. final JFrame frame2 = new JFrame("The Test");
  29. JLabel label1 = new JLabel("Press X and then click the moving dot as fast as you can.");
  30.  
  31. public static void main(String[] args){
  32. new Reflexology1();
  33. }
  34.  
  35. public Reflexology1(){
  36. frame.setSize(600, 475);
  37. frame.setLocationRelativeTo(null);
  38. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  39. frame.setTitle("Reflexology 1.0");
  40. frame.setResizable(false);
  41.  
  42. frame2.setSize(600, 475);
  43. frame2.setLocationRelativeTo(null);
  44. frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  45. frame2.setTitle("Reflexology 1.0");
  46. frame2.setResizable(false);
  47.  
  48. button1 = new JButton("Accept");
  49. button2 = new JButton("Decline");
  50. //movingButton = new JButton("Click Me");
  51.  
  52. ListenForAcceptButton lForAButton = new ListenForAcceptButton();
  53. ListenForDeclineButton lForDButton = new ListenForDeclineButton();
  54. button1.addActionListener(lForAButton);
  55. button2.addActionListener(lForDButton);
  56. //movingButton.addActionListener(lForMButton);
  57.  
  58. JTextArea textArea1 = new JTextArea(24, 50);
  59.  
  60. textArea1.setText("Tracking Events\n");
  61. textArea1.setLineWrap(true);
  62. textArea1.setWrapStyleWord(true);
  63. textArea1.setSize(15, 50);
  64. textArea1.setEditable(false);
  65.  
  66. FileReader reader = null;
  67. try {
  68. reader = new FileReader("EULA.txt");
  69. textArea1.read(reader, "EULA.txt");
  70. } catch (IOException exception) {
  71. System.err.println("Problem loading file");
  72. exception.printStackTrace();
  73. } finally {
  74. if (reader != null) {
  75. try {
  76. reader.close();
  77. } catch (IOException exception) {
  78. System.err.println("Error closing reader");
  79. exception.printStackTrace();
  80. }
  81. }
  82.  
  83. }
  84.  
  85. JScrollPane scrollBar1 = new JScrollPane(textArea1, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
  86. AdjustmentListener listener = new MyAdjustmentListener();
  87.  
  88. thePanel.add(scrollBar1);
  89. thePanel.add(button1);
  90. thePanel.add(button2);
  91.  
  92. frame.add(thePanel);
  93. ListenForMouse lForMouse = new ListenForMouse();
  94. thePlacebo.addMouseListener(lForMouse);
  95. thePlacebo.add(label1);
  96. frame2.add(thePlacebo);
  97.  
  98. ListenForWindow lForWindow = new ListenForWindow();
  99. frame.addWindowListener(lForWindow);
  100. frame2.addKeyListener(new KeyAdapter() {
  101. public void keyPressed(KeyEvent e){
  102. if(e.getKeyChar() == 'X' || e.getKeyChar() == 'x') {moveBallTimer.start();}
  103. }
  104. });
  105. frame.setVisible(true);
  106.  
  107. moveBallTimer = new Timer(1000, new ActionListener() {
  108. public void actionPerformed(ActionEvent e) {
  109. moveBall();
  110. System.out.println("Timer started!");
  111. repaint();
  112. }
  113. });
  114.  
  115. addKeyListener(new KeyAdapter() {
  116. public void keyPressed(KeyEvent e) {
  117. if(frame2.isVisible()){ moveBallTimer.start(); }
  118. }
  119. });
  120.  
  121. }
  122.  
  123.  
  124.  
  125. private class ListenForAcceptButton implements ActionListener{
  126. public void actionPerformed(ActionEvent e){
  127. if (e.getSource() == button1){
  128. Calendar ClCDateTime = Calendar.getInstance();
  129. System.out.println(ClCDateTime.getTimeInMillis() - _openTime);
  130. _closeTime = ClCDateTime.getTimeInMillis() - _openTime;
  131. //frame.getContentPane().remove(thePanel);
  132. //thePlacebo.addKeyListener(lForKeys);
  133. //frame.getContentPane().add(thePlacebo);
  134.  
  135. //frame.repaint();
  136. moveBallTimer.start();
  137. frame.setVisible(false);
  138. frame2.setVisible(true);
  139. frame2.invalidate();
  140. frame2.repaint();
  141.  
  142. thePlacebo.repaint();
  143. }
  144. }
  145. }
  146.  
  147.  
  148. private class ListenForDeclineButton implements ActionListener{
  149. public void actionPerformed(ActionEvent e){
  150. if (e.getSource() == button2){
  151. JOptionPane.showMessageDialog(Reflexology1.this, "You've declined the license agreement. DO NOT RESTART the program. Please go inform a researcher that you have declined the agreement.", "WARNING", JOptionPane.INFORMATION_MESSAGE);
  152. System.exit(0);
  153. }
  154. }
  155. }
  156.  
  157.  
  158. private class ListenForWindow implements WindowListener{
  159.  
  160. public void windowActivated(WindowEvent e) {
  161. //textArea1.append("Window is active");
  162.  
  163. }
  164.  
  165. // if this.dispose() is called, this is called:
  166. public void windowClosed(WindowEvent arg0) {
  167.  
  168. }
  169.  
  170. // When a window is closed from a menu, this is called:
  171. public void windowClosing(WindowEvent arg0) {
  172.  
  173. }
  174.  
  175. // Called when the window is no longer the active window:
  176. public void windowDeactivated(WindowEvent arg0) {
  177. //textArea1.append("Window is NOT active");
  178.  
  179. }
  180.  
  181. // Window gone from minimized to normal state
  182. public void windowDeiconified(WindowEvent arg0) {
  183. //textArea1.append("Window is in normal state");
  184.  
  185. }
  186.  
  187. // Window has been minimized
  188. public void windowIconified(WindowEvent arg0) {
  189. //textArea1.append("Window is minimized");
  190.  
  191. }
  192.  
  193. // Called when the Window is originally created
  194. public void windowOpened(WindowEvent arg0) {
  195. //textArea1.append("Let there be Window!");
  196. Calendar OlCDateTime = Calendar.getInstance();
  197. _openTime = OlCDateTime.getTimeInMillis();
  198. //System.out.println(_openTime);
  199.  
  200. }
  201.  
  202. }
  203.  
  204.  
  205. private class MyAdjustmentListener implements AdjustmentListener {
  206.  
  207. public void adjustmentValueChanged(AdjustmentEvent arg0) {
  208. AdjustmentEvent scrollBar1;
  209. //System.out.println(scrollBar1.getValue()));
  210.  
  211. }
  212.  
  213. }
  214.  
  215. class TestPanel extends JPanel {
  216. @Override
  217. public void paintComponent(Graphics g) {
  218. super.paintComponent(g);
  219.  
  220. Graphics2D g2d = (Graphics2D) g;
  221. g2d.setColor(Color.RED);
  222. g2d.fill(ball);
  223. System.out.println("Calling fill()");
  224. }
  225. }
  226.  
  227. Random rand = new Random();
  228.  
  229.  
  230. protected void moveBall() {
  231. //System.out.println("I'm in the moveBall() function!");
  232. int width = getWidth();
  233. int height = getHeight();
  234. int min, max, randomX, randomY;
  235. min = 0;
  236. max = 200;
  237. randomX = min + (int)(Math.random() * ((max - min)+1));
  238. randomY = min + (int)(Math.random() * ((max - min)+1));
  239. //System.out.println(randomX + ", " + randomY);
  240.  
  241. Rectangle ballBounds = ball.getBounds();
  242. // //System.out.println(ballBounds.x + ", " + ballBounds.y);
  243. // if (ballBounds.x + randomX < 0) {
  244. // randomX = 200;
  245. // } else if (ballBounds.x + ballBounds.width + randomX > width) {
  246. // randomX = -200;
  247. // }
  248. // if (ballBounds.y + randomY < 0) {
  249. // randomY = 200;
  250. // } else if (ballBounds.y + ballBounds.height + randomY > height) {
  251. // randomY = -200;
  252. // }
  253.  
  254. ballBounds.x = randomX;
  255. ballBounds.y = randomY;
  256. _ballXpos = ballBounds.x;
  257. _ballYpos = ballBounds.y;
  258. ball.setFrame(ballBounds);
  259. thePlacebo.repaint();
  260. }
  261.  
  262.  
  263.  
  264. public void start() {
  265. moveBallTimer.start();
  266. }
  267.  
  268. public void stop() {
  269. moveBallTimer.stop();
  270. }
  271.  
  272. private class ListenForMouse implements MouseListener{
  273.  
  274. // Called when the mouse is clicked
  275. public void mouseClicked(MouseEvent e) {
  276. //System.out.println("Mouse Panel pos: " + e.getX() + " " + e.getY() + "\n");
  277. if (e.getX() >=_ballXpos && e.getX() <= _ballXpos + 25 && e.getY() <=_ballYpos && e.getY() >= _ballYpos - 25 ) {
  278. System.out.println("TRUE");
  279. }
  280. System.out.println("{e.getX(): " + e.getX() + " / " + "_ballXpos: " + _ballXpos + " | " + "{e.getY(): " + e.getY() + " / " + "_ballYpos: " + _ballYpos);
  281.  
  282. }
  283.  
  284. public void mouseEntered(MouseEvent arg0) {
  285. // TODO Auto-generated method stub
  286.  
  287. }
  288.  
  289. public void mouseExited(MouseEvent arg0) {
  290. // TODO Auto-generated method stub
  291.  
  292. }
  293.  
  294. public void mousePressed(MouseEvent arg0) {
  295. // TODO Auto-generated method stub
  296.  
  297. }
  298.  
  299. public void mouseReleased(MouseEvent arg0) {
  300. // TODO Auto-generated method stub
  301.  
  302. }
  303. }
  304. // System.out.println("e.getX(): " + e.getX() + " / " + "_ballXpos: " + _ballXpos);
  305.  
  306.  
  307.  
  308. // Mouse over
  309. public void mouseEntered(MouseEvent arg0) {
  310. // TODO Auto-generated method stub
  311.  
  312. }
  313.  
  314. // Mouse left the mouseover area:
  315. public void mouseExited(MouseEvent arg0) {
  316. // TODO Auto-generated method stub
  317.  
  318. }
  319.  
  320. public void mousePressed(MouseEvent arg0) {
  321. // TODO Auto-generated method stub
  322.  
  323. }
  324.  
  325. public void mouseReleased(MouseEvent arg0) {
  326. // TODO Auto-generated method stub
  327.  
  328. }
  329.  
  330.  
  331.  
  332. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement