package plotlib2;

import java.util.*;
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
import javax.imageio.*; // ImageIO;


public class GraphTest extends JPanel {

  private static final float XMIN = -0.5f, XMAX = 6f,
                             YMIN = -0.05f, YMAX = 0.45f;

  private PlottingContext iPC;
  private PlotList iList;
  private NormalDensity9 iDensity;
  private Test999 test999;
int width=400, height=400;
BufferedImage bi7 = new BufferedImage(width, 
  height, BufferedImage.TYPE_INT_RGB);





  public GraphTest() {
    iPC = new PlottingContext();
    iPC.setRange(new Rectangle2D.Float(XMIN,YMIN,XMAX-XMIN,YMAX-YMIN));
    iList = new PlotList();
    iDensity = new NormalDensity9();
    FunctionPlot p = new FunctionPlot(iDensity);
    test999 = new Test999();
    FunctionPlot p1 = new FunctionPlot(test999);
    p.setColor(Color.blue);
    p1.setColor(Color.red);
    iList.add(p);
    iList.add(p1);
    createAxis();
    createSpecials();
  }

  public static void main(String[] args) throws Exception {
    String[] methods = ImageIO.getReaderFormatNames();
    for (int n=0; n<methods.length; n++){
      System.out.println(methods[n]);
    } // Ende for (int n=0; n<methods.length; n++){
    File f = new File("C:\\Eigene Dateien\\qq.jpg");
    BufferedImage bi = ImageIO.read(f);
    File outfile = new File("C:\\Java2D\\qq_image_CC25.jpg");
    //ImageIO.write(bi, "png", outfile);
    ImageIO.write(bi, "jpg", outfile);
    //ImageIO.write(ImageIO.read(f), "png", outfile);

    JFrame frame1 = new JFrame("Graphen-Test");
    frame1.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
         System.exit(0);
      }});
    GraphTest t = new GraphTest();
    frame1.getContentPane().add(t);
    frame1.pack();
    frame1.show();

    frame1.setVisible(true);
    //BufferedImage im2 = (BufferedImage)t.createImage(500,500);
    BufferedImage im2 = (BufferedImage)frame1.createImage(500,500);
    //BufferedImage im2 = (BufferedImage)frame1.getContentPane().createImage(500,500);
    // BufferedImage bi2 = new BufferedImage(im2);
    //BufferedImage bi2 = frame1.createImage(500,500);
    //File outfile2 = new File("C:\\Java2D\\plotfile_CC25.png");
    //File outfile2 = new File("C:\\Java2D\\plotfile_CC25.jpg");
    //ImageIO.write(bi, "png", outfile);
    //ImageIO.write((OutputStream)os, "png", outfile2);
    //ImageIO.write(out, "png", outfile2);
    //ImageIO.write(bi2, "png", outfile2);
    //ImageIO.write(im2, "png", outfile2);
    //ImageIO.write(im2, "jpg", outfile2);


//    Image im1 = frame1.getIconImage();
  }

  private void createAxis() {
    Axis xAxis = new Axis(XMIN,0,XMAX,0);
    xAxis.setHead(Symbols.EARROW);

    TickMarks xTicks = new TickMarks();
    for (float x=XMIN; x<=XMAX; x+=0.1) {
      Point2D.Float p = new Point2D.Float(x,0);
      xTicks.add(p);
    }
    xTicks.setOrientation(TickMarks.VERTICAL);
    xAxis.setMinorTickMarks(xTicks);

    float xOffset = -0.025f;
    xTicks = new TickMarks();
    for (float x=(float) Math.ceil(XMIN); x<=Math.floor(XMAX); x+=1.0) {
      Point2D.Float p = new Point2D.Float(x,0);
      xTicks.add(p);
      xTicks.addLabel(new PLabel(String.valueOf(x),
                 new Point2D.Float(x,xOffset)));
    }
    xTicks.setOrientation(TickMarks.VERTICAL);
    xTicks.setSize(TickMarks.BIG);
    xAxis.setMajorTickMarks(xTicks);

    Axis yAxis = new Axis(0,0,0,YMAX);
    yAxis.setHead(Symbols.NARROW);
    yAxis.setLabel(new PLabel("f(x)",new Point2D.Float(0.04f,(float) (YMAX-0.04))));
    TickMarks yTicks = new TickMarks();
    for (float y=0; y<=YMAX; y+=0.1) {
      Point2D.Float p = new Point2D.Float(0,y);
      yTicks.add(p);
      yTicks.addLabel(new PLabel(String.valueOf(y),
                 new Point2D.Float(XMIN,y)));
    }
    yTicks.setOrientation(TickMarks.HORIZONTAL);
    yAxis.setMajorTickMarks(yTicks);

    iList.add(xAxis);
    iList.add(yAxis);
  }

  private void createSpecials() {
    Lines l = new Lines();
    float x = iDensity.MU-iDensity.SIGMA;
    Line2D.Float l2d = new Line2D.Float(x,0.0f,x,(float) iDensity.f(x).getY());
    l.add(l2d);
    x = iDensity.MU;
    l2d = new Line2D.Float(x,0.0f,x,(float) iDensity.f(x).getY());
    l.add(l2d);
    x = iDensity.MU+iDensity.SIGMA;
    l2d = new Line2D.Float(x,0.0f,x,(float) iDensity.f(x).getY());
    l.add(l2d);
    l.setType(Lines.DOT);
    iList.add(l);
  }

  public Dimension getPreferredSize() {
    return getMinimumSize();
  }

  public Dimension getMinimumSize() {
    return new Dimension((int) ((XMAX-XMIN)*50),(int) ((YMAX-YMIN)*500));
  }

  public void paint(Graphics g) {
    iPC.setSize(getSize());
    iPC.setG2((Graphics2D) g);
    iPC.setDefaults();
    iList.plot(iPC);

    //BufferedImage im2 = (BufferedImage)g.createImage(500,500);
    //BufferedImage im2 = (BufferedImage)t.createImage(500,500);
    //BufferedImage im2 = (BufferedImage)frame1.getContentPane().createImage(500,500);
    // BufferedImage bi2 = new BufferedImage(im2);
    //BufferedImage bi2 = frame1.createImage(500,500);
    //File outfile2 = new File("C:\\Eigene Dateien\\plotfile_CC25.png");
    //File outfile2 = new File("C:\\Eigene Dateien\\plotfile_CC25.jpg");
    //ImageIO.write(bi, "png", outfile);
    //ImageIO.write((OutputStream)os, "png", outfile2);
    //ImageIO.write(out, "png", outfile2);
    //ImageIO.write(bi2, "png", outfile2);
    //ImageIO.write(im2, "png", outfile2);
    //ImageIO.write(im2, "jpg", outfile2);
  }
}

class NormalDensity9 implements FunctionPlot.Xfy {
  public final float MU = 3, SIGMA=1;
  public Point2D f(double x) {
    Point2D.Float p = new Point2D.Float();
    p.x = (float) x;
    p.y = (float) (1/(SIGMA*Math.sqrt(2*Math.PI)) *
         Math.exp(-(x-MU)*(x-MU)/(2*SIGMA*SIGMA)));
    return p;
  }
}


class Test999 implements FunctionPlot.Xfy {
  public final float MU = 3, SIGMA=1;
  public Point2D f(double x) {
    Point2D.Float p = new Point2D.Float();
    p.x = (float) x;
    p.y = (float) (Math.pow(x,2)*0.01);
    return p;
  }
}


