我尝试使用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延迟读取每个跟随无人机的信息,但没有成功。如何使其同步才能正常工作?

最佳答案

每个群体成员类都扩展jPanel并覆盖绘画功能,其作用是根据领导者的位置在其面板上绘制IR点


这是一个问题。不要这样不要让Sprite扩展不必要的“重量级”类,并且不必要地使程序复杂化。而是让Sprites是非组件类,即,不要扩展任何Swing或AWT组件类型,而是在一个其paintComponent方法已被覆盖并且调用了super.paintComponent(g)的图形JPanel中绘制它们。

我给Swarm类一个public void draw(Graphics2D g2)方法,创建它们的集合,也许是List<Swarm>,然后在paintComponent方法中,遍历该列表,传入从JVM获得的Graphics2D对象,绘制每个sprite /群对象。

其他问题:确保从GUI代码中提取Swarm对象的行为和逻辑。逻辑代码应该是程序模型的一部分,而不是视图(GUI)的一部分,该视图的工作是显示模型的状态,并允许用户(通过控制器)与模型进行交互。

09-10 06:49
查看更多