这个怎样加载到页面上,也就是说怎么显示在页面上

wuyingchanglang 2013-05-27 11:03:19
package demo ;

//实时曲线——时序图
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.Millisecond;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

public class RealTimeChart extends ChartPanel implements Runnable,ActionListener
{
private static final long serialVersionUID = 1L;
static JLabel show;//显示随机数
private static TimeSeries timeSeries;
public RealTimeChart(String chartContent,String title,String yaxisName)
{ //构造函数,第一个参数为图表内容,第二个参数为图表标题,第三个参数为纵坐标标题
super(createChart(chartContent,title,yaxisName)); //调用超类的方法createChart
}

private static JFreeChart createChart(String chartContent,String title,String yaxisName){
//创建时序图对象
timeSeries = new TimeSeries(chartContent,Millisecond.class);
TimeSeriesCollection timeseriescollection = new TimeSeriesCollection(timeSeries);
JFreeChart jfreechart = ChartFactory.createTimeSeriesChart(title,"Time",yaxisName,timeseriescollection,true,true,false);
XYPlot xyplot = jfreechart.getXYPlot(); //设定坐标系

//横坐标设定
ValueAxis valueaxis = xyplot.getDomainAxis();

//横坐标自动设置数据轴数据范围
valueaxis.setAutoRange(true);

//横坐标固定数据范围 30s
valueaxis.setFixedAutoRange(30000D);

//纵坐标设定
valueaxis = xyplot.getRangeAxis();

return jfreechart;
}
public int c;
public void run()
{
while(true)
{
String sql;
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String url="jdbc:mysql://localhost:3306/zeng";
String user="root";
String password="123456";
try {
conn= (Connection) DriverManager.getConnection(url,user,password);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
stmt=(Statement) conn.createStatement();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
sql="select * from ups_now";
try {
rs=stmt.executeQuery(sql);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
while(rs.next())
{
String b=rs.getString("out_current");
c = Integer.parseInt(b);
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
rs.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
stmt.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
conn.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

long a=c-randomNum();
timeSeries.add(new Millisecond(), a);
//第一个参数(当前时间)为横坐标的值
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
} //每隔一秒产生一个随机数
}

}

private long randomNum() //产生随机数
{
show.setText("随机数:"+(Math.random()*5-2.0));
long a=(long)(Math.random()*5-2.0);
//long a=9;
return a; //返回随机数
}


public static void main(String[] args)
{
JFrame frame=new JFrame("实时曲线");
RealTimeChart rtcp=new RealTimeChart("Real-time curves"," real-time curves","out_current");
frame.getContentPane().add(rtcp,BorderLayout.CENTER);
show=new JLabel();
frame.getContentPane().add(show,BorderLayout.SOUTH);
frame.pack();//窗口大小可随意变化
frame.setVisible(true);
(new Thread(rtcp)).start(); //启动线程,并自动执行run()函数
frame.addWindowListener(new WindowAdapter() //关闭窗口事件
{
public void windowClosing(WindowEvent windowevent)
{
System.exit(0);
}
});
}
}
...全文
68 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

51,409

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧