图形Applet未在Web浏览器上运行

时间:2011-07-24 17:36:05

标签: applet browser

我从DynamicTimeSeriesCollection显示的JFreeCharts获得了源代码。

当我将它作为一个简单的Java应用程序运行时,它工作正常,但后来我想把它作为applet。

所以我制作了Applet代码,并在start(),paint()和init()函数中插入了所有代码。

当我执行“运行文件”时运行良好,但是当我尝试在Web浏览器上打开生成的HTML页面(启用Java SE 6)时,它会显示一个空白屏幕。我使用的是Netbeans 7.0。

代码如下,

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.JButton;
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.DynamicTimeSeriesCollection;
import org.jfree.data.time.Second;
import org.jfree.data.xy.XYDataset;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.Timer;

public class GraphTest extends JApplet {

    private static final String TITLE = "ECG of the Patient";
    private static final String START = "Start";
    private static final String STOP = "Stop";
    private static final float MINMAX = 100;
    private static final int COUNT = 2 * 60;
    private static final int FAST = 100;
    private static final int SLOW = FAST * 5;
    private Timer timer;
    // private Object random;
    //private static final Random random = new Random();
    ReadAFile file_reader = new ReadAFile();
    byte[] values = file_reader.Readfile();
    float[] temp = new float[120];

    public GraphTest() {
        //super(title);
        final DynamicTimeSeriesCollection dataset =
                new DynamicTimeSeriesCollection(1, COUNT, new Second());
        dataset.setTimeBase(new Second(0, 0, 0, 1, 1, 2011));

        dataset.addSeries(temp, 0, "ECG Plot");
        JFreeChart chart = createChart(dataset);

        final JButton run = new JButton(STOP);
        run.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                String cmd = e.getActionCommand();
                if (STOP.equals(cmd)) {
                    timer.stop();
                    run.setText(START);
                } else {
                    timer.start();
                    run.setText(STOP);
                }
            }
        });

        final JComboBox combo = new JComboBox();
        combo.addItem("Fast");
        combo.addItem("Slow");
        combo.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if ("Fast".equals(combo.getSelectedItem())) {
                    timer.setDelay(FAST);
                } else {
                    timer.setDelay(SLOW);
                }
            }
        });

        this.add(new ChartPanel(chart), BorderLayout.CENTER);
        JPanel btnPanel = new JPanel(new FlowLayout());
        btnPanel.add(run);
        btnPanel.add(combo);
        this.add(btnPanel, BorderLayout.SOUTH);


        timer = new Timer(FAST, new ActionListener() {

            float[] newData = new float[1];
            int i = 0;

            @Override
            public void actionPerformed(ActionEvent e) {
                newData[0] = values[i];
                dataset.advanceTime();
                dataset.appendData(newData);
                i++;

            }
        });
    }

    /**
     * Initialization method that will be called after the applet is loaded
     * into the browser.
     */
    @Override
    public void init() {
        EventQueue.invokeLater(new Runnable() {

            private String TITLE;
            //  this.setLayout(new FlowLayout());

            @Override
            public void run() {

                //DTSCTest demo = new DTSCTest(TITLE);
                // DTSCTest demo = new DTSCTest(TITLE);
                // pack();
                //RefineryUtilities.centerFrameOnScreen();
                //RefineryUtilities.centerFrameOnScreen(demo);
                setVisible(true);
                //demo.start();
            }
        });
        // TODO start asynchronous download of heavy resources
    }

    @Override
    public void paint(Graphics g) {
    }

    private JFreeChart createChart(final XYDataset dataset) {
        final JFreeChart result = ChartFactory.createTimeSeriesChart(
                TITLE, "hh:mm:ss", "Magnitude", dataset, true, true, false);
        final XYPlot plot = result.getXYPlot();
        ValueAxis domain = plot.getDomainAxis();
        domain.setAutoRange(true);
        ValueAxis range = plot.getRangeAxis();
        range.setRange(-MINMAX, MINMAX);
        return result;
    }

    @Override
    public void start() {
        timer.start();
    }
}

这是另一个由此类调用的java文件

import java.io.*;

public class ReadAFile {

    float[] datavalues = new float[300];

    /**
     * @param args the command line arguments
     */
    public byte[] Readfile() {
        StringBuilder contents = new StringBuilder();
        try {
            // use buffering, reading one line at a time
            // FileReader always assumes default encoding is OK!
            BufferedReader input = new BufferedReader(new FileReader("Desktop/Mytest.txt"));

            try {
                String line = null; // not declared within while loop
                /*
                 * readLine is a bit quirky : it returns the content of a line
                 * MINUS the newline. it returns null only for the END of the
                 * stream. it returns an empty String if two newlines appear in
                 * a row.
                 */
                while ((line = input.readLine()) != null) {
                    contents.append(line);
                }
            } finally {
                input.close();
            }
        } catch (IOException ex) { System.out.println("Exception coming ");
        }
        byte[] finalarray = new byte[contents.length()];
        for (int ch = 0; ch < contents.length(); ++ch) {
            char co = contents.charAt(ch);
            int j = (int) co;
            finalarray[ch] = (byte) j;

        }
        return finalarray;
    }
}

1 个答案:

答案 0 :(得分:0)

我可能看起来像是在重复自己(如果你看看我的其他答案),但是:永远不要覆盖Swing中的paint()方法!

如果你真的需要画一些东西,请覆盖paintComponent()。但在你的情况下,你应该简单地将一个JFreeChart的组件放在applet中(你在构造函数中做了什么),然后完成。

(只是为了完整性:所有GUI相关的东西都应该在AWT事件调度线程中完成,比如使用EventQueue.invokeLater(或者SwingUtilities.invokeLater)。在这个线程中不会调用Applet的构造函数,这意味着你不应该在构造函数中进行GUI初始化。最好将它放在从init方法调用的invokeLater中。)