//=======================================================//
//JAVA-ARDUINO veri sistem entagrasyonu                                                       //
// analog yuvarlak çıktı göstergesi                                                                           //
// analogDialP                                                                                                         //
// M. Turhan Coban                                                                                                //
//=======================================================//
// Girdi Değişken listesi
// String name : gösterge ismi
// String unit : gösterge birim
// double minx: gösterge minimum rakam
// double maxx: gösterge maksimum rakam
// double minteta : gösterge minimum rakam açısı (radyan)
// double maxtetai: gösterge maksimum rakam açısı (radyan)
// int ni : rakamların da gösterildiği büyük skala bölüm sayısı
// int n1i: küçük skala bölüm sayısı
import javax.swing.*;
import java.awt.Graphics;
import java.awt.*;
 import java.awt.event.*;
 import java.awt.geom.*;
 import javax.swing.*;
 import java.net.*;
 import java.io.*;
 import java.awt.Graphics;
 import java.util.*;
 import java.text.*;
 import java.util.Locale;
 import java.awt.image.*;
 
 import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.RenderingHints;


import java.awt.geom.*;
import java.awt.font.*;

// ============
public class analogDialP extends JPanel
{ private static final long serialVersionUID = 756764376L;
  double x,minx,maxx;
  double minteta,maxteta;
  int n;
  int n1;
  polar p;
  double angle1,angle2,angle3;
  String name;
  String unit;
  public analogDialP(String namei,String uniti,double minxi,double maxxi,double mintetai,double maxtetai,int ni,int n1i)
  {
	  
   name=namei;
   unit=uniti;	  
   minx=minxi;
   maxx=maxxi;
   minteta=mintetai;
   maxteta=maxtetai;
   x=minx;
   n=ni;
   n1=n1i;
   angle1=minteta;
   angle2=maxteta;
   
  }
  
  public void setx(double xi)
  {x=xi;}
  
public  void drawPlotLabel(Graphics g2,String s, int xi,int yi,int R1i)
{Graphics2D g=(Graphics2D)g2;
 int x1,y1;
 int R1=R1i;
 x1=xi+R1;
 y1=yi+R1;
 g.drawString(s,x1,y1);
 //AffineTransform origTransform = g.getTransform();
 //g.translate(x1,y1);
 //g.rotate(90*Math.PI/180);
 //g.drawString(s,0,0);
 //g.setTransform(origTransform);
 //g.drawString("xxxxxxxxxxxxx",0,0);
}

public  void drawLabel1(Graphics g2,int xi,int yi,int R1i)
{
   double angle;
   Graphics2D g=(Graphics2D)g2;
   double R1=R1i;
   polar P1=new polar();
   int x[]=new int[n];
   int y[]=new int[n];
   GeneralPath polygon=new GeneralPath(GeneralPath.WIND_EVEN_ODD,
   x.length);
   int x1,y1;
   double teta=(maxteta-minteta)/n;
   double dx=(maxx-minx)/n;

   for(double xx=minx;xx<=maxx;xx+=dx)
   { angle=(xx-minx)/(maxx-minx)*(maxteta-minteta)+minteta;
     double teta1=-angle+Math.PI*3.0/2.0;
     //System.out.println("xx="+xx+"angle="+angle+"teta1="+teta1);
     P1.polar_input(R1+20,teta1);
     x1=(xi+(int)P1.xi());
     y1=(yi+(int)P1.xj());
     
     
     if(teta1>Math.PI/2.0 && teta1<3.0*Math.PI/2.0)
     {  x1=(xi+(int)P1.xi())-40;
     }
     else
     {
        x1=(xi+(int)P1.xi());
     }
     if(teta1>Math.PI && teta1<2.0*Math.PI)
     { y1=(yi-(int)P1.xj())+10;}
     else
       { y1=(yi-(int)P1.xj());}
     
    g.setFont(new Font("Serif",Font.BOLD,18));
    g.drawString(toString(xx,6,3),x1,y1);
   }

}
public  void drawLabel(Graphics g2,int xi,int yi,int R1i)
{
   double angle;
   Graphics2D g=(Graphics2D)g2;
   double R1=R1i;
   polar P1=new polar();
   polar P2=new polar();
   int x[]=new int[n];
   int y[]=new int[n];
   int x1,y1;
   double teta=(maxteta-minteta)/n;
   double teta2;
   double dx=(maxx-minx)/n;
   g.setFont(new Font("Serif",Font.BOLD,18));
   AffineTransform at;
   AffineTransform origTransform = g.getTransform();
   for(double xx=minx;xx<=maxx;xx+=dx)
   { angle=(xx-minx)/(maxx-minx)*(maxteta-minteta)+minteta;
     double teta1=-angle+Math.PI*3.0/2.0;
     teta2=angle+Math.PI/2.0-Math.PI/40.0;
     // dik yazı istersek
     // teta2=angle+Math.PI/2.0+Math.PI/80.0;
     P1.polar_input(R1,teta2);
     x1=(xi+(int)P1.xi());
     y1=(yi+(int)P1.xj());
     g.translate(x1,y1);
     g.rotate(teta2+Math.PI/2.0);
     // dik yazı istersek
     // g.rotate(teta2);
     g.drawString(toString(xx,4,1),0,0);
     g.setTransform(origTransform);
     //TextLayout t=new TextLayout(toString(xx,5,1),g.getFont(),g.getFontRenderContext());
     //at=AffineTransform.getRotateInstance(teta1,x1,y1);
     //g.setTransform(at);
     //t.draw(g,x1,y1);
     //at=AffineTransform.getRotateInstance(0,0,0);
     //g.setTransform(at);
     //g.translate(x1,y1);
     //g.rotate(angle);
     //t.draw(g,x1,y1);
     //g.setTransform(origTransform);
     //g.drawText(toString(xx,5,1));
     /*
     if(teta1>Math.PI/2.0 && teta1<3.0*Math.PI/2.0)
     {  x1=(xi+(int)P1.xi())-40;
     }
     else
     {
        x1=(xi+(int)P1.xi());
     }
     if(teta1>Math.PI && teta1<2.0*Math.PI)
     { y1=(yi-(int)P1.xj())+10;}
     else
       { y1=(yi-(int)P1.xj());}
     */

     //g.drawString(toString(xx,5,1),x1,y1);
   }

} //drawAkrep metodu sonu
  
public static String toString(double left, int w, int d)
// converts a double to a string with given width and decimals.
{
	NumberFormat df=NumberFormat.getInstance(Locale.US);
	df.setMaximumFractionDigits(d);
    df.setMinimumFractionDigits(d);
    df.setGroupingUsed(false);
    String s = df.format(left);
    while (s.length() < w)
      s = " " + s;
    if (s.length() > w)
    {
        s = "";
        for (int i=0; i<w; i++)
          s = s + "-";
    }
    return s;
}    
public  void drawLine(Graphics g2,int xi,int yi,int R1i,int R2i,int ni)
{
   double angle;
   Graphics2D g=(Graphics2D)g2;
   double R1=R1i;
   double R2=R2i;
   polar P1=new polar();
   polar P2=new polar();
   int x[]=new int[ni];
   int y[]=new int[ni];
   GeneralPath polygon=new GeneralPath(GeneralPath.WIND_EVEN_ODD,
   x.length);
   double teta=(maxteta-minteta)/ni;
   double dx=(maxx-minx)/ni;
   for(double xx=minx;xx<=maxx;xx+=dx)
   { angle=(xx-minx)/(maxx-minx)*(maxteta-minteta)+minteta;
     double teta1=-angle+Math.PI*3.0/2.0;
     P1.polar_input(R1,teta1);
     P2.polar_input(R2,teta1);
     polygon.moveTo((xi+(int)P1.xi()),(yi-(int)P1.xj()));
     polygon.lineTo((xi+(int)P2.xi()),(yi-(int)P2.xj()));
   }
   g.draw(polygon);
} //drawAkrep metodu sonu 


    
public  void drawAkrep(Graphics g2,int xi,int yi,int Ri)
{
   double angle=(x-minx)/(maxx-minx)*(maxteta-minteta)+minteta;
   Graphics2D g=(Graphics2D)g2;
   double R=Ri;
   double r=Ri*0.3;
   polar P1=new polar();
   polar P2=new polar();
   polar P3=new polar();
   int x[]=new int[5];
   int y[]=new int[5];
   GeneralPath polygon=new GeneralPath(GeneralPath.WIND_EVEN_ODD,
   x.length);
   x[0]=xi;
   y[0]=yi;
   x[4]=xi;
   y[4]=yi;
   double teta=Math.PI/10.0;
     double teta1=-angle+Math.PI*3.0/2.0;
     double teta2=teta1+teta/2.0;
     double teta3=teta1-teta/2+Math.PI*2;
     P1.polar_input(R,teta1);
     P2.polar_input(r,teta2);
     P3.polar_input(r,teta3);
     x[1]=xi+(int)P3.xi();
     y[1]=yi-(int)P3.xj();
     x[2]=xi+(int)P1.xi();
     y[2]=yi-(int)P1.xj();
     x[3]=xi+(int)P2.xi();
     y[3]=yi-(int)P2.xj();
   for(int i=0;i<5;i++)
   {
          if(i==0)
          {
          polygon.moveTo(x[0],y[0]);
          }
          else
          {
           polygon.lineTo(x[i],y[i]);
          }
   }
   g.draw(polygon);

} //drawAkrep metodu sonu 
public  void fillAkrep(Graphics g2,int xi,int yi,int Ri)
{
   double angle=(x-minx)/(maxx-minx)*(maxteta-minteta)+minteta;
   Graphics2D g=(Graphics2D)g2;
   double R=Ri;
   double r=Ri*0.3;
   polar P1=new polar();
   polar P2=new polar();
   polar P3=new polar();
   int x[]=new int[5];
   int y[]=new int[5];
   GeneralPath polygon=new GeneralPath(GeneralPath.WIND_EVEN_ODD,
   x.length);
   x[0]=xi;
   y[0]=yi;
   x[4]=xi;
   y[4]=yi;
   double teta=Math.PI/10.0;
     double teta1=-angle+Math.PI*3.0/2.0;
     double teta2=teta1+teta/2.0;
     double teta3=teta1-teta/2+Math.PI*2;
     P1.polar_input(R,teta1);
     P2.polar_input(r,teta2);
     P3.polar_input(r,teta3);
     x[1]=xi+(int)P3.xi();
     y[1]=yi-(int)P3.xj();
     x[2]=xi+(int)P1.xi();
     y[2]=yi-(int)P1.xj();
     x[3]=xi+(int)P2.xi();
     y[3]=yi-(int)P2.xj();
   for(int i=0;i<5;i++)
   {
          if(i==0)
          {
          polygon.moveTo(x[0],y[0]);
          }
          else
          {
           polygon.lineTo(x[i],y[i]);
          }
   }
   g.fill(polygon);

} //drawAkrep metodu sonu 


public static void drawDaire(Graphics g2, int xi,int yi, int R)
{
   Graphics2D g=(Graphics2D)g2;
   g.draw(new Ellipse2D.Double((xi-R),(yi-R),2*R,2*R));
}

public static void fillDaire(Graphics g2, int xi,int yi, int R)
{
   Graphics2D g=(Graphics2D)g2;
   g.fill(new Ellipse2D.Double((xi-R),(yi-R),2*R,2*R));
}
     public TexturePaint getImageTexture(String imageFile,int xi,int yi, int R)
    {
    URL url = getClass().getResource(imageFile);
    Image img = getToolkit().getImage(url);
    try {
      MediaTracker tracker = new MediaTracker(this);
      tracker.addImage(img, 0);
      tracker.waitForID(0);
    } catch (Exception e) {}
    int width = 2*R;
    int height = 2*R;
    BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics g = buffImg.getGraphics();
    g.drawImage(img,-30,-20, this);
    Rectangle2D rect = new Rectangle((xi-R),(yi-R), width, height);
    return new TexturePaint(buffImg, rect);
    }
 
  public void paintComponent(Graphics g)
  {
  final Graphics2D g2 = (Graphics2D) g;
  g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
  RenderingHints.VALUE_ANTIALIAS_ON);  
  try{
	 final BasicStroke s1 = new BasicStroke(1);
	 final BasicStroke s2 = new BasicStroke (2);
     final BasicStroke s10 = new BasicStroke (10);
     FontMetrics metrics=g.getFontMetrics();
     //System.out.println(metrics.getAscent());
     //System.out.println(metrics.getDescent());
     //System.out.println(metrics.getHeight());
     int xi=150;
     int yi=120;
     int R1=85;
     int R2=80;
     int R3=75;
     g2.setColor(Color.yellow);     
     TexturePaint tp = getImageTexture("arduino.jpg",xi,yi,R1);
     g2.setPaint(tp);	
     star.fillDaire(g2,xi,yi,R1);
     g2.setColor(Color.red);
     fillAkrep(g2,xi,yi,R2);
     g2.setColor(Color.black);

     g2.setStroke (s2);
     g2.setPaint (Color.black);
     drawDaire(g2,xi,yi,R1);
     drawDaire(g2,xi,yi,R2);

     drawDaire(g2,xi,yi,5);
     drawAkrep(g2,xi,yi,R2);
     drawLine(g2,xi,yi,R1,R2,n);
     g2.setStroke (s1);
     drawDaire(g2,xi,yi,R3);
     drawLine(g2,xi,yi,R2,R3,n1);
     g2.setStroke (s2);
     drawLabel(g2,xi,yi,R1+10);
     drawPlotLabel(g2,name,0,yi+20,R1);
     drawPlotLabel(g2,""+x+" "+unit,0,yi+40,R1);
     }
     catch(NullPointerException e){}
  }
}