2011-07-24 76 views
0

我從DynamicTimeSeriesCollection顯示的JFreeCharts獲取了源代碼。圖形Applet未在Web瀏覽器上運行

它運行良好,當我作爲一個簡單的Java應用程序運行它,但然後我想它作爲小程序。

所以我做了applet代碼,並插入所有代碼的start(),塗料()和init()函數。

這運行良好,當我做「運行文件」,但是當我嘗試在一個網頁瀏覽器打開生成的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; 
    } 
} 
+0

爲了更快地獲得更好的幫助,請發佈[SSCCE](http://pscode.org/sscce.html)。 –

回答

0

它可能看起來像我重複自己(如果你看看其他的答案我的),但是:切勿忽略Swing中的paint()方法!

如果你真的需要畫的東西,覆蓋paintComponent()。但就你而言,你應該簡單地將JFreeChart的一個組件放在applet中(你在構造函數中做的),然後完成。 (只是爲了完整性:所有GUI相關的東西都應該在AWT事件調度線程中完成,就像使用EventQueue.invokeLater(或SwingUtilities.invokeLater))。Applet的構造函數將不會在此線程中調用,這意味着您不應該在這個線程中調用它,也就是說,不應該在構造函數中執行GUI初始化,最好把它放在從init方法調用的invokeLater中。)

相關問題