I have created three buttons which create different plots. e.g. If Noise is pressed Noise chart is displayed but if after this Noisy is pressed an exception is thrown. What changes are required so that all buttons can work without repeated execution. Here is the code.
package project;
/*
* @author Abdul Baseer
*/
public class Buttons extends JFrame{
protected JButton Noise, Noisy, Filtered,FFT;
public Buttons(){
super("Adaptive Filtering");
setLayout(new FlowLayout());
Noise=new JButton("Noise Data");
Noise.setBackground(Color.red);
add(Noise);
Noisy=new JButton("Noise+Data");
Noisy.setBackground(Color.yellow);
add(Noisy);
Filtered=new JButton("Filtered Data");
Filtered.setBackground(Color.green);
add(Filtered);
ButtonHandler handler=new ButtonHandler();
Noisy.addActionListener(handler);
Noise.addActionListener(handler);
Filtered.addActionListener(handler);
}
private class ButtonHandler implements ActionListener{
String t = JOptionPane.showInputDialog("Observation Time in seconds");
double T=Double.parseDouble(t);
@Override
public void actionPerformed(ActionEvent event){
try {
Capture c=new Capture();
byte[]b = c.Capture(T);
int n=b.length;
double[]X=new double[n/4];
double[]Y=new double[n/4];
for(int i=0;i<n/4;i++){
X[i]=(3*((b[i*4]&0xff)|(b[i*4+1]<<8))/32768.0f);
Y[i]=((b[i*4+2]&0xff)|(b[i*4+3]<<8))/32768.0f;
}
if(event.getSource()==Noise){
Plot px=new Plot("Noise","Samples","Amplitude",X);
FFT F=new FFT();
double[]XX=F.FFT(X);
Plot fxp=new Plot("Noise Spectrum","Samples","Magnitude|X[k|",XX);
}
if(event.getSource()==Noisy){
Plot py=new Plot("Noisy","Samples","Amplitude",Y);
FFT F=new FFT();
double[]YY=F.FFT(Y);
Plot fyp=new Plot("Noisy Spectrum","Samples","Magnitude|Y[k|",YY);
}
if(event.getSource()==Filtered){
String l = JOptionPane.showInputDialog("Filter Order");
int L=Integer.parseInt(l);
String u = JOptionPane.showInputDialog("Step Size");
double U=Double.parseDouble(u);
LMS LM=new LMS();
double[]E=LM.LMS(L,U,X, Y);
ComPlot pe=new ComPlot("",Y,E);
FFT F=new FFT();
double[]EE=F.FFT(E);
Plot fep=new Plot("Filtered Spectrum","Samples","Magnitude|E[k|",EE);
}
} catch (LineUnavailableException | HeadlessException | NumberFormatException ex) {
System.out.println("Unsupported");
}
}
}
}