/** This class will moving an image along a bezier curve that is created by the users 
 *  clicks on the applet. This project didnt finish before the deadline for the submition
 *  and there for containe a few bugs ..... but its working to show the main functionality
 */

import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.lang.Thread;
import javax.swing.*;
import java.awt.Toolkit;

public class ProgApplet extends JFrame 
{
 //the img location
 String img_loc = "x.gif"; 
 Image img = Toolkit.getDefaultToolkit().getImage( img_loc );
 
 //the sleeping time "the speed"
 int sleep_time = 50;
 
 //the buttons
 Button spd_change, img_change , reset, low_res, dithered, normal;
 //the text fields
 TextField spd_txt,img_txt;
 //the labels
 Label spdlb, imglb;
 
 Panel control = new Panel();
 
 //the clicks counter
 int num=0;
 
 //the control points array and the route array
 Point [] p= new Point[4];
 Point [] points= new Point[100];
 
 //options check
 boolean normal_op = true;
 boolean dithered_op = false;
 boolean low_res_op = false;
 
 
 //create the interface
 public ProgApplet()
 {
  //size of the window
     setSize( 620, 500 );
     
     getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT));
     //intializing
     spd_change = new Button("change");
     img_change = new Button("change");
     low_res = new Button("low Res");
     dithered = new Button("dithered");
     normal = new Button("Normal");
     reset = new Button("Restart");
     spdlb = new Label("Speed:");
     imglb = new Label("Image:");
     spd_txt = new TextField(sleep_time+"");
     img_txt = new TextField(img_loc);
     
     //adding the components
     control.add(spdlb);
     control.add(spd_txt);
     control.add(spd_change);
     
     control.add(imglb);
     control.add(img_txt);
     control.add(img_change);
     control.add(reset);
     
     //option buttons
     control.add(low_res);
     control.add(dithered);
     control.add(normal);
     getContentPane().add(control);
     
     //mouse action listener
        addMouseListener( new mouseListener() );
        show();
    }
    
    //speed of movement changing method
    public void setSpeed(int spd)
    {
     sleep_time = spd ;
    }
    
    //image changing method
    public void setImage(String imgstrg)
    {
     img_loc = imgstrg ;
    }
     
    //mouse clicks handler 
    private class mouseListener extends MouseAdapter
    {
        public void mouseClicked(MouseEvent e)
        {
         p[num]=new Point(e.getX(),e.getY());  
           System.out.println("num:"+num+"\tX is " + e.getX() + ", Y is " + e.getY() );
           num++;
             
           if( num == 4 ) 
            repaint(); 
        }
    }
    
    //button actions handling
    public boolean action(Event e, Object o)
    {
     if (e.target == reset)
     { 
    //start all over with no points 
    num = 0;
    return true;   
   } 
   else if(e.target == spd_change)
   {  
   //change speed
   setSpeed(Integer.parseInt(spd_txt.getText()));
   return true; 
  }
  else if(e.target == img_change)
  {
   //change image
   setImage(img_txt.getText());
   return true; 
  }
  else if(e.target == normal)
  {
   normal_op = true;
   dithered_op = false;
   low_res_op = false;
   return true; 
  }
  else if(e.target == dithered)
  {
   normal_op = false;
   dithered_op = true;
   low_res_op = false;
   return true; 
  }
  else if(e.target == low_res)
  {
   normal_op = false;
   dithered_op = false;
   low_res_op = true;
   return true; 
  }
  return true; 
  
 }
     
 //the painting method
   public  void paint(Graphics gx ){
      if ( num == 4 )
      {
      Graphics2D g2d = (Graphics2D) gx;
      double t,x,y;
      int i=0;
    
    for( t = 0; t < 1; t = t + 0.01 )
    {
     x = (Math.pow(1.0 - t,3)*p[0].getX() + (3.0*t *Math.pow(1.0 - t,2 ))*p[1].getX() + (3.0*t*t*(1.0 - t))*p[2].getX() +(t*t*t)*p[3].getX());
                y = (Math.pow(1.0 - t,3)*p[0].getY() + (3.0*t *Math.pow(1.0 - t,2 ))*p[1].getY() + (3.0*t*t*(1.0 - t))*p[2].getY() +(t*t*t)*p[3].getY());  
    points[i]= new Point((int)x,(int)y);
    System.out.println(x);
    if (dithered_op)
    {
     //add a method here that will dither the image before printing it
     //and change its level by the counter of the loop
    }
    
    else if(low_res_op)
    {
     //add a method here that will get low resolution of the image before printing it 
     //and change its level by the counter of the loop
    }
    g2d.drawImage(img,(int)x,(int)y,null);
      g2d.fillOval((int)x, (int)y, 3, 3);
   
    try
    { 
                 Thread.sleep(sleep_time);  
                }
                catch (InterruptedException e)
                { 
                    System.out.println("interrupted"); 
                } 
  
     g2d.clearRect(0,0,500,500);
             
    } 
       repaint();
      }
 }
    //the main method
 public static void main(String[] args) 
 {
  new ProgApplet();
 }
 
}