在Java中实现粒子过滤器的最有效方法是什么? [英] What is the most efficient way to implement particle filters in Java?

查看:77
本文介绍了在Java中实现粒子过滤器的最有效方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Java实现粒子过滤器本地化,在其中我必须创建一个GUI,然后将其填充100个粒子和一个机器人.然后,我必须定期更新粒子和机器人.例如,我每次将x和y的值增加5个单位.这样的项目应该是什么设计?

I am working on implementing a particle filter localization in Java, in which I have to create a GUI and then fill it with 100 particles and a robot. Then, i have to update the particles and robot periodically. For example, I will increment the values of x and y by 5 units every time. What should be the design of such a project?

我有一个createGUI方法,正在调用构造函数来创建并填充框架中的粒子.但是,我将如何一次又一次地更新这些点.我会使用重绘还是会再次调用构造函数?

I am having a method createGUI and I am calling the constructor to create and fill the particles in the frame. But how will I update the points again and again. Will I use repaint or will I call constructor again?

请让我知道我应该如何处理项目的设计,以使其高效.

Please let me know how should I approach the design of my project, so that it is efficient.

到目前为止的代码:

import java.awt.*;
import java.util.*;
import javax.swing.*;

public class Filter {

    public static void main(String[] args)
    {
        createGUI();
        particleFilter();
    }

    //This method creates the basic GUI of the filter
    private static void createGUI()
    {

        //Creating the JFrame main window
        JFrame mainFrame = new JFrame();
        mainFrame.setSize(800, 500);
        mainFrame.setTitle("Particle Filter");
        mainFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        mainFrame.setLocation(100, 100);
        mainFrame.getContentPane().setLayout(new BoxLayout(mainFrame.getContentPane(), BoxLayout.X_AXIS));

        //creates two panels content and sidebar. Sidebar has null layout       
        JPanel content = new JPanel();
        content.setPreferredSize(new Dimension(700,500));
        content.setBackground(Color.LIGHT_GRAY);
        mainFrame.getContentPane().add(content);
        JPanel sidebar = new JPanel();
        sidebar.setBackground(Color.LIGHT_GRAY);
        sidebar.setPreferredSize(new Dimension(100,500));
        mainFrame.getContentPane().add(sidebar);
        sidebar.setLayout(null);

        //creates three buttons in sidebar
        JButton start_button = new JButton("START");
        start_button.setBounds(10, 75, 77, 23);
        sidebar.add(start_button);
        JButton stop_button = new JButton("STOP");
        stop_button.setBounds(10, 109, 77, 23);
        sidebar.add(stop_button);
        JButton reset_button = new JButton("RESET");
        reset_button.setBounds(10, 381, 77, 23);
        sidebar.add(reset_button);

        //calls the content_Walls class and sends the number of particles to be generated
        int n=1000; // n denotes the number of particles
        content.add( new content_Walls(n));
        mainFrame.setVisible(true);

    }

    private static void particleFilter()
    {

    }

}
    @SuppressWarnings("serial")
    class content_Walls extends JPanel
    {       
        ArrayList<Integer> list;
        content_Walls(int n)
        {
            setPreferredSize(new Dimension(680,450));
            setBackground(Color.WHITE);
            list = new ArrayList<Integer>(Collections.nCopies(n, 0));
        }       

        public void paintComponent(Graphics g)
        {
            int x,y=0;
            super.paintComponent(g);

            for(int i=0;i<list.size();i++)
            {
                x=randomInteger(11,670); // bounds of x between which the particles should be generated (reduced by 1 each)
                y=randomInteger(11,440);   // bounds of y between which the particles should be generated (reduced by 1 each)
                int radius = 4;
                g.fillOval(x, y, radius, radius);
            }

            x=randomInteger(11,670); 
            y=randomInteger(11,440);

            drawRobot(g,x,y,50);
            createObstacles(g,150,225,100,40);
            createObstacles(g,500,300,40,100);

            int xpoints[] = {50, 40, 60, 120};
            int ypoints[] = {50, 75, 100, 130};
            int npoints = 4;
            createPolygonObstacle(g,xpoints,ypoints,npoints);          

        }

        private void createPolygonObstacle(Graphics g, int xpoints[], int ypoints[], int npoints)
        {
             g.fillPolygon(xpoints, ypoints, npoints);
        }

        private void createObstacles(Graphics g, int x, int y, int width, int height)
        {
            g.setColor(Color.BLACK);
            g.fillRect(x, y, width, height);
        }

        private void drawRobot(Graphics g, int x, int y, int radius)
        {
            g.setColor(Color.GREEN);
            g.fillOval(x, y, radius, radius);           
        }

        private static int randomInteger(int min, int max)
        {
            Random rand = new Random();
            int randomNum = rand.nextInt((max - min) + 1) + min;
            return randomNum;
        }
    }

推荐答案

如果用于粒子交互的模型足够简单,则可以使用此处.一种更灵活的方法可以使用 model-view-controller 模式,该模式已检查此处.您还可以在SwingWorker的背景中迭代模型,如此处所示.

If your model for particle interaction is sufficiently simple, you can use a javax.swing.Timer to pace the simulation, as shown in the KineticModel cited here. A more flexible approach might use the model-view-controller pattern, examined here. You can also iterate your model in the background of a SwingWorker, as shown here.

这篇关于在Java中实现粒子过滤器的最有效方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆