Advertisement
Guest User

Untitled

a guest
Aug 25th, 2014
410
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.54 KB | None
  1. import java.awt.AWTException;
  2. import java.awt.Color;
  3. import java.awt.Graphics;
  4. import java.awt.Graphics2D;
  5. import java.awt.Point;
  6. import java.awt.Robot;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9. import java.awt.event.MouseEvent;
  10. import java.awt.event.MouseMotionListener;
  11. import java.util.ArrayList;
  12. import java.util.TimerTask;
  13.  
  14. import javax.swing.JFrame;
  15. import javax.swing.JPanel;
  16. import javax.swing.Timer;
  17.  
  18.  
  19. public final class Main extends JPanel implements MouseMotionListener {
  20.  
  21.     Timer timer;
  22.     int time = 10;
  23.     java.util.Timer taskTimer;
  24.     ArrayList<Point> mousePoints;
  25.     ArrayList<Long> times;
  26.     Robot robot;
  27.     int width, height;
  28.     ArrayList<Point> drawMousePoints;
  29.  
  30.     public Main(){
  31.         width = 500;
  32.         height = 500;
  33.         drawMousePoints = new ArrayList<>();
  34.  
  35.         robot = null;
  36.         try{
  37.             robot = new Robot();
  38.         }
  39.         catch(AWTException e){
  40.             System.out.println("The time machine malfunctioned... Reverting to 512 BC");
  41.         }
  42.         mousePoints = new ArrayList<>();
  43.         times = new ArrayList<>();
  44.  
  45.         taskTimer = new java.util.Timer();
  46.  
  47.         ActionListener al;
  48.         al = new ActionListener(){
  49.             @Override
  50.             public void actionPerformed(ActionEvent e){
  51.                 time--;
  52.                 if(time == 0)
  53.                     rewind();
  54.                 repaint();
  55.             }
  56.         };
  57.         timer = new Timer(1000, al);
  58.         start();
  59.     }
  60.  
  61.     @Override
  62.     public void paint(Graphics g){
  63.         g.clearRect(0, 0, width, height);
  64.         g.drawString("Time Machine activiates in: " + time, 15, 50);
  65.         for(int i=0; i<drawMousePoints.size(); i++){
  66.             Point drawMousePoint = drawMousePoints.get(i);
  67.             drawMouse(drawMousePoint.x-getLocationOnScreen().x, drawMousePoint.y-getLocationOnScreen().y, g, Color.BLACK, Color.LIGHT_GRAY, (double)i/drawMousePoints.size());
  68.         }
  69.     }
  70.  
  71.     public void drawMouse(int x, int y, Graphics g, Color line, Color fill, double alpha){
  72.         Graphics2D g2d = (Graphics2D)g;
  73.         g2d.setColor(new Color(fill.getRed(), fill.getGreen(), fill.getBlue(), (int)Math.max(Math.min(alpha*255, 255), 0)));
  74.         g2d.fillPolygon(new int[]{x, x, x+4, x+8, x+10, x+7, x+12}, new int[]{y, y+16, y+13, y+20, y+19, y+12, y+12}, 7);
  75.  
  76.         g2d.setColor(new Color(line.getRed(), line.getGreen(), line.getBlue(), (int)Math.max(Math.min(alpha*255, 255), 0)));
  77.         g2d.drawLine(x, y, x, y + 16);
  78.         g2d.drawLine(x, y+16, x+4, y+13);
  79.         g2d.drawLine(x+4, y+13, x+8, y+20);
  80.         g2d.drawLine(x+8, y+20, x+10, y+19);
  81.         g2d.drawLine(x+10, y+19, x+7, y+12);
  82.         g2d.drawLine(x+7, y+12, x+12, y+12);
  83.         g2d.drawLine(x+12, y+12, x, y);
  84.     }
  85.  
  86.     public void start(){
  87.         timer.start();
  88.         prevTime = System.currentTimeMillis();
  89.         mousePoints.clear();
  90.     }
  91.  
  92.     public void rewind(){
  93.         timer.stop();
  94.         long timeSum = 0;
  95.         for(int i=0; i<times.size(); i++){
  96.             timeSum += times.get(0);
  97.             final boolean done = i == times.size()-1;
  98.             taskTimer.schedule(new TimerTask(){
  99.                 @Override
  100.                 public void run(){
  101.                     Point point = mousePoints.remove(0);
  102.                     drawMousePoints.clear();
  103.                     drawMousePoints.addAll(mousePoints.subList(0, Math.min(mousePoints.size(), 30)));
  104.                     robot.mouseMove(point.x, point.y);
  105.                     repaint();
  106.                     if(done)
  107.                         System.exit(0);
  108.                 }
  109.             }, timeSum);
  110.         }
  111.     }
  112.  
  113.     long prevTime = 0;
  114.     public void record(MouseEvent m){
  115.         if(timer.isRunning()){
  116.             long Ttime = System.currentTimeMillis();
  117.             mousePoints.add(new Point(m.getXOnScreen(), m.getYOnScreen()));
  118.             times.add((Ttime-prevTime)/10);
  119.             prevTime = Ttime;
  120.         }
  121.     }
  122.  
  123.     public static void main(String[] args){
  124.  
  125.         Main timeMachine = new Main();
  126.  
  127.         JFrame frame = new JFrame("Time Machine");
  128.         frame.setSize(timeMachine.width, timeMachine.height);
  129.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  130.         frame.setVisible(true);
  131.         frame.addMouseMotionListener(timeMachine);
  132.  
  133.         frame.add(timeMachine);
  134.     }
  135.  
  136.     @Override
  137.     public void mouseDragged(MouseEvent m) {
  138.         record(m);
  139.     }
  140.  
  141.     @Override
  142.     public void mouseMoved(MouseEvent m) {
  143.         record(m);
  144.     }
  145.  
  146. }
Advertisement
RAW Paste Data Copied
Advertisement