 package DFT;
 
 import java.awt.Color;
 import java.awt.Dimension;
 import java.awt.Graphics;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.awt.event.MouseEvent;
 import java.awt.event.MouseListener;
 import java.awt.event.MouseMotionListener;
 import java.text.NumberFormat;
 import javax.swing.ButtonModel;
 import javax.swing.JButton;
 import javax.swing.JCheckBox;
 import javax.swing.JFormattedTextField;
 import javax.swing.JLabel;
 import javax.swing.JPanel;
 import javax.swing.JSeparator;
 import signals.Signals;
 
 public class Tools
   extends JPanel
   implements ActionListener, MouseListener, MouseMotionListener
 {
   private static final long serialVersionUID = 1L;
   private float[] samples;
   private float[] dft;
   private int Nsample = 150;
   private Signals signal;
   private Graph graph;
   private int width;
   private float offset = 0.0F;
   
   private boolean onSig = false;
   private int widthTmp;
   private boolean removeContinous = false;
   
 
   private JLabel samplingLabel = new JLabel("Sampling Rate: ");
   private JFormattedTextField samplingField = new JFormattedTextField(NumberFormat.getIntegerInstance());
   private JButton okSampleButton = new JButton("Ok");
   private JCheckBox continousPart = new JCheckBox("Remove continous part");
   
   public Tools(Signals s, int width, Graph graph) {
     this.signal = s;
     this.graph = graph;
     this.width = width;
     addMouseListener(this);
     addMouseMotionListener(this);
     
     this.samplingField.setPreferredSize(new Dimension(60, 20));
     this.samplingField.setValue(Integer.valueOf(this.Nsample));
     this.okSampleButton.setPreferredSize(new Dimension(50, 20));
     this.okSampleButton.addActionListener(this);
     this.okSampleButton.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent evt) {
         String tmp = Tools.this.samplingField.getText();
         tmp = tmp.replaceAll(",", "");
         Tools.this.Nsample = Integer.parseInt(tmp);
         Tools.this.genDFT();
         Tools.this.repaint();
       }
       
     });
     this.continousPart.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent evt) {
         Tools.this.removeContinous = Tools.this.continousPart.getModel().isSelected();
         Tools.this.genDFT();
         Tools.this.repaint();
       }
       
     });
     JSeparator sep = new JSeparator(1);
     sep.setPreferredSize(new Dimension(5, 20));
     
     add(this.samplingLabel);
     add(this.samplingField);
     add(this.okSampleButton);
     
     add(sep);
     
     add(this.continousPart);
     
     genDFT();
   }
   
   public void paintComponent(Graphics g)
   {
     g.setColor(new Color(220, 220, 220));
     g.fillRect(0, 0, getWidth(), getHeight());
     
 
     g.translate(0, 30);
     int sigHeight = Signals.heightUnit + Signals.Yspace + 10;
     
     g.setColor(Color.WHITE);
     g.fillRect(3, 5, getWidth() - 6, sigHeight - 5);
     g.setColor(Color.BLACK);
     g.drawRect(3, 5, getWidth() - 6, sigHeight - 5);
     
     if (this.onSig) {
       g.setColor(new Color(210, 210, 230));
       g.fillRect(Signals.leftSpace, Signals.Yspace - 8, this.widthTmp - Signals.leftSpace, Signals.heightUnit + 16);
     }
     
     this.signal.draw(g, null, 0, getWidth());
     
     g.setColor(Color.RED);
     for (int i = 0; i < this.Nsample; i++) {
       int x = Signals.leftSpace + i * (this.width - Signals.leftSpace) / this.Nsample;
       g.fillRect(x - 1, (int)(1.0F - this.samples[i]) * Signals.heightUnit + Signals.Yspace - 1, 3, 3);
     }
     g.translate(0, -30);
     
     this.graph.update(this.dft);
   }
   
   public void genDFT()
   {
     this.samples = new float[this.Nsample];
     
     float[] dftTmp = new float[this.Nsample];
     this.dft = new float[this.Nsample / 2];
     
 
 
     for (int i = 0; i < this.Nsample; i++) {
       int x = Signals.leftSpace + i * (this.width - Signals.leftSpace) / this.Nsample;
       this.samples[i] = (this.signal.wave[this.signal.getIndex(x)] + this.offset);
     }
     
     dftTmp = DftGen.from(this.samples);
     
     for (int i = 0; i < this.Nsample / 2; i++) { this.dft[i] = dftTmp[i];
     }
     
     if (this.removeContinous) { this.dft[0] = 0.0F;
     }
   }
   
 
 
  public void actionPerformed(ActionEvent e) {}
  

  public void mouseClicked(MouseEvent arg0)
  {
   // if ((this.onSig = this.widthTmp > Signals.leftSpace ? 1 : 0) != 0) 
 //{
    //  this.width = this.widthTmp;
    //  genDFT();
    //  repaint();
   // }
  }
  

  public void mouseEntered(MouseEvent e) {}
  

  public void mouseExited(MouseEvent arg0)
  {
    this.onSig = false;
    repaint();
  }
  



  public void mousePressed(MouseEvent arg0) {}
  



  public void mouseReleased(MouseEvent arg0) {}
  



  public void mouseDragged(MouseEvent arg0) {}
  



  public void mouseMoved(MouseEvent e)
  {
    if (e.getY() > 30) this.onSig = true; else
      this.onSig = false;
    this.widthTmp = ((e.getX() - Signals.leftSpace + this.signal.widthUnit / 2) / this.signal.widthUnit * this.signal.widthUnit + Signals.leftSpace);
    repaint();
  }
}


/* Location:              F:\JAVA\WORKSPACE\chronoEditor_v2.3.jar!\DFT\Tools.class
 * Java compiler version: 6 (50.0)
 * JD-Core Version:       0.7.1
 */