// This is the thing that makes the big plots on the left side.

import java.awt.*;

public class PlotSpecial extends Panel {
  private Plot dRhoPlot; public PlotPanel pp;
  public double deltarho, dmax, curvelocities[]={0,0,0,0,0,0,0,0,0,0};
  private int points[]={0,0,0,0,0,0,0,0,0,0};
  boolean curveExists[]={true,false,false,false,false,false,false,false,false,
			 false};

public void init() {
  setLayout(new BorderLayout());

  dRhoPlot=new Plot(); add("Center",dRhoPlot); dRhoPlot.init();
  dRhoPlot.setTitle("Velocity Contours"); dRhoPlot.setGrid(true);
  dRhoPlot.setNumSets(10); dRhoPlot.setMarksStyle("none");
  dRhoPlot.setXLabel("Sphere Density"); dRhoPlot.setYLabel("Diameter");

  dRhoPlot.addPoint(0,pp.rho,0,false); dRhoPlot.addPoint(0,pp.rho,dmax,true);
  /* drawPlot(1,pp.u); */ }

private void _drawPlot(int curvenum, double u) {
  if(curvenum<1||curvenum>9) return;
  if(curveExists[curvenum]) {                   // Erase old curve if it exists
    for(int i=0;i<points[curvenum];i++)
      dRhoPlot.erasePoint(curvenum,0);
    points[curvenum]=0; }

  curvelocities[curvenum]=u;                            // Draw new right curve
  double rhomin=pp.rho+u*u*u*pp.rho*pp.rho/800000/pp.mu/pp.g;
  double rhomax=pp.rho+deltarho;
  if(rhomin-pp.rho<=deltarho) {
    if(rhomax-pp.rho>deltarho) rhomax=pp.rho+deltarho;
    boolean first=false; for(int i=0; i<=100; i++) {
      double newrho=rhomin+i*Math.abs(rhomax-rhomin)/100.;
      double Re=pp.dc.rerat(pp.dc.free(newrho,u));
      if(Re>0&&(Re*=pp.mu/pp.rho/u)<=dmax) {
	dRhoPlot.addPoint(curvenum,newrho,Re,first);
	points[curvenum]++; first=true; }}}

  rhomax=pp.rho-u*u*u*pp.rho*pp.rho/800000/pp.mu/pp.g;
  rhomin=Math.max(pp.rho-deltarho,0);
  if(pp.rho-rhomax<=deltarho && rhomax>0) {
    boolean first=false; for(int i=0; i<=100; i++) {     // Draw new left curve
      double newrho=rhomin+i*Math.abs(rhomax-rhomin)/100.;
      double Re=pp.dc.rerat(pp.dc.free(newrho,u));
      if(Re>0&&(Re*=pp.mu/pp.rho/u)<=dmax) {
	dRhoPlot.addPoint(curvenum,newrho,Re,first);
	points[curvenum]++; first=true; }}}

  dRhoPlot.setXRange(Math.max(pp.rho-deltarho,0), pp.rho+deltarho);
  dRhoPlot.setYRange(0,dmax);
  curveExists[curvenum]=true; }

public void drawPlot(int curvenum, double u) {
  _drawPlot(curvenum, u); dRhoPlot.drawPlot(true); }

public void redoAllPlots() {
  dRhoPlot.erasePoint(0,0); dRhoPlot.erasePoint(0,0);
  dRhoPlot.addPoint(0,pp.rho,0,false); dRhoPlot.addPoint(0,pp.rho,dmax,true);
  for(int i=1; i<10; i++)
    if(curveExists[i]) _drawPlot(i, curvelocities[i]);
  dRhoPlot.drawPlot(true); }

public void changedRho(double newdRho) {
  deltarho=newdRho; redoAllPlots(); }

public void changeDmax(double newDmax) {
  dmax=newDmax; redoAllPlots(); }

public PlotSpecial(PlotPanel fff) {
  pp=fff; deltarho=Math.abs(pp.rho_p-pp.rho);
  dmax=100000*pp.mu/pp.rho/pp.u; }

public void paint(Graphics g) { dRhoPlot.paint(g); }}
