swt和jfreechart做 java的监控界面程序

/*

 * testChart.java

 *

 * Created on 2008年9月10日, 下午11:14

 */

package chart;

 

//导入java2d包

import java.awt.BorderLayout;

import java.awt.Dimension;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.*;

import java.io.PrintStream;

//导入jfreechart包(chart)

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;

//导入jfreechart包(data)

import org.jfree.data.time.*;

import org.jfree.data.xy.XYDataset;

//导入jfreechart包(ui)

import org.jfree.ui.ApplicationFrame;

import org.jfree.ui.RefineryUtilities;

 

/**

 *

 * @author  Administrator

 */

public class testChart extends JFrame implements Runnable, ActionListener {

 

    //时序图数据集

    private TimeSeries timeseries,timeseries2;

    //Value坐标轴初始值

    private double lastValue,tValue;

    static Class class$org$jfree$data$time$Millisecond;

    static Thread thread1;

 

    /** Creates new form testChart */

    public testChart() {

        //super(new BorderLayout());

        thread1 = new Thread(this);

        lastValue = 100D;

        //创建时序图对象

        timeseries = new TimeSeries("Random Data", testChart.class$org$jfree$data$time$Millisecond != null ? testChart.class$org$jfree$data$time$Millisecond : (testChart.class$org$jfree$data$time$Millisecond = testChart.getClass("org.jfree.data.time.Millisecond")));

        TimeSeriesCollection timeseriescollection = new TimeSeriesCollection(timeseries);

        

        timeseries2 = new TimeSeries("sdf Data", testChart.class$org$jfree$data$time$Millisecond != null ? testChart.class$org$jfree$data$time$Millisecond : (testChart.class$org$jfree$data$time$Millisecond = testChart.getClass("org.jfree.data.time.Millisecond")));

        TimeSeriesCollection timeseriescollection2 = new TimeSeriesCollection(timeseries2);

        //创建图表面板

        ChartPanel chartpanel = new ChartPanel(createChart(timeseriescollection));

        chartpanel.setPreferredSize(new Dimension(500, 270));

        

        

        initComponents();

        

        jTabbedPane2.add(chartpanel);

        jTabbedPane3.add(chartpanel);

 

 

    }

 

    /** This method is called from within the constructor to

     * initialize the form.

     * WARNING: Do NOT modify this code. The content of this method is

     * always regenerated by the Form Editor.

     */

    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          

    private void initComponents() {

 

        jTabbedPane1 = new javax.swing.JTabbedPane();

        jTabbedPane3 = new javax.swing.JTabbedPane();

        jTabbedPane2 = new javax.swing.JTabbedPane();

        textField1 = new java.awt.TextField();

        textField2 = new java.awt.TextField();

 

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        setName("Form"); // NOI18N

 

        jTabbedPane1.setName("jTabbedPane1"); // NOI18N

 

        jTabbedPane3.setName("jTabbedPane3"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(desktopapplication1.DesktopApplication1.class).getContext().getResourceMap(testChart.class);

        jTabbedPane1.addTab(resourceMap.getString("jTabbedPane3.TabConstraints.tabTitle"), jTabbedPane3); // NOI18N

 

        jTabbedPane2.setName("jTabbedPane2"); // NOI18N

        jTabbedPane1.addTab(resourceMap.getString("jTabbedPane2.TabConstraints.tabTitle"), jTabbedPane2); // NOI18N

 

        textField1.setFont(resourceMap.getFont("textField1.font")); // NOI18N

        textField1.setName("textField1"); // NOI18N

        textField1.setText(resourceMap.getString("textField1.text")); // NOI18N

 

        textField2.setName("textField2"); // NOI18N

        textField2.setText(resourceMap.getString("textField2.text")); // NOI18N

 

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());

        getContentPane().setLayout(layout);

        layout.setHorizontalGroup(

            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

            .addGroup(layout.createSequentialGroup()

                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 608, javax.swing.GroupLayout.PREFERRED_SIZE)

                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)

                    .addComponent(textField2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

                    .addComponent(textField1, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE))

                .addContainerGap(88, Short.MAX_VALUE))

        );

        layout.setVerticalGroup(

            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()

                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 305, Short.MAX_VALUE)

                .addGap(57, 57, 57))

            .addGroup(layout.createSequentialGroup()

                .addGap(21, 21, 21)

                .addComponent(textField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

                .addComponent(textField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

                .addGap(287, 287, 287))

        );

 

        pack();

    }// </editor-fold>                        

    public static void startThread() {

        thread1.start();

    }

 

    /**

     * @param args the command line arguments

     */

    public static void main(String args[]) {

        testChart testchart = new testChart();

        testchart.pack();

        RefineryUtilities.centerFrameOnScreen(testchart);

        testchart.setVisible(true);

        startThread();

    }

 

    public void actionPerformed(ActionEvent e) {

        if (e.getActionCommand().equals("EXIT")) {

            thread1.destroy();

            System.exit(0);

        }

    }

 

    public void run() {

        while (true) {

            try {

                //根据实际需要在此处加入需要执行的代码

                double d = 0.9D + 0.2D * Math.random();

                lastValue = lastValue * d;

                double b = 0.9D + 0.2D * Math.random();

                tValue = tValue * b;

                Millisecond millisecond = new Millisecond();

                System.out.println("Now=" + millisecond.toString());

                timeseries.add(millisecond, lastValue);

                timeseries2.add(millisecond, tValue);

                textField1.setText(Double.toString(lastValue));

                textField2.setText(Double.toString(tValue));

                Thread.sleep(300);

            } catch (InterruptedException e) {

            }

        }

    }

 

    private JFreeChart createChart(XYDataset xydataset) {

        JFreeChart jfreechart = ChartFactory.createTimeSeriesChart("时序图例子", "时间", "温度值", xydataset, true, true, false);

        XYPlot xyplot = jfreechart.getXYPlot();

        //纵坐标设定

        ValueAxis valueaxis = xyplot.getDomainAxis();

        valueaxis.setAutoRange(true);

        valueaxis.setFixedAutoRange(60000D);

 

        valueaxis = xyplot.getRangeAxis();

        valueaxis.setRange(0.0D, 200D);

 

        return jfreechart;

    }

 

    static Class getClass(String s) {

        Class cls = null;

        try {

            cls = Class.forName(s);

        } catch (ClassNotFoundException cnfe) {

            throw new NoClassDefFoundError(cnfe.getMessage());

        }

        return cls;

    }

    // Variables declaration - do not modify                     

    private javax.swing.JTabbedPane jTabbedPane1;

    private javax.swing.JTabbedPane jTabbedPane2;

    private javax.swing.JTabbedPane jTabbedPane3;

    private java.awt.TextField textField1;

    private java.awt.TextField textField2;

    // End of variables declaration                   

}


你可能感兴趣的:(java,thread,UI,swing,jfreechart)