在Java中实现粒子过滤器的最有效方法是什么?

时间:2014-07-24 00:22:59

标签: java swing jpanel paintcomponent

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

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

请告诉我如何处理项目设计,以便提高效率。

代码:

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 个答案:

答案 0 :(得分:3)

如果您的粒子交互模型足够简单,您可以使用javax.swing.Timer来调整模拟速度,如引用的KineticModel here所示。更灵活的方法可能会使用model-view-controller模式,检查here。您还可以在SwingWorker的背景中迭代模型,如图here所示。