1 Replies - 293 Views - Last Post: 28 June 2012 - 06:23 PM Rate Topic: -----

#1 #include(lou)  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 06-May 12

Passing data values from one class to another

Posted 28 June 2012 - 02:24 PM

Hi everyone I am trying to pass the data values from class Chart onto class NodeAvg. Any help appreciated.
package Testing123;

import java.awt.Color;
import java.awt.Paint;
import java.io.IOException;
import java.util.ArrayList;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.DrawingSupplier;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;

@SuppressWarnings("serial")
public class Chart extends ApplicationFrame {

  public Chart(XYSeriesCollection data) {
    super("EEG Scatterplot and Linear Regression");
    setContentPane(new ChartPanel(createChart(data)));
    pack();
  }

  public static JFreeChart createChart(XYSeriesCollection data) {
    JFreeChart chart = ChartFactory.createScatterPlot(null, "Packet Positions", "Value from Sensors", data,
    PlotOrientation.VERTICAL, true, false, false);
    chart.setBackgroundPaint(Color.LIGHT_GRAY); //BACKGROUND COLOR OF WINDOW
    XYPlot plot = (XYPlot) chart.getPlot();
    plot.setBackgroundPaint(Color.black);       //BACKGROUND COLOR OF PLOT
    XYItemRenderer scatterRenderer = plot.getRenderer();
    StandardXYItemRenderer regressionRenderer = new StandardXYItemRenderer();
    regressionRenderer.setBaseSeriesVisibleInLegend(false);
    plot.setDataset(1, regress(data));
    plot.setRenderer(1, regressionRenderer);
    DrawingSupplier ds = plot.getDrawingSupplier();
    for (int i = 0; i < data.getSeriesCount(); i++) {
      Paint paint = ds.getNextPaint();//picks the next color in the series for the plot 
      scatterRenderer.setSeriesPaint(i, paint);
      regressionRenderer.setSeriesPaint(i, paint);      
    }
//
//    try {
//        ChartUtilities.saveChartAsJPEG(new File("DataFiles/generatedImages/testing.jpg"), chart, 1000, 600);
//        System.out.println("Image saved successfully!");
//    } catch (Exception e){
//        System.out.println("Problem occured creating chart.");
//    }

   
    System.out.println(data);
    return chart;
  }

  //CALCULATE LINEAR REGRESSION
  public static XYDataset regress(XYSeriesCollection data) {
    // Determine bounds
    double xMin = Double.MAX_VALUE, xMax = 0;
    for (int i = 0; i < data.getSeriesCount(); i++) {
      XYSeries ser = data.getSeries(i);
      for (int j = 0; j < ser.getItemCount(); j++) {
        double x = ser.getX(j).doubleValue();
        if (x < xMin) {
          xMin = x;
        }
        if (x > xMax) {
          xMax = x;
        }
      }
    }
    // Create 2-point series for each of the original series
    XYSeriesCollection coll = new XYSeriesCollection();
    for (int i = 0; i < data.getSeriesCount(); i++) {
      XYSeries ser = data.getSeries(i);
      int n = ser.getItemCount();
      double sx = 0, sy = 0, sxx = 0, sxy = 0, syy = 0;
      for (int j = 0; j < n; j++) {
        double x = ser.getX(j).doubleValue();
        double y = ser.getY(j).doubleValue();
        sx += x;
        sy += y;
        sxx += x * x;
        sxy += x * y;
        syy += y * y;
      }
      double b = (n * sxy - sx * sy) / (n * sxx - sx * sx);
      double a = sy / n - b * sx / n;
      XYSeries regr = new XYSeries(ser.getKey());
      regr.add(xMin, a + b * xMin);
      regr.add(xMax, a + b * xMax);
      coll.addSeries(regr);
    }
    return coll;
  }
 
  public static XYSeriesCollection getNodeData(String node, int currentPacket, String fileName) throws IOException {
    
   ReadFile file1 = null;
    //cALLING A FILE CHOOSER SO WE GET A FAST FILE INTO THIS EXAMPLE
//    JFileChooser chooser = new JFileChooser();
//      chooser.showOpenDialog(null);
//    file1 = new ReadFile(chooser.getSelectedFile().getAbsolutePath());
   while(file1 == null)
      file1 = new ReadFile(fileName);
      for (int i = 1; i < currentPacket; i++)
        file1.getNextPacket();
   
   
    //CREATING A LOCAL COPY OF THE READ DATA FROM THE CHOOSER:
    double[] localData = new double[file1.getReadData(node).size()];
    
    for(int i = 0; i < localData.length; ++i)
      localData[i] = file1.getReadData(node).get(i).doubleValue();

    XYSeriesCollection data = new XYSeriesCollection();
    int arraySize = localData.length;
    
    for (int i = 1; i <= 1; i++) {
      XYSeries series = new XYSeries("Packet: " + currentPacket + " of Node: " + node);
      for (int j = 0; j <= (arraySize - 1); j++) {


        double x = j;
        double y = localData[j];

        series.add(x, y);
      }
      
      data.addSeries(series);
       System.out.println("the raw data should be: " + data);
    }
    for (int i = 1; i < currentPacket; i++)
        file1.getPreviousPacket();
    return data;
  }
  
  public static XYSeriesCollection getAllNodeData(int currentPacket, String fileName) throws IOException {
    
   ReadFile file1 = null;
    //cALLING A FILE CHOOSER SO WE GET A FAST FILE INTO THIS EXAMPLE
//    JFileChooser chooser = new JFileChooser();
//      chooser.showOpenDialog(null);
//    file1 = new ReadFile(chooser.getSelectedFile().getAbsolutePath());
   while(file1 == null)
      file1 = new ReadFile(fileName);
            
  for (int i = 1; i < currentPacket; i++)
        file1.getNextPacket();
      
        System.out.println("current packet from multichart should be " + currentPacket);
      
    String [] allNodes = new String[] {"AF3", "F7", "F3", "FC5", "T7", "P7", "O1", 
                          "O2", "P8", "T8", "FC6", "F4", "F8", "AF4"};
    //CREATING A LOCAL COPY OF THE READ DATA FROM THE CHOOSER:
    XYSeriesCollection data = new XYSeriesCollection();
   for (int p = 0; p < 13; p++)
   {
    double[] localData = new double[file1.getReadData(allNodes[p]).size()];
    
    for(int i = 0; i < localData.length; ++i)
      localData[i] = file1.getReadData(allNodes[p]).get(i).doubleValue();
    int arraySize = localData.length;
    
   
      XYSeries series = new XYSeries(allNodes[p]);
      for (int j = 0; j <= (arraySize - 1); j++) {


        double x = j;
        double y = localData[j];

        series.add(x, y);
      }
      
      
      data.addSeries(series);
   }
   for (int i = 1; i < currentPacket; i++)
        file1.getPreviousPacket();
    return data;
  }
  
public static XYSeriesCollection getFFTNodeData(String node, int currentPacket, String fileName) throws IOException {
    
   ReadFile file1 = null;
    
    
    //cALLING A FILE CHOOSER SO WE GET A FAST FILE INTO THIS EXAMPLE
//    JFileChooser chooser = new JFileChooser();
//      chooser.showOpenDialog(null);
//    file1 = new ReadFile(chooser.getSelectedFile().getAbsolutePath());
   while(file1 == null)
      file1 = new ReadFile(fileName);
         
      for (int i = 1; i <= currentPacket; i++)
        file1.getNextPacket();
   
   transformData = null;
   transformData = new FastFourierTransform(file1.getReadData(node));
   transformData.Transform();
   
      
   
    //CREATING A LOCAL COPY OF THE READ DATA FROM THE CHOOSER:
    double[] localData = new double[file1.getReadData(node).size()];
    temp = transformData.ReturnListArray();
    Double[] tempDoubleArray = (Double[])temp.toArray(new Double[temp.size()]);
   
      

    XYSeriesCollection data = new XYSeriesCollection();
    int arraySize = localData.length;
    
    for (int i = 1; i <= 1; i++) {
      XYSeries series = new XYSeries("Packet: " + currentPacket + " of Node: " + node);
      for (int j = 0; j <= (arraySize - 1); j++) {


        double x = j;
        double y = tempDoubleArray[j];

        series.add(x, y);
      }
      
      data.addSeries(series);
    }
    return data;
  }

public static XYSeriesCollection getFFTAllNodeData(int currentPacket, String fileName) throws IOException {
    
   ReadFile file1 = null;
    //cALLING A FILE CHOOSER SO WE GET A FAST FILE INTO THIS EXAMPLE
//    JFileChooser chooser = new JFileChooser();
//      chooser.showOpenDialog(null);
//    file1 = new ReadFile(chooser.getSelectedFile().getAbsolutePath());
   while(file1 == null)
      file1 = new ReadFile(fileName);
            
  
      
   
      for (int i = 1; i < currentPacket; i++)
        file1.getNextPacket();
      
      
      System.out.println("current packet from multichart should be " + currentPacket);
      
    String [] allNodes = new String[] {"AF3", "F7", "F3", "FC5", "T7", "P7", "O1", 
                          "O2", "P8", "T8", "FC6", "F4", "F8", "AF4"};
    //CREATING A LOCAL COPY OF THE READ DATA FROM THE CHOOSER:
    XYSeriesCollection data = new XYSeriesCollection();
   for (int p = 0; p < 13; p++)
   {
    transformData = null;
    transformData = new FastFourierTransform(file1.getReadData(allNodes[p]));
   transformData.Transform();
   
   
   
      
   
    //CREATING A LOCAL COPY OF THE READ DATA FROM THE CHOOSER:
    double[] localData = new double[file1.getReadData(allNodes[p]).size()];
    temp = transformData.ReturnListArray();
    Double[] tempDoubleArray = (Double[])temp.toArray(new Double[temp.size()]);
    
    for(int i = 0; i < localData.length; ++i)
      localData[i] = file1.getReadData(allNodes[p]).get(i).doubleValue();
    int arraySize = localData.length;
    
   
      XYSeries series = new XYSeries(allNodes[p]);
      for (int j = 0; j <= (arraySize - 2); j++) {


        double x = j;
        double y = tempDoubleArray[j];
        System.out.println(tempDoubleArray[j]+ "\n");

        series.add(x, y);
      }
      
      data.addSeries(series);
   }
   
    return data;
  }

private static FastFourierTransform transformData;
private static ArrayList<Double> temp;
private static BrainWavesAnalyserView dataFile;

//  public static void main(String[] args) throws Exception {
//    String node = "AF3";
//    LinearRegression plot = new LinearRegression(getTestData(node));
//    RefineryUtilities.centerFrameOnScreen(plot);
//    plot.setVisible(true);  }

  
}



--------------------------------------------------------------------------------------------------
NodeAvg class:
package Testing123;

import java.util.ArrayList;
import org.jfree.ui.ApplicationFrame;

public class NodeAvg extends ApplicationFrame{

  public static Average FindAvg (//How do I get the data values of the nodes?);

          private double data [];
  
NodeAvg(ArrayList<Double> incomingArray) {
    
data = new double[incomingArray.size()];

 for(int i = 0; i < data.length; ++i){
        data[i] = incomingArray.get(i).doubleValue();
        //Output test.
        System.out.println("Data is: " + data[i]);
   }
  }
}
//Average: Average by using view from Excel. Average readings with the same readings for each
//node, accross all paclets.

//Output to Chart:



Is This A Good Question/Topic? 0
  • +

Replies To: Passing data values from one class to another

#2 pbl  Icon User is online

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8017
  • View blog
  • Posts: 31,126
  • Joined: 06-March 08

Re: Passing data values from one class to another

Posted 28 June 2012 - 06:23 PM

Don't really see the relation between Chart (which is a GUI component) and NodeAvg (which is a mathematical concept)
How can both of them extend ApplicationFrame ?

It is like having both the class Kennel and the class Dog extending Vertebrate
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1