本文原創,禁止轉載!
這個是基於物聯網的人流統計軟件
主界面類
package IotDemo;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.Font;
import javax.swing.JTextField;
import java.awt.Choice;
import java.awt.Label;
import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.awt.Button;
public class userUI extends JFrame {
static String oneHourData="";
private JPanel contentPane;
private static int minuteG;
private static int maxPeople;
private static int order=0;
private static String tel="";
private static JLabel label;
private static JTextField textField;
private static int isFirstRunning=1;
private JTextField people;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
userUI frame = new userUI();
frame.setUndecorated(true);
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public userUI() {
//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(0, 0, 822, 695);
contentPane = new JPanel();
//contentPane.setBorder(new EmptyBorder(0, 0, 0, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel(" \u5BA2\u6D41\u5206\u6790\u7D71\u8A08\u5DE5\u5177");
lblNewLabel.setFont(new Font("宋体", Font.BOLD, 36));
lblNewLabel.setBounds(0, 0, 800, 50);
contentPane.add(lblNewLabel);
textField = new JTextField();
textField.setBounds(0, 70, 538, 20);
contentPane.add(textField);
textField.setColumns(10);
JLabel lblNewLabel_1 = new JLabel("\u7D71\u8A08\u5340\u584A");
lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 20));
lblNewLabel_1.setForeground(Color.WHITE);
lblNewLabel_1.setBounds(0, 50, 800, 20);
lblNewLabel_1.setOpaque(true);
lblNewLabel_1.setBackground(Color.GRAY);
contentPane.add(lblNewLabel_1);
label = new JLabel("\u4E0A\u4E00\u5206\u9418\u5BA2\u6D41\u91CF\uFF1A0");
label.setFont(new Font("宋体", Font.PLAIN, 20));
label.setBounds(538, 70, 262, 20);
contentPane.add(label);
JLabel label_1 = new JLabel("\u5206\u6790\u5340\u584A");
label_1.setOpaque(true);
label_1.setForeground(Color.WHITE);
label_1.setFont(new Font("宋体", Font.PLAIN, 20));
label_1.setBackground(Color.GRAY);
label_1.setBounds(0, 164, 800, 20);
contentPane.add(label_1);
Choice cd = new Choice();
cd.setBounds(236, 199, 87, 27);
for(int i=1;i<=31;i++) {
cd.add(i+"");
}
contentPane.add(cd);
Choice ch = new Choice();
ch.setBounds(367, 199, 81, 27);
for(int i=0;i<=23;i++) {
ch.add(i+"");
}
contentPane.add(ch);
JLabel label_2 = new JLabel("\u65E5\u671F:");
label_2.setFont(new Font("宋体", Font.PLAIN, 20));
label_2.setBounds(34, 199, 53, 21);
contentPane.add(label_2);
Choice cm = new Choice();
cm.setBounds(105, 199, 87, 27);
for(int i=1;i<=12;i++) {
cm.add(i+"");
}
contentPane.add(cm);
JLabel label_3 = new JLabel("\u6708");
label_3.setBounds(198, 199, 32, 21);
contentPane.add(label_3);
Label label_4 = new Label("\u65E5");
label_4.setBounds(329, 196, 32, 30);
contentPane.add(label_4);
JLabel label_5 = new JLabel("\u6642");
label_5.setBounds(454, 199, 37, 21);
contentPane.add(label_5);
JButton button = new JButton("\u67E5\u8A62");
button.setFont(new Font("宋体", Font.PLAIN, 20));
button.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int month=Integer.parseInt(cm.getSelectedItem());
int day=Integer.parseInt(cd.getSelectedItem());
int hour=Integer.parseInt(ch.getSelectedItem());
String hourDataChain="";
dbConn c=new dbConn();
try {
hourDataChain=c.query(month, day, hour);
} catch (Exception e1) {
e1.printStackTrace();
}
try {
showChat(hourDataChain);}
catch (NullPointerException e1) {
JOptionPane.showMessageDialog(null, "您選擇時間沒有記錄。數據會有1小時的延遲。", "錯誤",JOptionPane.WARNING_MESSAGE);
}
}
});
button.setBounds(489, 197, 158, 29);
contentPane.add(button);
JButton button_1 = new JButton("\u9000\u51FA");
button_1.setFont(new Font("宋体", Font.PLAIN, 20));
button_1.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
System.exit(0);
}
});
button_1.setBounds(662, 197, 123, 29);
contentPane.add(button_1);
JLabel label_6 = new JLabel("\u52D5\u4F5C\u5340\u584A");
label_6.setOpaque(true);
label_6.setForeground(Color.WHITE);
label_6.setFont(new Font("宋体", Font.PLAIN, 20));
label_6.setBackground(Color.GRAY);
label_6.setBounds(0, 96, 800, 20);
contentPane.add(label_6);
JLabel label_7 = new JLabel("\u5105\u55AE\u5206\u9418\u5BA2\u6D41\u91CF\u9054\u5230");
label_7.setFont(new Font("宋体", Font.PLAIN, 20));
label_7.setBounds(0, 131, 180, 21);
contentPane.add(label_7);
people = new JTextField();
people.setFont(new Font("宋体", Font.PLAIN, 20));
people.setBounds(187, 128, 96, 27);
contentPane.add(people);
people.setColumns(10);
JLabel label_8 = new JLabel("\u4EBA\u6B21\u6642\uFF0C\u57F7\u884C\u52D5\u4F5C\uFF1A");
label_8.setFont(new Font("宋体", Font.PLAIN, 20));
label_8.setBounds(294, 131, 180, 21);
contentPane.add(label_8);
Choice action = new Choice();
action.setBounds(478, 128, 169, 27);
action.add("發送提醒短信");
action.add("開啓繼電器");
action.add("關閉程式");
contentPane.add(action);
JButton button_2 = new JButton("\u9001\u51FA");
button_2.setFont(new Font("宋体", Font.PLAIN, 20));
button_2.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
maxPeople=Integer.parseInt(people.getText());
order=action.getSelectedIndex();
if(order==0) {
System.out.println(tel);
}
}
});
button_2.setBounds(662, 128, 123, 29);
contentPane.add(button_2);
JLabel lblCs = new JLabel(" 2018 ");
lblCs.setFont(new Font("宋体", Font.PLAIN, 20));
lblCs.setBounds(0, 670, 800, 21);
contentPane.add(lblCs);
String randomData="50";
for(int i=0;i<59;i++) {
randomData+=("@"+(int)(Math.random()*100));
}
showChat(randomData);
new Thread(new Runnable() {
public void run() {
recorder();
textField.requestFocus();
}}).start();
textField.requestFocus();
}
public static void showChat(String hourDataChain) {
StandardChartTheme mChartTheme = new StandardChartTheme("TW");
mChartTheme.setLargeFont(new Font("黑体", Font.BOLD, 20));
mChartTheme.setExtraLargeFont(new Font("宋体", Font.PLAIN, 15));
mChartTheme.setRegularFont(new Font("宋体", Font.PLAIN, 15));
ChartFactory.setChartTheme(mChartTheme);
CategoryDataset mDataset = GetDataset(hourDataChain);
JFreeChart mChart = ChartFactory.createLineChart(
"",
"時間",
"人次",
mDataset,
PlotOrientation.VERTICAL,
true,
true,
false);
CategoryPlot mPlot = (CategoryPlot)mChart.getPlot();
mPlot.setBackgroundPaint(Color.LIGHT_GRAY);
mPlot.setRangeGridlinePaint(Color.BLUE);
mPlot.setOutlinePaint(Color.RED);
ChartFrame mChartFrame = new ChartFrame("", mChart);
mChartFrame.setUndecorated(true);
mChartFrame.pack();
mChartFrame.setAlwaysOnTop(true);
mChartFrame.setBounds(30, 250, 750, 400);
mChartFrame.setVisible(true);
}
public static CategoryDataset GetDataset(String hourDataChain)
{
DefaultCategoryDataset mDataset = new DefaultCategoryDataset();
String oneHourData[]=new String[60];
Arrays.fill(oneHourData,0,60,"0");
oneHourData=hourDataChain.split("@");
for(int i=0;i<60;i++) {
mDataset.addValue(Integer.parseInt(oneHourData[i]), "人次", ""+i);
}
return mDataset;
}
public static void recorder() {
while(true) {
Date d=new Date();
int month=d.getMonth();
int day=d.getDate();
int hour=d.getHours();
int minute=d.getMinutes();
int counter=textField.getText().length();
if(minute!=minuteG) {
label.setText("上一分鐘客流量:"+counter+"人次。");
if(counter>maxPeople) {
switch(order) {
case 0:sendMsm s=new sendMsm();
s.send(tel, "上一分鐘客流為"+counter+"人次,已超過您設定最大客流量。");
break;
case 1:JOptionPane.showMessageDialog(null, "沒有連接合適的繼電器。", "錯誤",JOptionPane.WARNING_MESSAGE); break;
case 2:System.exit(0);break;
}
}
if(minute==0&&isFirstRunning==0) {
dbConn c=new dbConn();
try {
c.insert(month, day, hour, oneHourData);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
textField.setText("");
textField.requestFocus();
oneHourData=""+counter;
System.out.println(oneHourData);
}else {
if(isFirstRunning==0) {
oneHourData+="@"+counter;
System.out.println(oneHourData);
}
else {
oneHourData=""+counter;
isFirstRunning=0;
System.out.println(oneHourData);
}
textField.setText("");
textField.requestFocus();
}
try {
Thread.sleep(1000*60);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}}
}
數據庫連接類
package IotDemo;
import java.sql.*;
public class dbConn {
public static Connection init() throws Exception {
Class.forName( "com.mysql.cj.jdbc.Driver" );
Connection conn = DriverManager.getConnection("jdbc:mysql://xxx.xxx.xxx.xxx:3306/iot", "iot", "xxxxxx" );
System.out.println("连接成功");
return conn;
}
public static String query(int month,int day,int hour) throws Exception {
//连接对象
Connection conn=init();
String result = null;
//select `Data` from iot where Month=2 AND Day=2 AND Hour=2;
PreparedStatement cmd=conn.prepareStatement("select `Data` from iot where Month="+
month+" AND Day="+day+" AND Hour="+hour+";");
System.out.println("select `Data` from iot where Month="+
month+" AND Day="+day+" AND Hour="+hour+";");
ResultSet set=cmd.executeQuery();
while (set.next()) {result= set.getString("Data");}
set.close();
conn.close();
return result;
}
public static void insert(int month,int day,int hour,String data) throws Exception {
Connection conn=init();
PreparedStatement psql;
psql = conn.prepareStatement("insert into iot(`Month`,`Day`,`Hour`,`Data`) "
+ "values(?,?,?,?)");
psql.setInt(1,month);
psql.setInt(2,day);
psql.setInt(3,hour);
psql.setNString(4,data);
psql.executeUpdate();
}
public static void update(int month,int day,int hour,String data) throws Exception {
String tempQueryResult=query(month,day,hour);
if(tempQueryResult!=null) {
Connection conn=init();
PreparedStatement psql;
psql = conn.prepareStatement("update iot set Data = '"+data+"' where Month="+month+" AND Day="+day+" AND Hour="+hour+";");
psql.executeUpdate();
System.out.println("已更新已有行!");
}
else {
insert(month,day,hour,data);
System.out.println("已插入新的行!");
}
}
public static void main(String[] args) throws Exception {
String temp="";
//update(3,3,4,"22@33@33@66@37");
for(int i=0;i<24;i++) {
for(int j=0;j<60;j++) {
if(j==0) {
temp+=(int)(Math.random()*30);
}
else {
if(i>0 && i<10) {
temp+="@"+(int)(Math.random()*30);
}
else {
temp+="@"+(int)(Math.random()*100);
}
}
}
update(9,28,i,temp);
temp="";
}
}
}
表格生成類
package IotDemo;
import java.awt.Color;
import java.awt.Font;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
public class Line {
public static void main(String[] args) {
StandardChartTheme mChartTheme = new StandardChartTheme("CN");
mChartTheme.setLargeFont(new Font("黑体", Font.BOLD, 20));
mChartTheme.setExtraLargeFont(new Font("宋体", Font.PLAIN, 15));
mChartTheme.setRegularFont(new Font("宋体", Font.PLAIN, 15));
ChartFactory.setChartTheme(mChartTheme);
CategoryDataset mDataset = GetDataset();
JFreeChart mChart = ChartFactory.createLineChart(
"",
"時間",
"人次",
mDataset,
PlotOrientation.VERTICAL,
true,
true,
false);
CategoryPlot mPlot = (CategoryPlot)mChart.getPlot();
mPlot.setBackgroundPaint(Color.LIGHT_GRAY);
mPlot.setRangeGridlinePaint(Color.BLUE);
mPlot.setOutlinePaint(Color.RED);
ChartFrame mChartFrame = new ChartFrame("", mChart);
mChartFrame.pack();
mChartFrame.setVisible(true);
}
public static CategoryDataset GetDataset()
{
DefaultCategoryDataset mDataset = new DefaultCategoryDataset();
mDataset.addValue(29, "9月28日", "9時");
mDataset.addValue(15, "9月28日", "10時");
mDataset.addValue(44, "9月28日", "11時");
mDataset.addValue(23, "9月28日", "12時");
mDataset.addValue(33, "9月28日", "13時");
mDataset.addValue(71, "9月28日", "14時");
mDataset.addValue(82, "9月28日", "15時");
mDataset.addValue(66, "9月28日", "16時");
mDataset.addValue(23, "9月28日", "17時");
mDataset.addValue(15, "9月28日", "18時");
return mDataset;
}
}






Comments NOTHING