import java.io.*;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.awt.geom.*;
import java.net.URL;
import java.awt.image.*;
import java.util.*;

public class  scanP extends JPanel implements MouseListener,MouseMotionListener,MouseWheelListener
{
  int    x1[],y1[];
  public JLabel jp;
  public double a[][];
  int n;
  boolean mousemoved;
  Rectangle2D l;
  Ellipse2D l1,l2;
  Color c;
  Image img;
  public PrintWriter fout;
  String dosya;
  //BufferedWriter fout;
  double xmin,xmax,ymin,ymax;
  int    xcmin,xcmax,ycmin,ycmax;
 
 public scanP(String p1, String idosya,double ixmin,double ixmax,double iymin,double iymax)
 {super();
     dosya=idosya;
     try {fout=new PrintWriter(new BufferedWriter(new FileWriter(dosya))); }
     catch(IOException e1) {System.err.println("girdi hatası.");} 
     catch(NumberFormatException e2){}
  URL url = getClass().getResource(p1);
  img = getToolkit().getImage(url);
  xmin=ixmin;
  xmax=ixmax;
  ymin=iymin;
  ymax=iymax;
  jp=new JLabel();
  xcmin=(int)xmin;
  ycmin=(int)ymin;
  xcmax=(int)xmax;
  ycmax=(int)ymax;
  x1=new int[1000];
  y1=new int[1000];
  a=new double[2][1000];
  n=-2;
  addMouseListener(this);
  addMouseMotionListener(this);
  addMouseWheelListener(this);
  mousemoved=false;
 }
 public void setMinMax(double ixmin,double ixmax,double iymin,double iymax)
 {  xmin=ixmin;
  xmax=ixmax;
  ymin=iymin;
  ymax=iymax;
 }
 public JLabel getLabel()
 {  return jp;
 }
 
 public void setDosyaismi(String Di)
 {  dosya=Di;
    try {fout=new PrintWriter(new BufferedWriter(new FileWriter(dosya))); }
     catch(IOException e1) {System.err.println("girdi hatası.");} 
     catch(NumberFormatException e2){}}
 
 public void setResimismi(String Ci)
 {  URL url = getClass().getResource(Ci);
    img = getToolkit().getImage(url);}
 
 public void setCizgi(int x1i,int y1i)
 {if(n==-2) {xcmin=x1i;ycmin=y1i;}
 else if(n==-1) {xcmax=x1i;ycmax=y1i;}
 else 
 {x1[n]=x1i;y1[n]=y1i;
  a[0][n]=xmin+(xmax-xmin)/(xcmax-xcmin)*(x1i-xcmin);
  a[1][n]=ymin+(ymax-ymin)/(ycmax-ycmin)*(y1i-ycmin); 
  System.out.println(a[0][n]+" "+a[1][n]);
  fout.println(a[0][n]+" "+a[1][n]);
 }}

 public void paintComponent(Graphics g)
 {
 //super.paintComponent(g);
 Graphics2D g2=(Graphics2D)g;	 
 g2.setFont(new Font("Serif",Font.BOLD,24));	 
 g2.setColor(Color.blue);
 g2.setStroke(new BasicStroke(2.0f));
 Dimension d=getSize();
 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
 int dx = d.width;
 int dy = d.height;
 g2.drawImage(img, 0, 0,dx,dy, this);
 if(n>=-1)
 {
 l1=new Ellipse2D.Double(xcmin-2,ycmin-2,5,5);
 l2=new Ellipse2D.Double(xcmax-2,ycmax-2,5,5);
 g2.draw(l1);
 g2.draw(l2);
 }
 for(int i=0;i<n;i++)
 {l=new Rectangle2D.Double(x1[i]-1,y1[i]-1,3,3);
 g2.draw(l);
 }	 
}
    
//MouseListener (fare dinleyicisi)
public void mouseClicked(MouseEvent e)
 {}
 
public void mouseclose()
{if(!mousemoved)
     {fout.close();
      Text.print(dosya,"digitize çıktı verisi : "+dosya+"dosyası ");
      mousemoved=true;
     }
}

 public void mousePressed(MouseEvent e)
 {  if(e.isMetaDown()) {mouseclose();} //sağ tuş basıldığında
    else if(e.isAltDown())             //orta tuş basıldığında
    {} 
    else { setCizgi(e.getX(),e.getY());n++;repaint();}  //sol tuş basıldığında
 }

  public void mouseReleased(MouseEvent e)
 { }

 public void mouseEntered(MouseEvent e)
 { }

 public void mouseExited(MouseEvent e)
 { }
//MouseMotionListener (fare hareket dinleyicisi) metotlari

 public void mouseDragged(MouseEvent e)
 {   }
 
 public void mouseMoved(MouseEvent e)
 { double x1,y1;
  x1=xmin+(xmax-xmin)/(xcmax-xcmin)*(e.getX()-xcmin);
  y1=ymin+(ymax-ymin)/(ycmax-ycmin)*(e.getY()-ycmin); 
  jp.setText("x = "+x1+"y = "+y1);
 } 
  
 public void mouseWheelMoved(MouseWheelEvent e)
 { }  
 
}





