我嘗試使用Java Swing模擬無人機羣集。jPanel的同步
無人機的飛行跟蹤紅外點。
每個羣組成員類擴展JPanel並重寫paint函數,它的作用是根據領導地位
@Override
public void paint(Graphics g) {
super.paint(g);
for (int x = currX_1 - currIRdim; x < currX_1 + currIRdim; x++) {
for (int y = currY_1 - currIRdim; y < currY_1 + currIRdim; y++) {
g.setColor(Color.RED);
g.drawLine(x, y, x, y);
}
}
for (int x = currX_2 - currIRdim; x < currX_2 + currIRdim; x++) {
for (int y = currY_2 - currIRdim; y < currY_2 + currIRdim; y++) {
g.setColor(Color.RED);
g.drawLine(x, y, x, y);
}
}
}
的羣的領導者和他身邊的無人機更新以下成員的面板其面板上繪製IR點每個移動
private void updateFollowers(AgentIrPanel[] screensToUpdate, String command) {
int xdiff = screensToUpdate[0].getDiffX();
int dimdiff = screensToUpdate[0].getDiffDim();
switch (behaviour) {
case SWARM_LEADER:
screensToUpdate[0].setCurrX_1(screensToUpdate[0].getCurrX_1() + xdiff);
screensToUpdate[0].setCurrX_2(screensToUpdate[0].getCurrX_2() + xdiff);
screensToUpdate[0].repaintPoints(); // call jpanel.repaint()
screensToUpdate[1].setCurrIRdim(screensToUpdate[1].getCurrIRdim() - dimdiff);
screensToUpdate[1].repaintPoints();
break;
case FOLLOW_LEFT:
screensToUpdate[0].setCurrIRdim(screensToUpdate[0].getCurrIRdim() - dimdiff);
screensToUpdate[0].repaintPoints();
}
}
以下成員得到他們的面板2 IR點的和公認的從先前點的差異,這決定向他們應該移動
private String secRowReading() {
BufferedImage img = new BufferedImage(300, 300, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = img.createGraphics();
this.paint(g2);
if ((new Color(img.getRGB(X_1 - IRdim ,Y_1 - IRdim)).equals(Color.BLACK))
&& (new Color(img.getRGB(X_2 - IRdim ,Y_2 - IRdim)).equals(Color.BLACK))
&& (new Color(img.getRGB(X_1 - IRdim + diffX, Y_1 - IRdim)).equals(Color.RED))
&& (new Color(img.getRGB(X_2 - IRdim + diffX, Y_1 - IRdim)).equals(Color.RED))){
ans = "right";
}else if ((new Color(img.getRGB(X_1 - IRdim ,Y_1 - IRdim)).equals(Color.BLACK))
&& (new Color(img.getRGB(X_2 - IRdim ,Y_2 - IRdim)).equals(Color.BLACK))
&& (new Color(img.getRGB(X_1 - IRdim - diffX, Y_1 - IRdim)).equals(Color.RED))
&& (new Color(img.getRGB(X_2 - IRdim - diffX, Y_1 - IRdim)).equals(Color.RED))){
ans = "left";
}else if ((new Color(img.getRGB(X_1 - IRdim ,Y_1 - IRdim)).equals(Color.BLACK))
&& (new Color(img.getRGB(X_2 - IRdim,Y_2 - IRdim)).equals(Color.BLACK))
&& (new Color(img.getRGB(X_1 - IRdim + diffDim ,Y_1 - IRdim + diffDim)).equals(Color.RED))
&& (new Color(img.getRGB(X_2 - IRdim + diffDim ,Y_2 - IRdim + diffDim)).equals(Color.RED))) {
ans = "front";
}else if ((new Color(img.getRGB(X_1 - IRdim - diffDim,Y_1 - IRdim -diffDim)).equals(Color.RED))
&& (new Color(img.getRGB(X_2 - IRdim - diffDim,Y_2 - IRdim -diffDim)).equals(Color.RED))){
ans = "back";
}else {
ans = "stop";
}
g2.dispose();
return ans;
}
問題是 - 以下的無人機不能一起讀這個閱讀成功,只有當他們單獨操作時。 在飛行過程中,其中一名追隨者不能及時識別領導已經應用並停止的變化,這會擾亂整個航班。
我試圖用Timer()延遲每個後續無人機的閱讀計劃,但沒有成功。如何同步它以正常工作?
......................你好? –