Revision 533
separate file for RobotIcon in client
trunk/code/projects/colonet/client/Colonet.java | ||
---|---|---|
885 | 885 |
} |
886 | 886 |
|
887 | 887 |
public synchronized void paint (Graphics g) { |
888 |
if (img == null) |
|
888 |
if (img == null) {
|
|
889 | 889 |
return; |
890 |
} |
|
890 | 891 |
|
891 | 892 |
// Calculate scaling |
892 | 893 |
int maxWidth = getWidth() - 2*BORDER; |
... | ... | |
900 | 901 |
int y = 0; |
901 | 902 |
|
902 | 903 |
if (widthRatio > heightRatio) { //height is the limiting factor |
903 |
scale = heightRatio;
|
|
904 |
newHeight = maxHeight;
|
|
905 |
newWidth = (int) (img.getWidth() * scale);
|
|
906 |
y = BORDER;
|
|
907 |
x = (maxWidth - newWidth) / 2 + BORDER;
|
|
904 |
scale = heightRatio; |
|
905 |
newHeight = maxHeight; |
|
906 |
newWidth = (int) (img.getWidth() * scale); |
|
907 |
y = BORDER; |
|
908 |
x = (maxWidth - newWidth) / 2 + BORDER; |
|
908 | 909 |
} else { //width is the limiting factor |
909 |
scale = widthRatio;
|
|
910 |
newWidth = maxWidth;
|
|
911 |
newHeight = (int) (img.getHeight() * scale);
|
|
912 |
x = BORDER;
|
|
913 |
y = (maxHeight - newHeight) / 2 + BORDER;
|
|
910 |
scale = widthRatio; |
|
911 |
newWidth = maxWidth; |
|
912 |
newHeight = (int) (img.getHeight() * scale); |
|
913 |
x = BORDER; |
|
914 |
y = (maxHeight - newHeight) / 2 + BORDER; |
|
914 | 915 |
} |
915 | 916 |
|
916 | 917 |
// Draw everything onto the buffer |
... | ... | |
924 | 925 |
|
925 | 926 |
// Draw Identifiers and battery levels |
926 | 927 |
if (robotIcons != null) { |
927 |
bufferedGraphics.setStroke(new BasicStroke(2)); |
|
928 |
for (int i = 0; i < robotIcons.size(); i++) { |
|
929 |
RobotIcon r = robotIcons.get(i); |
|
930 |
bufferedGraphics.setColor(r.color); |
|
931 |
// Identifier circle |
|
932 |
int px = (int) (x + r.x * scale); |
|
933 |
int py = (int) (y + r.y * scale); |
|
934 |
bufferedGraphics.drawOval(px-RADIUS, py-RADIUS, 2*r.RADIUS, 2*r.RADIUS); |
|
935 |
// Battery |
|
936 |
//if (r.battery >= 0) { |
|
937 |
bufferedGraphics.setColor(Color.GREEN); |
|
938 |
bufferedGraphics.fillRect(px+20, py+20, 30, 10); |
|
939 |
bufferedGraphics.setColor(Color.BLACK); |
|
940 |
bufferedGraphics.drawRect(px+20, py+20, 50, 10); |
|
941 |
//} |
|
942 |
// If the robot has a destination, draw the vector |
|
943 |
if (r.destx >= 0) { |
|
944 |
bufferedGraphics.drawLine(px, py, (int)(x + r.destx * scale), (int)(y + r.desty * scale)); |
|
945 |
} |
|
928 |
bufferedGraphics.setStroke(new BasicStroke(2)); |
|
929 |
for (int i = 0; i < robotIcons.size(); i++) { |
|
930 |
RobotIcon r = robotIcons.get(i); |
|
931 |
bufferedGraphics.setColor(r.color); |
|
932 |
// Identifier circle |
|
933 |
int px = (int) (x + r.x * scale); |
|
934 |
int py = (int) (y + r.y * scale); |
|
935 |
bufferedGraphics.drawOval(px-RADIUS, py-RADIUS, 2*r.RADIUS, 2*r.RADIUS); |
|
936 |
// Battery |
|
937 |
//if (r.battery >= 0) { |
|
938 |
bufferedGraphics.setColor(Color.GREEN); |
|
939 |
bufferedGraphics.fillRect(px+20, py+20, 30, 10); |
|
940 |
bufferedGraphics.setColor(Color.BLACK); |
|
941 |
bufferedGraphics.drawRect(px+20, py+20, 50, 10); |
|
942 |
//} |
|
943 |
// If the robot has a destination, draw the vector |
|
944 |
if (r.destx >= 0) { |
|
945 |
bufferedGraphics.drawLine(px, py, (int)(x + r.destx * scale), (int)(y + r.desty * scale)); |
|
946 | 946 |
} |
947 |
} |
|
947 | 948 |
} |
948 | 949 |
|
949 | 950 |
// Identify currently-selected robot |
950 |
if (selectedBot >= 0) { |
|
951 |
bufferedGraphics.setColor(Color.BLACK);
|
|
952 |
RobotIcon r = robotIcons.get(selectedBot);
|
|
953 |
int px = (int) (x + r.x * scale);
|
|
954 |
int py = (int) (y + r.y * scale);
|
|
955 |
bufferedGraphics.drawOval(px-RADIUS-6, py-RADIUS-6, 2*r.RADIUS+12, 2*r.RADIUS+12);
|
|
951 |
if (selectedBot >= 0 && selectedBot < robotIcons.size()) {
|
|
952 |
bufferedGraphics.setColor(Color.BLACK); |
|
953 |
RobotIcon r = robotIcons.get(selectedBot); |
|
954 |
int px = (int) (x + r.x * scale); |
|
955 |
int py = (int) (y + r.y * scale); |
|
956 |
bufferedGraphics.drawOval(px-RADIUS-6, py-RADIUS-6, 2*r.RADIUS+12, 2*r.RADIUS+12); |
|
956 | 957 |
} |
957 | 958 |
|
958 | 959 |
//Display buffered content |
... | ... | |
1061 | 1062 |
} |
1062 | 1063 |
|
1063 | 1064 |
/* |
1064 |
* RobotIcon class |
|
1065 |
* Provides a means for graphically representing and keeping track of webcam bots. |
|
1066 |
*/ |
|
1067 |
class RobotIcon { |
|
1068 |
public final int RADIUS = 30; |
|
1069 |
public final int CLOSE = 80; |
|
1070 |
|
|
1071 |
public int x, y; |
|
1072 |
public int destx, desty; |
|
1073 |
public int id; |
|
1074 |
public Color color; |
|
1075 |
public int battery; |
|
1076 |
|
|
1077 |
public RobotIcon (int id, int x, int y) { |
|
1078 |
this.color = Color.RED; |
|
1079 |
this.x = x; |
|
1080 |
this.y = y; |
|
1081 |
this.id = id; |
|
1082 |
this.destx = -1; |
|
1083 |
this.desty = -1; |
|
1084 |
this.battery = -1; |
|
1085 |
} |
|
1086 |
|
|
1087 |
/** |
|
1088 |
* Relocates this RobotIcon to a new coordinate point. |
|
1089 |
* |
|
1090 |
*/ |
|
1091 |
public void move (int newX, int newY) { |
|
1092 |
this.x = newX; |
|
1093 |
this.y = newY; |
|
1094 |
} |
|
1095 |
|
|
1096 |
/** |
|
1097 |
* Determines if a given point is within a reasonable range of the current location |
|
1098 |
* to be considered the same robot when moving. The threshold is determined by the |
|
1099 |
* CLOSE value. |
|
1100 |
* |
|
1101 |
* @returns Whether or not the given point is reasonably close to the current location. |
|
1102 |
* |
|
1103 |
*/ |
|
1104 |
public boolean isClose (int nx, int ny) { |
|
1105 |
int dist = (int) Point.distance(this.x, this.y, nx, ny); |
|
1106 |
return (dist < CLOSE); |
|
1107 |
} |
|
1108 |
|
|
1109 |
/** |
|
1110 |
* Determines whether a given point is within the rectangle that circumscribes the |
|
1111 |
* robot's circlular icon. Used for clicking on robots in webcam view. |
|
1112 |
* |
|
1113 |
*/ |
|
1114 |
public boolean contains (int px, int py) { |
|
1115 |
Rectangle rect = new Rectangle(x-RADIUS, y-RADIUS, 2*RADIUS, 2*RADIUS); |
|
1116 |
return rect.contains(px, py); |
|
1117 |
} |
|
1118 |
|
|
1119 |
public String toString () { |
|
1120 |
String s = "RobotIcon at (" + x + "," + y + "), id " + id; |
|
1121 |
return s; |
|
1122 |
} |
|
1123 |
|
|
1124 |
} |
|
1125 |
|
|
1126 |
|
|
1127 |
/* |
|
1128 | 1065 |
* VectorController class |
1129 | 1066 |
* Manages robot motion control graphically |
1130 | 1067 |
*/ |
trunk/code/projects/colonet/client/Makefile | ||
---|---|---|
20 | 20 |
|
21 | 21 |
default: $(DIR)/Colonet.class $(DIR)/ColonetServerInterface.class $(DIR)/Colonet.jar |
22 | 22 |
|
23 |
$(DIR)/Colonet.jar: $(DIR)/Colonet.class $(DIR)/ColonetServerInterface.class |
|
23 |
$(DIR)/Colonet.jar: $(DIR)/Colonet.class $(DIR)/ColonetServerInterface.class $(DIR)/RobotIcon.class
|
|
24 | 24 |
@echo --- Creating archive file --- |
25 | 25 |
cd $(DIR); jar cf Colonet.jar *.class; jarsigner -keystore colonetkey -storepass colonet Colonet.jar colonet |
26 | 26 |
@echo --- Done --- |
... | ... | |
32 | 32 |
$(DIR)/ColonetServerInterface.class: ColonetServerInterface.java |
33 | 33 |
$(JC) $(JCWARN) $(JCFLAGS) ColonetServerInterface.java |
34 | 34 |
|
35 |
$(DIR)/RobotIcon.class: RobotIcon.java |
|
36 |
$(JC) $(JCWARN) $(JCFLAGS) RobotIcon.java |
|
37 |
|
|
35 | 38 |
# Install into the local computer's www directory. |
36 | 39 |
# The user should use sudo when running make install. |
37 | 40 |
|
trunk/code/projects/colonet/client/RobotIcon.java | ||
---|---|---|
1 |
/* |
|
2 |
* RobotIcon class |
|
3 |
* Provides a means for graphically representing and keeping track of webcam bots. |
|
4 |
*/ |
|
5 |
|
|
6 |
import javax.swing.*; |
|
7 |
import javax.swing.event.*; |
|
8 |
import javax.imageio.*; |
|
9 |
import java.awt.*; |
|
10 |
import java.awt.image.*; |
|
11 |
import java.awt.event.*; |
|
12 |
import java.net.*; |
|
13 |
import java.io.*; |
|
14 |
import java.util.*; |
|
15 |
|
|
16 |
public class RobotIcon { |
|
17 |
public final int RADIUS = 30; |
|
18 |
public final int CLOSE = 80; |
|
19 |
|
|
20 |
public int x, y; |
|
21 |
public int destx, desty; |
|
22 |
public int id; |
|
23 |
public Color color; |
|
24 |
public int battery; |
|
25 |
|
|
26 |
public RobotIcon (int id, int x, int y) { |
|
27 |
this.color = Color.RED; |
|
28 |
this.x = x; |
|
29 |
this.y = y; |
|
30 |
this.id = id; |
|
31 |
this.destx = -1; |
|
32 |
this.desty = -1; |
|
33 |
this.battery = -1; |
|
34 |
} |
|
35 |
|
|
36 |
/** |
|
37 |
* Relocates this RobotIcon to a new coordinate point. |
|
38 |
* |
|
39 |
*/ |
|
40 |
public void move (int newX, int newY) { |
|
41 |
this.x = newX; |
|
42 |
this.y = newY; |
|
43 |
} |
|
44 |
|
|
45 |
/** |
|
46 |
* Determines if a given point is within a reasonable range of the current location |
|
47 |
* to be considered the same robot when moving. The threshold is determined by the |
|
48 |
* CLOSE value. |
|
49 |
* |
|
50 |
* @returns Whether or not the given point is reasonably close to the current location. |
|
51 |
* |
|
52 |
*/ |
|
53 |
public boolean isClose (int nx, int ny) { |
|
54 |
int dist = (int) Point.distance(this.x, this.y, nx, ny); |
|
55 |
return (dist < CLOSE); |
|
56 |
} |
|
57 |
|
|
58 |
/** |
|
59 |
* Determines whether a given point is within the rectangle that circumscribes the |
|
60 |
* robot's circlular icon. Used for clicking on robots in webcam view. |
|
61 |
* |
|
62 |
*/ |
|
63 |
public boolean contains (int px, int py) { |
|
64 |
Rectangle rect = new Rectangle(x-RADIUS, y-RADIUS, 2*RADIUS, 2*RADIUS); |
|
65 |
return rect.contains(px, py); |
|
66 |
} |
|
67 |
|
|
68 |
public String toString () { |
|
69 |
String s = "RobotIcon at (" + x + "," + y + "), id " + id; |
|
70 |
return s; |
|
71 |
} |
|
72 |
} |
|
0 | 73 |
Also available in: Unified diff