18.3 实例应用

正如在简介中所提及的,使用JFreeChart的applet可以在下面链接中看到:

http://www.object-refinery.com/jfreechart/applet.html

运行applet应用时,需要两个方面饿支持。一是代码方式创建applet,二是HTML文件用来调用applet。

18.3.1 HTML

因为applet需要引入额外的jar文件,所以在HTML中使用applet是显得非常重要。HTML applet标签如下:

<APPLET ARCHIVE="jfreechart-1.0.6-applet-demo.jar,
jfreechart-1.0.6.jar,jcommon-1.0.9.jar"
CODE="demo.applet.Applet1" width=640 height=260
ALT="You should see an applet, not this text.">
</APPLET>

注意这里有三个jar需要引入,第一个包含了applet类,另外两个jar文件是JFreeChart和JCommon类库。我们需要在HTML文件中将applet标签引入。

18.3.2 源代码

实例applet的源代码见下(代码中我们使用了很少的applet特殊的代码,仅仅扩展了JApplet):

package demo;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.Timer;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.time.Millisecond;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
/**
* A simple applet demo.
*/
public class Applet1 extends JApplet {
    /** Time series for total memory used. */
    private TimeSeries total;
    /** Time series for free memory. */
    private TimeSeries free;
    /**
    * Creates a new instance.
    */
    public Applet1() {
        // create two series that automatically discard data more than
        // 30 seconds old...
        this.total = new TimeSeries("Total", Millisecond.class);
        this.total.setMaximumItemAge(30000);
        this.free = new TimeSeries("Free", Millisecond.class);
        this.free.setMaximumItemAge(30000);
        TimeSeriesCollection dataset = new TimeSeriesCollection();
        dataset.addSeries(total);
        dataset.addSeries(free);
        DateAxis domain = new DateAxis("Time");
        NumberAxis range = new NumberAxis("Memory");
        XYItemRenderer renderer = new XYLineAndShapeRenderer(true, false);
        XYPlot plot = new XYPlot(dataset, domain, range, renderer);
        plot.setBackgroundPaint(Color.lightGray);
        plot.setDomainGridlinePaint(Color.white);
        plot.setRangeGridlinePaint(Color.white);
        renderer.setSeriesPaint(0, Color.red);
        renderer.setSeriesPaint(1, Color.green);
        renderer.setSeriesStroke(0, new BasicStroke(1.5f));
        renderer.setSeriesStroke(1, new BasicStroke(1.5f));
        domain.setAutoRange(true);
        domain.setLowerMargin(0.0);
        domain.setUpperMargin(0.0);
        domain.setTickLabelsVisible(true);
        range.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        JFreeChart chart = new JFreeChart("Memory Usage",
        JFreeChart.DEFAULT_TITLE_FONT, plot, true);
        chart.setBackgroundPaint(Color.white);
        ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPopupMenu(null);
        getContentPane().add(chartPanel);
        new Applet1.DataGenerator().start();
    }
    /**
    * Adds an observation to the ’total memory’ time series.
    *
    * @param y
    * the total memory used.
    */
    private void addTotalObservation(double y) {
        total.add(new Millisecond(), y);
    }
    /**
    * Adds an observation to the ’free memory’ time series.
    *
    * @param y
    * the free memory.
    */
    private void addFreeObservation(double y) {
        free.add(new Millisecond(), y);
    }
    /**
    * The data generator.
    */
    class DataGenerator extends Timer implements ActionListener {
        /**
        * Constructor.
        */
        DataGenerator() {
            super(100, null);
            addActionListener(this);
        }
        /**
        * Adds a new free/total memory reading to the dataset.
        *
        * @param event
        * the action event.
        */
        public void actionPerformed(ActionEvent event) {
            long f = Runtime.getRuntime().freeMemory();
            long t = Runtime.getRuntime().totalMemory();
            addTotalObservation(t);
            addFreeObservation(f);
        }
    }
}