我尝试使用Java Swing模拟大量无人机。
通过跟踪红外点,无人机的飞行成群。
每个群体成员类都扩展jPanel并覆盖绘制功能,其作用是根据领导者位置在其面板上绘制IR点
@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); } } }
虫群首领和他身边的无人驾驶飞机随时更新以下成员的面板
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()。schedule延迟读取每个跟随无人机的信息,但没有成功。如何使其同步才能正常工作?