What is the most efficient way to implement particle filters in Java?

旧街凉风 提交于 2019-12-13 05:04:38

问题


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?

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.

Code So Far:

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;
        }
    }

回答1:


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.



来源:https://stackoverflow.com/questions/24923207/what-is-the-most-efficient-way-to-implement-particle-filters-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!