Not a member of Pastebin yet?
                        Sign Up,
                        it unlocks many cool features!                    
                - import java.awt.AWTException;
 - import java.awt.Color;
 - import java.awt.Graphics;
 - import java.awt.Graphics2D;
 - import java.awt.Point;
 - import java.awt.Robot;
 - import java.awt.event.ActionEvent;
 - import java.awt.event.ActionListener;
 - import java.awt.event.MouseEvent;
 - import java.awt.event.MouseMotionListener;
 - import java.util.ArrayList;
 - import java.util.TimerTask;
 - import javax.swing.JFrame;
 - import javax.swing.JPanel;
 - import javax.swing.Timer;
 - public final class Main extends JPanel implements MouseMotionListener {
 - Timer timer;
 - int time = 10;
 - java.util.Timer taskTimer;
 - ArrayList<Point> mousePoints;
 - ArrayList<Long> times;
 - Robot robot;
 - int width, height;
 - ArrayList<Point> drawMousePoints;
 - public Main(){
 - width = 500;
 - height = 500;
 - drawMousePoints = new ArrayList<>();
 - robot = null;
 - try{
 - robot = new Robot();
 - }
 - catch(AWTException e){
 - System.out.println("The time machine malfunctioned... Reverting to 512 BC");
 - }
 - mousePoints = new ArrayList<>();
 - times = new ArrayList<>();
 - taskTimer = new java.util.Timer();
 - ActionListener al;
 - al = new ActionListener(){
 - @Override
 - public void actionPerformed(ActionEvent e){
 - time--;
 - if(time == 0)
 - rewind();
 - repaint();
 - }
 - };
 - timer = new Timer(1000, al);
 - start();
 - }
 - @Override
 - public void paint(Graphics g){
 - g.clearRect(0, 0, width, height);
 - g.drawString("Time Machine activiates in: " + time, 15, 50);
 - for(int i=0; i<drawMousePoints.size(); i++){
 - Point drawMousePoint = drawMousePoints.get(i);
 - drawMouse(drawMousePoint.x-getLocationOnScreen().x, drawMousePoint.y-getLocationOnScreen().y, g, Color.BLACK, Color.LIGHT_GRAY, (double)i/drawMousePoints.size());
 - }
 - }
 - public void drawMouse(int x, int y, Graphics g, Color line, Color fill, double alpha){
 - Graphics2D g2d = (Graphics2D)g;
 - g2d.setColor(new Color(fill.getRed(), fill.getGreen(), fill.getBlue(), (int)Math.max(Math.min(alpha*255, 255), 0)));
 - 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);
 - g2d.setColor(new Color(line.getRed(), line.getGreen(), line.getBlue(), (int)Math.max(Math.min(alpha*255, 255), 0)));
 - g2d.drawLine(x, y, x, y + 16);
 - g2d.drawLine(x, y+16, x+4, y+13);
 - g2d.drawLine(x+4, y+13, x+8, y+20);
 - g2d.drawLine(x+8, y+20, x+10, y+19);
 - g2d.drawLine(x+10, y+19, x+7, y+12);
 - g2d.drawLine(x+7, y+12, x+12, y+12);
 - g2d.drawLine(x+12, y+12, x, y);
 - }
 - public void start(){
 - timer.start();
 - prevTime = System.currentTimeMillis();
 - mousePoints.clear();
 - }
 - public void rewind(){
 - timer.stop();
 - long timeSum = 0;
 - for(int i=0; i<times.size(); i++){
 - timeSum += times.get(0);
 - final boolean done = i == times.size()-1;
 - taskTimer.schedule(new TimerTask(){
 - @Override
 - public void run(){
 - Point point = mousePoints.remove(0);
 - drawMousePoints.clear();
 - drawMousePoints.addAll(mousePoints.subList(0, Math.min(mousePoints.size(), 30)));
 - robot.mouseMove(point.x, point.y);
 - repaint();
 - if(done)
 - System.exit(0);
 - }
 - }, timeSum);
 - }
 - }
 - long prevTime = 0;
 - public void record(MouseEvent m){
 - if(timer.isRunning()){
 - long Ttime = System.currentTimeMillis();
 - mousePoints.add(new Point(m.getXOnScreen(), m.getYOnScreen()));
 - times.add((Ttime-prevTime)/10);
 - prevTime = Ttime;
 - }
 - }
 - public static void main(String[] args){
 - Main timeMachine = new Main();
 - JFrame frame = new JFrame("Time Machine");
 - frame.setSize(timeMachine.width, timeMachine.height);
 - frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 - frame.setVisible(true);
 - frame.addMouseMotionListener(timeMachine);
 - frame.add(timeMachine);
 - }
 - @Override
 - public void mouseDragged(MouseEvent m) {
 - record(m);
 - }
 - @Override
 - public void mouseMoved(MouseEvent m) {
 - record(m);
 - }
 - }
 
Advertisement
 
                    Add Comment                
                
                        Please, Sign In to add comment