Started mouse moving function

This commit is contained in:
davpapp 2018-01-24 04:18:18 -05:00
parent 5fa3894245
commit a3fb536210
11 changed files with 84 additions and 50 deletions

Binary file not shown.

Binary file not shown.

BIN
bin/MousePoint.class Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1,10 +1,8 @@
/* Reads a file of coordinates
*
*
* Testing:
* - Only valid coordinates are added (through regex)
* -
*/
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.PointerInfo;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
@ -16,12 +14,28 @@ import java.util.regex.Pattern;
public class Mouse {
private ArrayList<MousePath> mousePaths;
PointerInfo pointer;
public Mouse(String path) {
pointer = MouseInfo.getPointerInfo();
mousePaths = new ArrayList<MousePath>();
readFile(path);
}
public void moveMouse(int xGoal, int yGoal) {
Point startingPoint = pointer.getLocation();
int x = (int) startingPoint.getX();
int y = (int) startingPoint.getY();
for (MousePath mousePath : mousePaths) {
MousePoint pathStartingPoint = mousePath.getStartingPoint();
if (pathStartingPoint.distance(startingPoint) < 20.0) {
System.out.println("Found possible path!");
mousePath.display();
}
}
}
public void readFile(String path) {
try {
File file = new File(path);
@ -30,9 +44,9 @@ public class Mouse {
Pattern linePattern = Pattern.compile("[0-9]*,[0-9]*,[0-9]*$");
String line;
Point lastPoint = new Point(0, 0, 0);
MousePoint lastPoint = new MousePoint(0, 0, 0);
int numberOfRepeats = 0;
ArrayList<Point> currentPath = new ArrayList<Point>();
ArrayList<MousePoint> currentPath = new ArrayList<MousePoint>();
currentPath.add(lastPoint);
while ((line = bufferedReader.readLine()) != null) {
@ -41,7 +55,7 @@ public class Mouse {
continue;
}
Point point = getPointFromLine(line);
MousePoint point = getPointFromLine(line);
if (!point.isValid()) {
continue;
}
@ -77,9 +91,22 @@ public class Mouse {
return false;
}
private Point getPointFromLine(String line) {
private MousePoint getPointFromLine(String line) {
String[] parts = line.split(Pattern.quote(","));
return new Point(Integer.parseInt(parts[0]), Integer.parseInt(parts[1]), Integer.parseInt(parts[2]));
return new MousePoint(Integer.parseInt(parts[0]), Integer.parseInt(parts[1]), Integer.parseInt(parts[2]));
}
/// ---------------------- Getters ----------------------------------------
public int getNumberOfPaths() {
return mousePaths.size();
}
public ArrayList<MousePath> getMousePaths() {
return mousePaths;
}
public void displayPaths() {
@ -89,13 +116,4 @@ public class Mouse {
}
System.out.println("There are " + mousePaths.size() + " paths.");
}
public int getNumberOfPaths() {
return mousePaths.size();
}
public ArrayList<MousePath> getMousePaths() {
return mousePaths;
}
}

View File

@ -6,17 +6,17 @@ import java.util.ArrayList;
public class MousePath {
private ArrayList<Point> path;
private ArrayList<MousePoint> path;
private int numPoints;
private Point startingPoint;
private Point endingPoint;
private MousePoint startingPoint;
private MousePoint endingPoint;
private int timespan;
public MousePath(ArrayList<Point> _path)
public MousePath(ArrayList<MousePoint> _path)
{
path = new ArrayList<Point>(_path.size());
for (Point point : _path) {
Point pointCopy = new Point(point.getX(), point.getY(), point.getTime());
path = new ArrayList<MousePoint>(_path.size());
for (MousePoint point : _path) {
MousePoint pointCopy = new MousePoint(point.getX(), point.getY(), point.getTime());
path.add(pointCopy);
}
numPoints = path.size();
@ -25,7 +25,7 @@ public class MousePath {
timespan = endingPoint.getTime() - startingPoint.getTime();
}
public ArrayList<Point> getPath() {
public ArrayList<MousePoint> getPath() {
return path;
}
@ -37,8 +37,16 @@ public class MousePath {
return timespan;
}
public MousePoint getStartingPoint() {
return startingPoint;
}
public MousePoint getEndingPoint() {
return endingPoint;
}
public void display() {
for (Point p : path) {
for (MousePoint p : path) {
System.out.println("(" + p.getX() + ", " + p.getY() + "), " + p.getTime());
}
System.out.println("Length:" + numPoints + ", Timespan:" + timespan);

View File

@ -1,10 +1,12 @@
public class Point {
import java.awt.Point;
public class MousePoint {
private int x;
private int y;
private int time;
public Point(int _x, int _y, int _time)
public MousePoint(int _x, int _y, int _time)
{
x = _x;
y = _y;
@ -26,10 +28,14 @@ public class Point {
return time;
}
public boolean isSameLocation(Point p2) {
public boolean isSameLocation(MousePoint p2) {
return (this.x == p2.getX() && this.y == p2.getY());
}
public double distance(MousePoint p2) {
return Math.hypot(this.x - p2.getX(), this.y - p2.getY());
}
public double distance(Point p2) {
return Math.hypot(this.x - p2.getX(), this.y - p2.getY());
}
@ -39,11 +45,11 @@ public class Point {
if (obj == null) {
return false;
}
if (!(obj instanceof Point)) {
if (!(obj instanceof MousePoint)) {
return false;
}
Point p = (Point) obj;
MousePoint p = (MousePoint) obj;
// Compare the data members and return accordingly
return (this.x == p.x && this.y == p.y && this.time == p.time);

View File

@ -6,7 +6,7 @@ class PointTest {
@Test
void initializePointTest() {
Point p = new Point(54, 4, 134);
MousePoint p = new MousePoint(54, 4, 134);
assertEquals(p.getX(), 54);
assertEquals(p.getY(), 4);
assertEquals(p.getTime(), 134);
@ -14,31 +14,31 @@ class PointTest {
@Test
void isValidPointTest() {
Point p1 = new Point(54, 4, 134);
MousePoint p1 = new MousePoint(54, 4, 134);
assertEquals(p1.isValid(), true);
Point p2 = new Point(-3, 84, 832);
MousePoint p2 = new MousePoint(-3, 84, 832);
assertEquals(p2.isValid(), false);
Point p3 = new Point(1940, 84, 832);
MousePoint p3 = new MousePoint(1940, 84, 832);
assertEquals(p3.isValid(), false);
Point p4 = new Point(3, -5, 832);
MousePoint p4 = new MousePoint(3, -5, 832);
assertEquals(p4.isValid(), false);
Point p5 = new Point(0, 1084, 832);
MousePoint p5 = new MousePoint(0, 1084, 832);
assertEquals(p5.isValid(), false);
Point p6 = new Point(0, 1001, -4);
MousePoint p6 = new MousePoint(0, 1001, -4);
assertEquals(p6.isValid(), false);
}
@Test
void isSameLocationTest() {
Point p1 = new Point(54, 4, 134);
Point p2 = new Point(54, 4, 832);
Point p3 = new Point(85, 4, 832);
Point p4 = new Point(54, 12, 832);
MousePoint p1 = new MousePoint(54, 4, 134);
MousePoint p2 = new MousePoint(54, 4, 832);
MousePoint p3 = new MousePoint(85, 4, 832);
MousePoint p4 = new MousePoint(54, 12, 832);
assertEquals(p1.isSameLocation(p2), true);
assertEquals(p1.isSameLocation(p3), false);
assertEquals(p1.isSameLocation(p4), false);
@ -46,10 +46,10 @@ class PointTest {
@Test
void distanceTest() {
Point p1 = new Point(54, 4, 134);
Point p2 = new Point(54, 4, 832);
Point p3 = new Point(85, 4, 832);
Point p4 = new Point(85, 12, 832);
MousePoint p1 = new MousePoint(54, 4, 134);
MousePoint p2 = new MousePoint(54, 4, 832);
MousePoint p3 = new MousePoint(85, 4, 832);
MousePoint p4 = new MousePoint(85, 12, 832);
assertEquals(p1.distance(p2), 0.0);
assertEquals(p1.distance(p3), 31.0);

View File

@ -8,7 +8,9 @@ public class main {
//URL url = main.class.getClassLoader().getResource("testfiles/mouse_path_test1.txt");
//System.out.println(url.getPath());
//getResource("testfiles/mouse_path_test1.txt");
Mouse mouse = new Mouse("/home/dpapp/eclipse-workspace/RunescapeAI/testfiles/mouse_path_test1.txt");
mouse.displayPaths();
System.out.println("Starting mouse script...");
Mouse mouse = new Mouse("/home/dpapp/GhostMouse/coordinates.txt");
//mouse.displayPaths();
mouse.moveMouse(743, 414);
}
}