Ik wil graag een applicatie starten die ik gevonden heb via het irnet. Maar als ik dit met Jcreator run krijg ik alleen het bericht "Found port: COM4" en geen JFrame of grafiek in beeld. Kan iemand me hiermee helpen?
Code:
import org.jfree.chart.JFreeChart;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.ChartFactory;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.chart.plot.PlotOrientation;
import java.io.IOException;
import java.io.File;
import java.io.*;
import java.util.*;
import javax.comm.*;
import java.text.*;
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.event.*;
public class serialskin implements Runnable, SerialPortEventListener {
Vector<Double> raw = new Vector<Double>();
XYSeries data = new XYSeries("user");
static CommPortIdentifier portId;
static Enumeration portList;
InputStream inputStream;
SerialPort serialPort;
Thread readThread;
double minutes;
public static void main(String[] args) {
boolean portFound = false;
String defaultPort = "COM4";
double temp1;
if (args.length==0){temp1 = .5;}
else{temp1 = Integer.parseInt(args[0]);}
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals(defaultPort)) {
System.out.println("Found port: "+defaultPort);
portFound = true;
serialskin reader = new serialskin(temp1);
}
}
}
}
public serialskin(double a) {
minutes = a*60;
try {
serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
} catch (PortInUseException e) {}
try {
inputStream = serialPort.getInputStream();
} catch (IOException e) {}
try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {}
serialPort.notifyOnDataAvailable(true);
try {
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {}
readThread = new Thread(this);
readThread.start();
}
public void run() {
try {
Thread.sleep(20000);
} catch (InterruptedException e) {}
}
public void serialEvent(SerialPortEvent event) {
double time = 0;
int newData = 0;
double sampleTime = .125;
OutputStream os;
InputStream is;
try {
os = serialPort.getOutputStream();
is = serialPort.getInputStream();
} catch (IOException e) {
serialPort.close();
return;
}
StringBuffer inputBuffer = new StringBuffer();
switch (event.getEventType()) {
case SerialPortEvent.DATA_AVAILABLE:
while (newData != -1) {
try {
newData = is.read();
if (newData == 32){
try{
raw.add(Double.parseDouble(inputBuffer.toString()));
data.add(time,Double.parseDouble(inputBuffer.toString()));}
catch(NumberFormatException e){
break;
}
if ((int)(time+sampleTime)!=(int)time){
System.out.print((int)(time+sampleTime)+"\t");
if ((int)(time+sampleTime)%10==0)
System.out.println();
}
time+=sampleTime;
inputBuffer= new StringBuffer();
}
else{
inputBuffer.append((char) newData);
}
if ((int)time == (minutes)) {
serialPort.close();
System.out.println("Completed");
// ------- signal processing testing ------------------------------------------------------------
gui temp = new gui();
signal eve = new signal(raw,sampleTime);
eve.smooth();
temp.addData(eve.returnXY("smoothed data"));
eve.findAverage();
eve.forwardDiff();
eve.smooth();
temp.addData(eve.returnXY("smoothed slope"));
temp.addData(eve.returnEvent());
temp.updateChart(data);
// ------------------------------------------------------------------------------------------------------
break;}
} catch (IOException ex) {
System.err.println(ex);
return;}
}
}
}
}
class signal{
Vector<Double> data;
double mean;
double sampleTime;
public signal(Vector<Double> a, double b){
sampleTime = b;
data = a;}
public XYSeries returnXY(String name){
XYSeries temp = new XYSeries(name);
for (int i=0;i<data.size();i++)
temp.add(i*sampleTime,data.get(i));
return temp;
}
public void smooth(){
Vector<Double> temp = new Vector<Double>();
temp.add(data.get(0));
temp.add(data.get(1));
temp.add(data.get(2));
for(int i=3;i<(data.size()-3);i++){
temp.add(i,((.38039*(data.get(i)))+
(.239215*(data.get(i-1)+data.get(i+1)))+
(.062745*(data.get(i-2)+data.get(i+2)))+
(.007843*(data.get(i-3)+data.get(i+3)))));
}
temp.add(data.get(data.size()-3));
temp.add(data.get(data.size()-2));
temp.add(data.get(data.size()-1));
data = temp;
}
public void findAverage(){
double average=0;
for (int i=0;i<data.size();i++){
average+=data.get(i)/(data.size());
}
System.out.println("mean is: "+average);
mean = average;
}
public void forwardDiff(){
Vector<Double> temp=new Vector<Double>();
for(int i=0;i<(data.size()-1);i++){
temp.add((data.get(i+1)-data.get(i))*(1/sampleTime));}
temp.add(data.get(data.size()-1));
data= temp;
}
public XYSeries returnEvent(){
Vector<Double> temp = new Vector<Double>();
for (int i=0;i<data.size();i++){
if (data.get(i)>=30 && data.get(i)<=60)
temp.add(100.0);
else if (data.get(i)>60 && data.get(i)<=90)
temp.add(200.0);
else if (data.get(i)>90 && data.get(i)<=120)
temp.add(300.0);
else if (data.get(i)>120 && data.get(i)<150)
temp.add(400.0);
else if (data.get(i)>150)
temp.add(500.0);
else
temp.add(0.0);
}
eventPrintout(temp);
signal temp1 = new signal(temp,sampleTime);
return temp1.returnXY("event");
}
public void eventPrintout(Vector<Double> a){
int b,c,d,e,f;
b = 0;
c = 0;
d = 0;
e = 0;
f = 0;
double max;
int counter;
for (int i=0;i<a.size();i++){
max = 0;
counter=0;
if (a.get(i)>0){
max = a.get(i);
i++;
while (i<a.size() && a.get(i)>0){
if (a.get(i)>max) max=a.get(i);
counter++;
i++;
}
if (i<a.size()){ // && counter>(1/sampleTime)
if (max==100) f++;
else if (max==200) e++;
else if (max==300) d++;
else if (max==400) c++;
else if (max==500) b++;
}
}
}
System.out.println("A:"+b);
System.out.println("B:"+c);
System.out.println("C:"+d);
System.out.println("D:"+e);
System.out.println("E:"+f);
}
}
class gui extends JFrame{
JScrollPane graph;
XYSeriesCollection picture;
public gui(){
super("Skin Conductance");
picture = new XYSeriesCollection();
setDefaultLookAndFeelDecorated(true);
setPreferredSize(new Dimension(1280,1024));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(new BorderLayout());
graph = new JScrollPane();
getContentPane().add(graph,BorderLayout.CENTER);
setVisible(true);
pack();
}
public void addData(XYSeries x){
picture.addSeries(x);
}
public void updateChart(XYSeries x){
graph.setViewportView(createChart(x));
pack();
}
public JLabel createChart(XYSeries x){
String temp = JOptionPane.showInputDialog(this,"Enter name for run:");
JFreeChart chart = ChartFactory.createXYLineChart( // Generate the graph
"t vs. uS", // Title
"time (sec)", // x-axis Label
"skin conductance (uS x 100)", // y-axis Label
picture, // Dataset
PlotOrientation.VERTICAL, // Plot Orientation
true, // Show Legend
true, // Use tooltips
false // Configure chart to generate URLs?
);
try {
ChartUtilities.saveChartAsJPEG(new File("C:\\"+temp+".jpg"), chart, 1400,1050);
} catch (IOException e) {
System.err.println("Problem occurred creating chart.");
}
BufferedImage bimage = chart.createBufferedImage(1200,900);
ImageIcon a = new ImageIcon(bimage);
JLabel b = new JLabel(a);
return b;
}
}