【发布时间】:2014-07-24 00:22:58
【问题描述】:
我正在用 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;
}
}
【问题讨论】:
-
首先创建一个代表系统虚拟特性的模型。使用
javax.swing.Timer之类的东西来安排定期回调。更新注册到Timer的ActionListener内的模型状态,在负责渲染模型状态的主组件上调用repaint -
我会在杂货店买一盒 Mr Coffee 过滤器。
标签: java swing jpanel paintcomponent