2018-01-24 02:56:52 -05:00
|
|
|
/* Reads a file of coordinates
|
|
|
|
*/
|
2018-01-24 04:18:18 -05:00
|
|
|
import java.awt.MouseInfo;
|
|
|
|
import java.awt.Point;
|
|
|
|
import java.awt.PointerInfo;
|
2018-01-23 21:50:06 -05:00
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileReader;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.ArrayList;
|
2018-01-25 03:59:36 -05:00
|
|
|
import java.util.HashMap;
|
2018-01-23 21:50:06 -05:00
|
|
|
import java.util.regex.Matcher;
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
|
|
public class Mouse {
|
2018-01-25 03:59:36 -05:00
|
|
|
private HashMap<Integer[], ArrayList<MousePath>> gridMap;
|
|
|
|
//private ArrayList<MousePath> mousePaths;
|
2018-01-24 04:18:18 -05:00
|
|
|
PointerInfo pointer;
|
2018-01-23 21:50:06 -05:00
|
|
|
|
|
|
|
public Mouse(String path) {
|
2018-01-25 03:59:36 -05:00
|
|
|
// TODO: Is there another way to get the pointer location??
|
2018-01-24 04:18:18 -05:00
|
|
|
pointer = MouseInfo.getPointerInfo();
|
2018-01-25 03:59:36 -05:00
|
|
|
gridMap = new HashMap<Integer[], ArrayList<MousePath>>();
|
|
|
|
ArrayList<MousePath> mousePaths = readFile(path);
|
|
|
|
assignPathsToGrid(mousePaths);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void moveMouse(int endingX, int endingY) {
|
|
|
|
int[] mouseLoc = getMouseLocation();
|
|
|
|
int deltaX = endingX - mouseLoc[0];
|
|
|
|
int deltaY = endingY - mouseLoc[1];
|
|
|
|
Integer[] gridKey = getGridMapKey(deltaX, deltaY);
|
|
|
|
|
|
|
|
// Fetch from map
|
2018-01-23 21:50:06 -05:00
|
|
|
}
|
|
|
|
|
2018-01-25 03:59:36 -05:00
|
|
|
|
|
|
|
public int[] getMouseLocation() {
|
2018-01-24 04:18:18 -05:00
|
|
|
Point startingPoint = pointer.getLocation();
|
|
|
|
int x = (int) startingPoint.getX();
|
|
|
|
int y = (int) startingPoint.getY();
|
2018-01-25 03:59:36 -05:00
|
|
|
int loc[] = {x, y};
|
|
|
|
return loc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Integer[] getGridMapKey(int deltaX, int deltaY) {
|
|
|
|
Integer[] gridKey = {deltaX / 100, deltaY / 100};
|
|
|
|
return gridKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void assignPathsToGrid(ArrayList<MousePath> mousePaths) {
|
|
|
|
Integer[] key1 = getGridMapKey(0, 0);
|
|
|
|
Integer[] key2 = getGridMapKey(0, 0);
|
|
|
|
gridMap.put(key1, new ArrayList<MousePath>());
|
|
|
|
if (gridMap.containsKey(key2)) {
|
|
|
|
System.out.println("same key!");
|
2018-01-24 04:18:18 -05:00
|
|
|
}
|
2018-01-25 03:59:36 -05:00
|
|
|
if (gridMap.containsKey(key1)) {
|
|
|
|
System.out.println("Same key2!");
|
|
|
|
}
|
|
|
|
/*for (MousePath mousePath : mousePaths) {
|
|
|
|
int deltaX = mousePath.getDeltaX();
|
|
|
|
int deltaY = mousePath.getDeltaY();
|
|
|
|
Integer[] gridKey = getGridMapKey(deltaX, deltaY);
|
|
|
|
|
|
|
|
if (gridMap.containsKey(gridKey)) {
|
|
|
|
System.out.println("Same category!");
|
|
|
|
gridMap.get(gridKey).add(mousePath);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ArrayList<MousePath> newPath = new ArrayList<MousePath>();
|
|
|
|
newPath.add(mousePath);
|
|
|
|
gridMap.put(gridKey, newPath);
|
|
|
|
}
|
|
|
|
}*/
|
2018-01-24 04:18:18 -05:00
|
|
|
}
|
|
|
|
|
2018-01-25 03:59:36 -05:00
|
|
|
public ArrayList<MousePath> readFile(String path) {
|
|
|
|
ArrayList<MousePath> mousePaths = new ArrayList<MousePath>();
|
2018-01-23 21:50:06 -05:00
|
|
|
try {
|
|
|
|
File file = new File(path);
|
|
|
|
FileReader fileReader = new FileReader(file);
|
|
|
|
BufferedReader bufferedReader = new BufferedReader(fileReader);
|
2018-01-24 02:56:52 -05:00
|
|
|
Pattern linePattern = Pattern.compile("[0-9]*,[0-9]*,[0-9]*$");
|
2018-01-23 21:50:06 -05:00
|
|
|
|
|
|
|
String line;
|
2018-01-24 04:18:18 -05:00
|
|
|
MousePoint lastPoint = new MousePoint(0, 0, 0);
|
2018-01-23 21:50:06 -05:00
|
|
|
int numberOfRepeats = 0;
|
2018-01-24 04:18:18 -05:00
|
|
|
ArrayList<MousePoint> currentPath = new ArrayList<MousePoint>();
|
2018-01-23 21:50:06 -05:00
|
|
|
currentPath.add(lastPoint);
|
2018-01-24 02:56:52 -05:00
|
|
|
|
2018-01-23 21:50:06 -05:00
|
|
|
while ((line = bufferedReader.readLine()) != null) {
|
|
|
|
if (!isLineValid(line, linePattern)) {
|
|
|
|
System.out.println(line + " does not match regex -- SKIPPING");
|
|
|
|
continue;
|
|
|
|
}
|
2018-01-24 02:56:52 -05:00
|
|
|
|
2018-01-24 04:18:18 -05:00
|
|
|
MousePoint point = getPointFromLine(line);
|
2018-01-24 02:56:52 -05:00
|
|
|
if (!point.isValid()) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-01-23 21:50:06 -05:00
|
|
|
|
2018-01-24 04:02:36 -05:00
|
|
|
if (point.isSameLocation(lastPoint)) {
|
2018-01-24 02:56:52 -05:00
|
|
|
numberOfRepeats++;
|
2018-01-23 21:50:06 -05:00
|
|
|
if (numberOfRepeats == 20) {
|
2018-01-24 02:56:52 -05:00
|
|
|
if (currentPath.size() < 5) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-01-23 21:50:06 -05:00
|
|
|
MousePath newPath = new MousePath(currentPath);
|
2018-01-24 02:56:52 -05:00
|
|
|
mousePaths.add(newPath); // Deep copies
|
2018-01-23 21:50:06 -05:00
|
|
|
currentPath.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
numberOfRepeats = 0;
|
|
|
|
currentPath.add(point);
|
|
|
|
}
|
|
|
|
lastPoint = point;
|
|
|
|
}
|
|
|
|
fileReader.close();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2018-01-25 03:59:36 -05:00
|
|
|
return mousePaths;
|
2018-01-23 21:50:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isLineValid(String line, Pattern linePattern) {
|
|
|
|
Matcher matcher = linePattern.matcher(line);
|
|
|
|
if (matcher.find()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-01-24 04:18:18 -05:00
|
|
|
private MousePoint getPointFromLine(String line) {
|
2018-01-24 02:56:52 -05:00
|
|
|
String[] parts = line.split(Pattern.quote(","));
|
2018-01-24 04:18:18 -05:00
|
|
|
return new MousePoint(Integer.parseInt(parts[0]), Integer.parseInt(parts[1]), Integer.parseInt(parts[2]));
|
2018-01-23 21:50:06 -05:00
|
|
|
}
|
|
|
|
|
2018-01-24 04:18:18 -05:00
|
|
|
|
|
|
|
public void displayPaths() {
|
2018-01-25 03:59:36 -05:00
|
|
|
System.out.println("Displaying paths in HashMap...");
|
|
|
|
for (HashMap.Entry<Integer[], ArrayList<MousePath>> entry : gridMap.entrySet()) {
|
|
|
|
Integer[] gridKey = entry.getKey();
|
|
|
|
System.out.println("Key is: (" + gridKey[0] + ", " + gridKey[1] + ")");
|
|
|
|
|
|
|
|
ArrayList<MousePath> mousePaths = entry.getValue();
|
|
|
|
System.out.println("There are " + mousePaths.size() + " paths with these deltas.");
|
|
|
|
for (MousePath path : mousePaths) {
|
|
|
|
//path.display();
|
|
|
|
System.out.println("----------------------------------------------------------");
|
|
|
|
}
|
|
|
|
System.out.println("Size of HashMap: " + gridMap.size());
|
2018-01-24 04:18:18 -05:00
|
|
|
}
|
2018-01-24 03:42:10 -05:00
|
|
|
}
|
2018-01-23 21:50:06 -05:00
|
|
|
}
|