-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
144 lines (129 loc) · 3.2 KB
/
Copy pathMain.java
File metadata and controls
144 lines (129 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import java.awt.Point;
import java.util.Scanner;
public class Main {
public static ImageFrame f = new ImageFrame(1000, 1000, 0.6f);
public static GoogleImagesScraper g = new GoogleImagesScraper();
public static void main(String[] args) throws Exception {
try (Scanner s = new Scanner(System.in)) {
while (true) {
handleCommand(s.nextLine());
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void handleCommand(String input) {
String[] cmdParts = input.split(" ", 2);
switch (cmdParts[0].toLowerCase()) {
// Help Command
case "help":
System.out.println(Messages.HELP_MESSAGE);
break;
// Exit Command
case "exit":
System.exit(0);
// Window Commands
case "window":
// Integrity check
if (cmdParts.length == 1) {
System.out.println(Messages.WINDOW_CMD_HELP);
return;
}
switch (cmdParts[1]) {
// Show Window
case "show":
f.openWindow();
break;
// Hide Window
case "hide":
f.hideWindow();
break;
}
break;
// Search Command
case "s":
case "show":
case "search":
// Integrity check
if (cmdParts.length == 1) {
System.out.println(Messages.SEARCH_CMD_HELP);
return;
}
// Search on Google and display it
g.setSearch(cmdParts[1], 0);
if (!displaySearch()) {
// No results available
System.out.println(Messages.NO_RESULTS_MESSAGE);
}
break;
// Position Commands
case "pos":
case "position":
Point pos = getPointFromInput(cmdParts);
if (pos == null) {
System.out.println(Messages.POS_CMD_HELP);
} else {
f.setWindowLocation(pos.x, pos.y);
}
break;
// Location Commands
case "scale":
Point scale = getPointFromInput(cmdParts);
if (scale == null) {
System.out.println(Messages.SCALE_CMD_HELP);
} else {
if (scale.x < 0 || scale.y < 0) {
System.out.println(Messages.SCALE_BELOW_ZERO_MESSAGE);
}
f.setWindowScale(scale.x, scale.y);
}
}
}
public static Point getPointFromInput(String[] cmdParts) {
// Integrity Check
if (cmdParts.length == 1) {
return null;
}
// Split and check for integrity
String[] pointInput = cmdParts[1].split(" ");
if (pointInput.length < 2) {
return null;
}
// Parse the values entered by the user
try {
// X Position
int x = -1;
System.out.println(pointInput[0]);
if (!pointInput[0].equals("reset"))
x = Integer.parseInt(pointInput[0]);
// Y Position
int y = -1;
if (!pointInput[1].equals("reset")) {
y = Integer.parseInt(pointInput[1]);
}
// Return the Point
return new Point(x,y);
} catch (Exception e) {
System.out.println(Messages.NAN_MESSAGE);
return null;
}
}
// Tries a hundred times to display an image. Returns false when no possible
// images were found
public static boolean displaySearch() {
try {
for (int i = 0; i < 100; i++) {
String url = g.getImageUrl(i);
System.out.println(url);
if (f.setCurrentImageFromURL(url)) {
f.closeWindow();
f.openWindow();
return true;
}
}
return false;
} catch (Exception e) {
return false;
}
}
}