package org.cemte.jacek.mpo;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JWindow;

public class MPODemo extends JWindow{
    
        float [] filterLowPass ={(float)1/9,(float)1/9,(float)1/9,
                                 (float)1/9,(float)1/9,(float)1/9,
                                 (float)1/9,(float)1/9,(float)1/9}; 
        
        float [] filterHighPass={(float)-1,(float)-1,(float)-1,
                                 (float)-1,(float)9,(float)-1,
                                 (float)-1,(float)-1,(float)-1}; 
        
        float [] filterEdges={(float)-1,(float)0,(float)1,
                              (float)-1,(float)0,(float)1,
                              (float)-1,(float)0,(float)1}; 
	
	private ImagePanel ip = new ImagePanel();
        

	
	public void buildGUI(){
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
                this.setSize(screenSize.width,screenSize.height-15);
                this.setLayout(new BorderLayout());
	  
		ip.createAndDrawImage();
		JPanel buttons = new JPanel();
		
		buttons.setBackground(Color.black);
		this.add(ip, BorderLayout.CENTER);
		
		//Dodaj przycisk generacji obrazu
		JButton jbGen= new JButton("GENERUJ");
		jbGen.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae){
				ip.createAndDrawImage();
                                //ImageFactory.testRobot();
			}
		});
		buttons.add(jbGen);
		
		//Dodaj przycisk filtracji DP
		JButton jbFDP= new JButton("FILTR DP");
		jbFDP.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae){
				if(ip.iOps.getBImage()!=null){
					float kernel[]=filterLowPass; 
					ip.iOps.filter(kernel);
					ip.repaint();
				}
			}
		});
		buttons.add(jbFDP);
		
//		Dodaj przycisk filtracji GP
		JButton jbFGP= new JButton("FILTR GP");
		jbFGP.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae){
				if(ip.iOps.getBImage()!=null){
					float kernel[]=filterHighPass; 
					ip.iOps.filter(kernel);
					ip.repaint();
				}
			}
		});
		buttons.add(jbFGP);

//		Dodaj przycisk filtracji kraw�dziowej
		JButton jbFKR= new JButton("FILTR KRAW.");
		jbFKR.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae){
				if(ip.iOps.getBImage()!=null){
					float kernel[]=filterEdges;
					ip.iOps.filter(kernel);
					ip.repaint();
				}

			}
		});
		buttons.add(jbFKR);
		
//		Dodaj przycisk przeksztace� geometrycznych
		JButton jbROT= new JButton("GEOM");
		jbROT.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae){
				if(ip.iOps.getBImage()!=null){
					ip.iOps.geomTransform();
					ip.repaint();
				}
			}
		});
		buttons.add(jbROT);
		
//		Dodaj przycisk odbicia
		JButton jbREF= new JButton("ODBICIE");
		jbREF.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae){
				if(ip.iOps.getBImage()!=null){
					ip.iOps.reflect();
					ip.repaint();
				}

			}
		});
		buttons.add(jbREF);
                
                //Dodaj przycisk odbicia
		JButton jbZOOM= new JButton("POW");
		jbZOOM.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae){
				if((ip.iOps.getBImage())!=null){
                                        //System.out.println("ZOOM");
					ip.iOps.scale(2);
					ip.repaint();
				}

			}
		});
		buttons.add(jbZOOM);
                
                JButton jbZOOMLESS= new JButton("ZMN");
		jbZOOMLESS.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae){
				if((ip.iOps.getBImage())!=null){
                                        //System.out.println("ZOOM");
					ip.iOps.scale(0.5);
					ip.repaint();
				}

			}
		});
		buttons.add(jbZOOMLESS);

//		Dodaj przycisk zapisu obrazu
		JButton jbRJPEG= new JButton("CZYTAJ OBRAZ");
		jbRJPEG.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae){
					//ip.iOps.readBImage("/Users/jwr/NetBeansProjects/MPO/lecture.jpg");
					//ip.iOps.readBImage("C:\\Test.jpg");
                                        ip.iOps.readBImage("/Users/jwr/Desktop/lips_tests/usta2_n.jpg");
					ip.repaint();
			}
		});
		buttons.add(jbRJPEG);
		/*
//		Dodaj przycisk zapisu obrazu
		JButton jbCapture= new JButton("Z KAMERY");
		jbCapture.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae){
					ip.iOps.setImage((new ImageFactory()).getCapture());
					ip.iOps.getBI(ip.iOps.width,ip.iOps.height);
					ip.repaint();
			}
		});
		buttons.add(jbCapture);
*/
//		Dodaj przycisk zapisu obrazu
/*		JButton jbScreenCapture= new JButton("SCREEN CAPTURE");
		jbScreenCapture.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae){
				try{
					ip.iOps.setBImage((new ImageFactory()).getScreenCapture());
				} catch (Exception e){ e.printStackTrace();}
				ip.repaint();
				
			}
		});
		buttons.add(jbScreenCapture);
*/		
//		Dodaj przycisk zapisu obrazu
		JButton jbSJPEG= new JButton("ZAPISZ JPEG");
		jbSJPEG.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae){
				if(ip.iOps.getBImage()!=null){
					//ip.iOps.writeBImage("/Users/jwr/NetBeansProjects/MPO/Test.jpg", "jpeg");
					//ip.iOps.writeBImage("C:\\Test.png", "png");
                                        ip.iOps.writeBImage("/Users/jwr/Desktop/Test.jpg", "jpeg");
				}

			}
		});
		buttons.add(jbSJPEG);
		
		//Dodaj konwersję obrazu
		JButton jbSCS= new JButton("KONWERTUJ");
		jbSCS.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae){
				if(ip.iOps.getBImage()!=null){
                                      
                                        Image img=ImageFactory.create1CImage(ip.iOps.width, ip.iOps.height, ip.iOps.convertCS(4, 0));
                                        ip.iOps.setImage(img);
                                        ip.iOps.img2bi();
                                        ip.repaint();
					//ip.iOps.writeBImage("/Users/jwr/NetBeansProjects/MPO/Test.jpg", "jpeg");
					//ip.iOps.writeBImage("C:\\Test.png", "png");
				}

			}
		});
		buttons.add(jbSCS);
                
                JButton jbEND= new JButton("KONIEC");
		jbEND.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae){
				System.exit(1);

			}
		});
		buttons.add(jbEND);
		
		this.add(buttons, BorderLayout.SOUTH);
		
		this.setVisible(true);
	}//buildGUI()
    
    public static void main (String a[]){
		MPODemo view = new MPODemo();
		view.buildGUI();
	}//main
}//end class


