

package DDrawing;

import java.awt.Color;
import java.util.Vector ;


public class DPeakSet extends DContainer{
  private double width;
  private Vector peaks;
  private Color fill_color = Color.lightGray;

  public DPeakSet( double width ){
    this( width, 5 );
  }

  public DPeakSet( double width, int initial_capacity ) {
    if( width <= 0 ) throw
      new IllegalArgumentException("The width of the peaks has to be a positive value");
    this.width = width;
    peaks = new Vector( initial_capacity );
  }

  public void setColor( Color color ){
    super.setColor( color );
    for( int i=0; i<peaks.size(); i++ ){
      DCountingPeak p = (DCountingPeak)peaks.get(i);
      p.setColor( color );
    }
  }

  public void setFillColor( Color fill_color ){
    this.fill_color = fill_color;
    for( int i=0; i<peaks.size(); i++ ){
      DCountingPeak p = (DCountingPeak)peaks.get(i);
      p.setFillColor( color );
    }
  }

  public Color getFillColor(){ return fill_color; }

  public void addValue( double v ){
    boolean found = false;
    for( int i=0; i<peaks.size() && !found; i++ ){
      DCountingPeak p = (DCountingPeak)peaks.get(i);
      if( p.addValue( v ) ) {
        found = true;
        if( p.height > rectangle.height ) {
          rectangle.height = p.height;
          repaint();
        }
      }
    }
    if( found ) return;
    double min = (int)( v / width );
    DCountingPeak p = new DCountingPeak( min, width );
    peaks.add( p );
    p.addValue();
    addDElement( p );
  }

  public void paint( DMeasures m ){
    for( int i=0; i<peaks.size(); i++ ){
      DCountingPeak p = (DCountingPeak)peaks.get(i);
      p.paint( m );
    }
  }
}

/**
 * this class can be used as part of a peak diagramm
 */
class DCountingPeak extends DRectangle{

  public DCountingPeak( double minx, double width ) {
    super( minx, 0, width, 0 );
    color = Color.gray;
    fillColor = Color.lightGray;
  }

  public boolean addValue(){ height += 1; repaint(); return true; }

  public boolean addValue( double v ){
    if( v >= x && v < x + width ){
      height += 1;
      repaint();
      return true;
    }
    return false;
  }

  public void reset(){
    if( height == 0 ) return;
    height = 0;
    repaint();
  }
}