我正在构建一个使用RFID标签跟踪物体位置的系统。我拥有一切,并认为该程序应该工作并编写这个动态Jfreechart,但没有显示点。
如何获得积分?
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.xy.XYDataItem;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
public class TrackingChart extends JFrame
{
private static final String title = "RFID Tracking";
private XYSeries moved = new XYSeries("Tags");
private final Timer timer;
private Table<String, Integer, Integer> tgs = HashBasedTable.create();
public TrackingChart(String s)
{
super(s);
update();
final ChartPanel chartPanel = createDemoPanel();
this.add(chartPanel, BorderLayout.CENTER);
JPanel control = new JPanel();
HashTableScatter app = new HashTableScatter();
timer = new Timer(100, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
moved.clear();
update();
}
});
control.add(new JButton(new AbstractAction("Start") {
@Override
public void actionPerformed(ActionEvent e) {
app.run("169.254.2.132");
timer.start();
tgs = app.gettgs();
}
}));
control.add(new JButton(new AbstractAction("Stop") {
@Override
public void actionPerformed(ActionEvent e) {
app.halt();
timer.stop();
}
}));
this.add(control, BorderLayout.SOUTH);
}
public double getx(int x, int y, int d)
{
/*
* RSSI = TxPower - 10 * n * lg(d)
* n = 2 (in free space)
*
* d = 10 ^ ((TxPower - RSSI) / (10 * n))
* max TxPower of Astra ex is -65 dbm will calibrate with more test
*/
double a = Math.pow(10, (-65 - x)/20);
double b = Math.pow(10, (-65 - y)/20);
double xpo = (b*b - a*a -d*d)/(-2d);
return xpo;
}
public double gety(int x, int y, int d)
{
double a = Math.pow(10, (-65 - x)/20);
double b = Math.pow(10, (-65 - y)/20);
double xpo = (b*b - a*a -d*d)/(-2d);
double ypo = Math.sqrt(a*a - xpo*xpo);
return ypo;
}
private void update() {
String[] as = tgs.rowKeySet().toArray(new String [tgs.rowKeySet().size()]);
for (int i = 0; i < tgs.rowKeySet().size(); i++) {
int a;
int b;
Integer x = tgs.get(as[i], 1);
if(x == null)
{
x = 0;
a = x;
}
else
{
a = x;
}
Integer y = tgs.get(as[i], 2);
if(y == null)
{
y = 0;
b = y;
}
else
{
b = y;
}
double xs = getx(a,b,10);
double ys = gety(a,b,10);
moved.add(new XYDataItem(xs,ys));
}
}
private ChartPanel createDemoPanel() {
JFreeChart jfreechart = ChartFactory.createScatterPlot(
title, "X", "Y", createSampleData(),
PlotOrientation.VERTICAL, true, true, false);
XYPlot xyPlot = (XYPlot) jfreechart.getPlot();
XYItemRenderer renderer = xyPlot.getRenderer();
NumberAxis domain = (NumberAxis) xyPlot.getDomainAxis();
domain.setRange(0, 10);
domain.setTickUnit(new NumberTickUnit(1));
NumberAxis range = (NumberAxis) xyPlot.getRangeAxis();
range.setRange(0, 10);
range.setTickUnit(new NumberTickUnit(1));
return new ChartPanel(jfreechart){
@Override
public Dimension getPreferredSize() {
return new Dimension(640, 480);
}
};
}
private XYDataset createSampleData() {
XYSeriesCollection xySeriesCollection = new XYSeriesCollection();
xySeriesCollection.addSeries(moved);
return xySeriesCollection;
}
public static void main(String args[]) throws InterruptedException {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
TrackingChart demo = new TrackingChart(title);
demo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
demo.pack();
demo.setLocationRelativeTo(null);
demo.setVisible(true);
}
});
}
}