Tuesday, February 4, 2014

Show movie details using JAVA

   In this post i am going to post about using the anditson.com API to search for a movie online and display the results like a gallery as shown above using JAVA.anditson.com provides a good API which actually provides all data associated with a movie  in JSON or XML format.I have used the XML format.Here are the codes...

Code to search online:

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class movieDetails {
    String Year;
    String TORRENT;
    String Plot;
    String Language;
    String Awards;
    String Poster;
    String imdbRating;
    String tomatoConsensus;
    String BoxOffice;
    String Production;
    String Runtime;
    String Actors;
    String Director;
    String Released;
    String Rated;
    String Title;
    
    public  movieDetails(String movie)
    {
     String url = "http://anditson.com/?t="+movie+"&rt=xml&tomatoes=true&imdata=true&trailer=true&torrent=true&stream=true";  
     try
      {
       DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
       DocumentBuilder b = f.newDocumentBuilder();
       Document doc = b.parse(url);
       doc.getDocumentElement().normalize();
                // loop through each item
      NodeList items = doc.getElementsByTagName("movie_data");
      Element e = (Element)items.item(0);
    
      Title=e.getElementsByTagName("Title").item(0).getChildNodes().item(0).getNodeValue();
      Year=e.getElementsByTagName("Year").item(0).getChildNodes().item(0).getNodeValue();
      Rated=e.getElementsByTagName("Rated").item(0).getChildNodes().item(0).getNodeValue();
      Released=e.getElementsByTagName("Released").item(0).getChildNodes().item(0).getNodeValue();
      Director=e.getElementsByTagName("Director").item(0).getChildNodes().item(0).getNodeValue();
      Actors=e.getElementsByTagName("Actors").item(0).getChildNodes().item(0).getNodeValue();
      Plot=e.getElementsByTagName("Plot").item(0).getChildNodes().item(0).getNodeValue();
      Language=e.getElementsByTagName("Language").item(0).getChildNodes().item(0).getNodeValue();
      Awards=e.getElementsByTagName("Awards").item(0).getChildNodes().item(0).getNodeValue();
      Poster=e.getElementsByTagName("Poster").item(0).getChildNodes().item(0).getNodeValue();
      imdbRating=e.getElementsByTagName("imdbRating").item(0).getChildNodes().item(0).getNodeValue();
      tomatoConsensus=e.getElementsByTagName("tomatoConsensus").item(0).getChildNodes().item(0).getNodeValue();
      BoxOffice=e.getElementsByTagName("BoxOffice").item(0).getChildNodes().item(0).getNodeValue();
      Production=e.getElementsByTagName("Production").item(0).getChildNodes().item(0).getNodeValue();
      TORRENT=e.getElementsByTagName("TORRENT").item(0).getChildNodes().item(0).getNodeValue();
      Runtime=e.getElementsByTagName("Runtime").item(0).getChildNodes().item(0).getNodeValue();
          
      } catch (Exception e) {e.printStackTrace();}
    
    }
    public String getTitle(){return this.Title;}
    public String getYear(){return this.Year;}
    public String getedRat(){return this.Rated;}
    public String getReleased(){return this.Released;}
    public String getDirector(){return this.Director;}
    public String getActors(){return this.Actors;}
    public String getPlot(){return this.Plot;}
    public String getLanguage(){return this.Language;}
    public String getAwards(){return this.Awards;}
    public String getPoster(){return this.Poster;}
    public String getImdbRating(){return this.imdbRating;}
    public String getTomatoCons(){return this.tomatoConsensus;}
    public String getBox(){return this.BoxOffice;}
    public String getProduction(){return this.Production;}
    public String getTime(){return this.Runtime;}
    public String getTorrent(){return this.TORRENT;}
}


Code for the GUI:

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 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.text.Text;

import javax.swing.JFrame;
import javax.swing.JLabel;
public class newMessageBox 
  {
    static Dimension gh=java.awt.Toolkit.getDefaultToolkit().getScreenSize();
     JFrame jFrame = new JFrame();
    public newMessageBox(final String movie)
      {
        jFrame.setLayout(null);
         jFrame.setUndecorated(true);
        jFrame.setBackground(new Color(Color.white.getRed(), Color.white.getGreen(),Color.white.getBlue(),0));
        jFrame.setType(javax.swing.JFrame.Type.UTILITY);
      jFrame.setBounds(0,0,gh.width,gh.height);
      
      final JLabel cloesButton = new JLabel("X");
      cloesButton.setForeground(Color.white);
      cloesButton.setToolTipText("Close");
      cloesButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                 jFrame.dispose();
                 }
           @Override
           public void mouseEntered(MouseEvent arg0) {
                cloesButton.setForeground(Color.red);
               }
          @Override
           public void mouseExited(MouseEvent arg0) {
                cloesButton.setForeground(Color.white);
               }});
      cloesButton.setSize(15,15);
      cloesButton.setBounds(jFrame.getWidth()-17, 2, 15,15);
      jFrame.getContentPane().add(cloesButton);
    
      final JFXPanel jFXPanel3 = getPanel(0,0,400,300);  
      jFrame.setVisible(true);
      
      Platform.setImplicitExit(false);
      Platform.runLater(new Runnable(){
            @Override
            public void run() {

                movieDetails md= new movieDetails(movie);
                fxPanelPath(getPanel(410,5,gh.width-420,25),"C:\\Alberto\\Interface\\Twitter\\ed.jpg","Name :"+md.getTitle());
                fxPanelPath(getPanel(410,35,gh.width-420,25),"C:\\Alberto\\Interface\\Twitter\\ed.jpg","Released on :"+md.getReleased());
                fxPanelPath(getPanel(410,65,gh.width-420,25),"C:\\Alberto\\Interface\\Twitter\\ed.jpg","Cast :"+md.getActors());
                fxPanelPath(getPanel(410,95,gh.width-420,25),"C:\\Alberto\\Interface\\Twitter\\ed.jpg","Directed by :"+md.getDirector());
                fxPanelPath(getPanel(410,125,gh.width-420,25),"C:\\Alberto\\Interface\\Twitter\\ed.jpg","Awards :"+md.getAwards());
                fxPanelPath(getPanel(410,155,gh.width-420,25),"C:\\Alberto\\Interface\\Twitter\\ed.jpg","Lang :"+md.getLanguage());
                fxPanelPath(getPanel(410,185,gh.width-420,25),"C:\\Alberto\\Interface\\Twitter\\ed.jpg","Buesiness :"+md.getBox());
                fxPanelPath(getPanel(410,215,gh.width-420,25),"C:\\Alberto\\Interface\\Twitter\\ed.jpg","Rat. :"+md.getedRat());
                fxPanelPath(getPanel(410,245,gh.width-420,25),"C:\\Alberto\\Interface\\Twitter\\ed.jpg","Length :"+md.getTime());
                fxPanelPath(getPanel(410,275,gh.width-420,25),"C:\\Alberto\\Interface\\Twitter\\ed.jpg","IMDB Rat."+md.getImdbRating());
                fxPanelPath(getPanel(10,305,gh.width-20,25),"C:\\Alberto\\Interface\\Twitter\\ed.jpg","House :"+md.getProduction());
                fxPanelPath(getPanel(10,335,gh.width-20,25),"C:\\Alberto\\Interface\\Twitter\\ed.jpg","Story"+md.getPlot());
                fxPanelPath(getPanel(10,365,gh.width-20,25),"C:\\Alberto\\Interface\\Twitter\\ed.jpg","Plot :"+md.getTomatoCons());
                fxPanelPath(getPanel(10,395,gh.width-20,25),"C:\\Alberto\\Interface\\Twitter\\ed.jpg","Torrent :"+md.getTorrent());
                fxPanelURL(jFXPanel3,md.getPoster());       
        }});
    }

    
    
    
    
    
    
    
    
    
//****************************************************************************************************************************//
    private  void fxPanelURL(JFXPanel panel,String path)
    {
      Image image = new Image(path,400,300,true,true);
      DropShadow ds1 = new DropShadow();
      ds1.setOffsetY(4.0f);
      ds1.setOffsetX(4.0f);

      Group root = new Group();
      Scene scene = new Scene(root);
        final ImageView  chart = new ImageView();
        chart.setImage(image);
        chart.setEffect(ds1);
        
        root.getChildren().add(chart);
        
        scene.setFill(null);
        panel.setScene(scene);
    }
private  void fxPanelPath(JFXPanel panel,String path,String text)
{
  File file = new File(path);
  Image image = new Image(file.toURI().toString(),gh.width,100,false,true);
  DropShadow ds1 = new DropShadow();
  ds1.setOffsetY(4.0f);
  ds1.setOffsetX(4.0f);

  Group root = new Group();
  Scene scene = new Scene(root);
    final ImageView  chart = new ImageView();
    chart.setImage(image);
    chart.setEffect(ds1);
  
    Text tv=new Text();
    tv.setText(text);
    tv.setX(chart.getX()+26);
    tv.setY(chart.getY()+14);
    
    root.getChildren().add(chart);
    root.getChildren().add(tv);
    
    scene.setFill(null);
    panel.setScene(scene);
}
private JFXPanel getPanel(int xPos,int yPos,int width,int height)
{  
    final  JFXPanel jFXPanel = new JFXPanel();
    jFXPanel.setOpaque(false);
    jFXPanel.setBounds(xPos,yPos,width,height);
    jFXPanel .addMouseListener(setPos(jFXPanel));
    jFXPanel.addMouseMotionListener(setPos(jFXPanel));
    jFrame.getContentPane().add(jFXPanel);
    return jFXPanel;
    }
private    MouseAdapter setPos(final JFXPanel comp){
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) {
             }

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

}

Thanks....Have your feedback please....