Thursday, January 23, 2014

Image Gallery using JAVA and JAVAFX

          I quite browsed internet a lot to find something to create a image gallery in JAVA but i got null out of it.So i started making my own gallery in JAVA and JAVAFX and also integrated SQL database with it so that i can keep the information about the images and my gallery can be searchable according to persons in the image or place or date taken etc.Here below i am providing the codes to make the gallery..My gallery looks like this :
 
     So first of all create the following database in your local database.I suppose you are using My-SQL and phpmyadmin(comes in XAMPP) .After creating the DB now comes the coding..


  Code to add new folders to your database and view the images..

import javafx.scene.effect.*;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;

import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import com.oksbwn.ErrorHandling.handleExceptions;
public class AddNewGallery 
  {
    static Dimension gh=java.awt.Toolkit.getDefaultToolkit().getScreenSize();
    static int x=gh.width;
 static int y=gh.height;
    Random randomGenerator = new Random();
    static JFrame jFrame = new JFrame("- JFrame -");
   
    public static void main(String[] args)
     {
     new AddNewGallery();
     }

    public AddNewGallery()
      {
     jFrame.setLayout(null);
      jFrame.setUndecorated(true);
     jFrame.setBackground(new Color(Color.white.getRed(), Color.white.getGreen(),Color.white.getBlue(),50));
     jFrame.setType(javax.swing.JFrame.Type.UTILITY);
      jFrame.setBounds(0,0,x,y);
      final  JLabel cloesButton = new JLabel("X");
       cloesButton.setForeground(Color.red);
       cloesButton.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent arg0) {
         jFrame.dispose();
          }});
       cloesButton.setBounds(jFrame.getWidth()-30,5, 15,15);
     jFrame.getContentPane().add(cloesButton);
     
     final  JLabel AgainButton = new JLabel("A");
      AgainButton.setForeground(Color.green);
      AgainButton.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent arg0) {
         jFrame.dispose();
         new AddNewGallery();
          }});
      AgainButton.setBounds(jFrame.getWidth()-60,5, 15,15);
     jFrame.getContentPane().add(AgainButton);
      
      
      final JFXPanel jFXPanel3 = getPanel("");  
       jFrame.setVisible(true);
       //final String actPath="E://Image_Gallery#oksbwn//16.02.20111//";
      final String actPath=JOptionPane.showInputDialog(null, "Folder", "E://Image_Gallery#oksbwn//16.02.20111//");
       
    //File f = new File("C:\\Users\\oksbwn\\Pictures\\");
     File f = new File(actPath);
    final ArrayList names = new ArrayList(Arrays.asList(f.list()));
       Platform.setImplicitExit(false);
       Platform.runLater(new Runnable(){
            @Override
            public void run() {
               try {
     Class.forName("com.mysql.jdbc.Driver");
     Connection con1=DriverManager.getConnection("jdbc:mysql://localhost:3306/bikash","root","");
            initFxLater(jFXPanel3,"");
                 for(String file : names)
                 {
              initFxLater(getPanel(actPath+file),actPath+file);
              ResultSet res= con1.prepareStatement("SELECT * FROM `image_gallery` WHERE `Path` LIKE '"+actPath.substring(2, actPath.length())+file+"'").executeQuery();
                if(!res.next()) 
                 {
                 con1.prepareStatement("INSERT INTO `image_gallery` (`Sl_No`, `Image`, `Path`, `Persons`,`About`,`Date`,`Place`) VALUES (NULL, '"+file+"', '"+actPath.substring(2, actPath.length())+file+"','','','','')").execute();
                  } 
               }
                 con1.close();
                 } catch (Exception e) {new handleExceptions(e);}
              
                   }
        });
    }

    
    
    
    
    
    

//****************************************************************************************************************************//
private  void initFxLater(JFXPanel panel,String path)
{
  File file = new File(path);
  Image image = new Image(file.toURI().toString(),100,100,true,true);
  DropShadow ds1 = new DropShadow();
  ds1.setOffsetY(4.0f);
  ds1.setOffsetX(4.0f);
   
    final ImageView  chart = new ImageView();
    chart.setImage(image);
    chart.setEffect(ds1);
    Group root = new Group();
    Scene scene = new Scene(root);
    HBox box = new HBox();
       
    box.getChildren().add(chart);
    root.getChildren().add(box);
    scene.setFill(null);
    panel.setScene(scene);
}
private JFXPanel getPanel(String path)
{  
    final  JFXPanel jFXPanel = new JFXPanel();
 jFXPanel.setOpaque(false);
    jFXPanel.setBounds(randomGenerator.nextInt(x-120),randomGenerator.nextInt(y-120),110,110);
    jFXPanel .addMouseListener(setPos(jFXPanel,path));
    jFXPanel.addMouseMotionListener(setPos(jFXPanel,path));
    jFrame.getContentPane().add(jFXPanel);
 return jFXPanel;
 }
private MouseAdapter setPos(final JFXPanel comp,final String path){
final  MouseAdapter mouseListener2 = new MouseAdapter() {
    int x, y;
    @Override
    public void mousePressed(MouseEvent e) {
        if (e.getButton() == MouseEvent.BUTTON1) {
            x = e.getX();
            y = e.getY();
        }
    }
    @Override
    public void mouseClicked(MouseEvent e) {
      //  System.out.println(path)
     new FullImage(path);
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        if ((e.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK) != 0) {
         comp.setLocation(e.getXOnScreen() - x, e.getYOnScreen() - y);
        }
    }
  };
return mouseListener2;
}

}

    Now it will ah to ask for the path  to image folder so after providing the path and other details it will show you the images so are in thumbnails.To see the large image when clicked over the thumbnails i used the following code :

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;

import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.DropShadow;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import com.oksbwn.ErrorHandling.handleExceptions;
import com.oksbwn.popUp.popMe;
import com.oksbwn.voiceEnable.Voice;


public class FullImage {
     JFrame jFrame = new JFrame("- JFrame -");
String path2;

     final  JFXPanel jFXPanel = new JFXPanel();
  
     static Dimension gh=java.awt.Toolkit.getDefaultToolkit().getScreenSize();
     static int x=gh.width;
  static int y=gh.height;
    public FullImage(final String path)
      {

      path2=path;
     jFrame.setLayout(null);
      jFrame.setUndecorated(true);
     jFrame.setBackground(new Color(Color.white.getRed(), Color.white.getGreen(),Color.white.getBlue(),50));
     jFrame.setType(javax.swing.JFrame.Type.UTILITY);
      jFrame.setBounds(0,0,x,y );
      jFrame .addMouseListener(mouseListener2);
      jFrame.addMouseMotionListener(mouseListener2);
      
      final  JLabel cloesButton = new JLabel("CLOSE");
       cloesButton.setForeground(Color.green);
       cloesButton.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent arg0) {
         jFrame.dispose();
          }});
       cloesButton.setBounds(jFrame.getWidth()-40,5,35,25);
     jFrame.getContentPane().add(cloesButton);
     jFXPanel.setOpaque(false);
      jFXPanel.setBounds(0,0,x,y);

      jFXPanel .addMouseListener(mouseListener2);
      jFXPanel.addMouseMotionListener(mouseListener2);
      jFrame.getContentPane().add(jFXPanel);
      
       jFrame.setVisible(true);
      Platform.setImplicitExit(false);
       Platform.runLater(new Runnable(){
            @Override
            public void run() {
               initFxLater(jFXPanel,path);
              }
        });
    }

    
    
    
    
    
    

//****************************************************************************************************************************//
private  void initFxLater(JFXPanel panel,String path)
{
  File file = new File(path);
  Image image = new Image(file.toURI().toString(),x-10,y-10,true,true);
  DropShadow ds1 = new DropShadow();
  ds1.setOffsetY(4.0f);
  ds1.setOffsetX(4.0f);
   
    final ImageView  chart = new ImageView();
    chart.setImage(image);
    chart.setEffect(ds1);
    Group root = new Group();
    Scene scene = new Scene(root);
    HBox box = new HBox();
       
    box.getChildren().add(chart);
    root.getChildren().add(box);
    scene.setFill(null);
    panel.setScene(scene);
}
final  MouseAdapter mouseListener2 = new MouseAdapter() {
    int x, y;
    @Override
    public void mousePressed(MouseEvent e) {
        if (e.getButton() == MouseEvent.BUTTON1) {
            x = e.getX();
            y = e.getY();
        }
    }
    @Override
    public void mouseDragged(MouseEvent e) {
        if ((e.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK) != 0) {
         jFXPanel.setLocation(e.getXOnScreen() - x, e.getYOnScreen() - y);
        }
    }
    
   
    @Override
    public void mouseClicked(MouseEvent e) {
     try{
      Class.forName("com.mysql.jdbc.Driver");
      
   Connection con1=DriverManager.getConnection("jdbc:mysql://localhost:3306/bikash","root","");
         ResultSet res= con1.prepareStatement("SELECT * FROM `image_gallery` WHERE `Path` LIKE '"+path2.substring(2, path2.length())+"'").executeQuery();
          if(res.next())
           {
           new Voice("Sir this image is of   "+res.getString(4)+"at   "+res.getString(7)+"    in "+res.getString(6)+"    captured cooment is "+res.getString(5));
             new popMe("Image of "+res.getString(4)+"at "+res.getString(7)+" in "+res.getString(6)+" cooment: "+res.getString(5), "Image Gallery","ok",10,140);
           if(res.getString(5).isEmpty()){
     String Persons=  JOptionPane.showInputDialog("Persons in Photo:");
     //String About =JOptionPane.showInputDialog("");
     String About =JOptionPane.showInputDialog("About the Photo");
     String Date= JOptionPane.showInputDialog("Date:");
     String Place= JOptionPane.showInputDialog("Where:");
            con1.prepareStatement("UPDATE `image_gallery` SET `Persons`='"+Persons+"',`About`='"+About+"',`Date`='"+Date+"',`Place`='"+Place+"' WHERE `Path` LIKE '"+path2.substring(2, path2.length())+"'").executeUpdate();
                
              }
           } 
       con1.close();
       } catch (Exception e1) {new handleExceptions(e1);}}
  };
}
You would normall see some eroors as i have used some self defined functions...So just edit those and modify the codes according to the usage.... Hope it would help you........For queries leave a comment...

No comments:

Post a Comment