Thursday, January 30, 2014

A Tweet Gallery in JAVA

             
           As the title says the above image is the screenshot of a tweet gallery that i have designed using JAVA and JAVAFX., to access the tweets i have used twitter4j library.Below are the codes to design the gallery.Also My-SQL database is used to store the tweets periodically and when clicked on view then it shows the latest 25 tweets from the database.


Code to authenticate User:

package com.oksbwn.Y2014.twitter;

import twitter4j.Twitter;
import twitter4j.TwitterFactory;
import twitter4j.conf.ConfigurationBuilder;

public class getAuthorized {

 public  Twitter auth() {

     String consumerKey = "";
        String consumerSecret ="";
        String accessToken = "";
        String accessSecret = "";

        ConfigurationBuilder cb = new ConfigurationBuilder();
        cb.setDebugEnabled(true)
            .setOAuthConsumerKey(consumerKey)
            .setOAuthConsumerSecret(consumerSecret)
            .setOAuthAccessToken(accessToken)
            .setOAuthAccessTokenSecret(accessSecret);
        Twitter twitter = new TwitterFactory(cb.build()).getInstance(); 
return twitter;
 }

} 
Code to save tweets to database: 

package com.oksbwn.Y2014.twitter;
import java.net.URLEncoder;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.util.List;

import twitter4j.Status;
import twitter4j.Twitter;

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

public class TwitterDatabaseAccess {
public void update() throws Exception
   {
  Class.forName("com.mysql.jdbc.Driver");
  Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/alberto","root","");
     getAuthorized aut = new getAuthorized();
     Twitter twitter=aut.auth();
     twitter.verifyCredentials();
     
  String text;
  ResultSet result=null;
  float id;
     List statuses = twitter.getHomeTimeline();
     int i=0;
     for (Status status : statuses) 
        {
        text= status.getText();
        text=status.getUser().getScreenName()+":"+text;
        id=status.getId();
      try{
      result=con.prepareStatement("SELECT * FROM `twitter` WHERE `Sl_No` LIKE '%"+id+"%'").executeQuery();
      }catch(Exception e1){new handleExceptions(e1);}
     if(!result.next())
       {
       text= URLEncoder.encode(text,"UTF-8");
       text= URLEncoder.encode(text,"UTF-8");
       con.prepareStatement("INSERT INTO `twitter` (`No`,`Sl_No`, `Content`) VALUES (null,'"+id+"','"+text+"')").execute();
                 //  wait(4);
                i++;
       }}
     new popMe(i+"New Tweets","Twitter","ok",3,125);
     new Voice("Sir you have "+i+"new Tweets");
     con.close();
     
   }
}

Code to show tweets from database: 
package com.oksbwn.Y2014.twitter;
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.net.URLDecoder;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
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.text.Text;

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

import com.oksbwn.ErrorHandling.handleExceptions;
public class newMessageBox 
  {
    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();
    public void close()
      {
     jFrame.dispose();
      }
   
    public static void main(String[] args)
     {
     //new newMessageBox();
     TwitterDatabaseAccess td=new TwitterDatabaseAccess();
     try {
   td.update();
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
    // nm.close();
     }

    public newMessageBox()
      {
     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,x,y);
      
   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("");  
       jFrame.setVisible(true);
      
      Platform.setImplicitExit(false);
       Platform.runLater(new Runnable(){
            @Override
            public void run() {
               try {
     initFxLater(jFXPanel3,"","");
        String text;ResultSet result=null;
        Class.forName("com.mysql.jdbc.Driver");
        Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/alberto","root","");
                       result=con.prepareStatement("SELECT * FROM `twitter` ORDER BY `No`  DESC").executeQuery();
        
     String Otext;
     int size=0;
      int i;
      
                 for (i=0;result.next() && i<25 data-blogger-escaped--="" data-blogger-escaped-.addmouselistener="" data-blogger-escaped-0="" data-blogger-escaped-add="" data-blogger-escaped-anel="" data-blogger-escaped-catch="" data-blogger-escaped-chart.getx="" data-blogger-escaped-chart.gety="" data-blogger-escaped-chart.seteffect="" data-blogger-escaped-chart.setimage="" data-blogger-escaped-chart="" data-blogger-escaped-comp.setlocation="" data-blogger-escaped-comp="" data-blogger-escaped-con.close="" data-blogger-escaped-dropshadow="" data-blogger-escaped-ds1.setoffsetx="" data-blogger-escaped-ds1.setoffsety="" data-blogger-escaped-ds1="" data-blogger-escaped-e.getbutton="" data-blogger-escaped-e.getmodifiersex="" data-blogger-escaped-e.getxonscreen="" data-blogger-escaped-e.getyonscreen="" data-blogger-escaped-e="" data-blogger-escaped-ed.jpg="" data-blogger-escaped-f="" data-blogger-escaped-false="" data-blogger-escaped-file.touri="" data-blogger-escaped-file="" data-blogger-escaped-final="" data-blogger-escaped-getpanel="" data-blogger-escaped-group="" data-blogger-escaped-handleexceptions="" data-blogger-escaped-i="" data-blogger-escaped-if="" data-blogger-escaped-image="" data-blogger-escaped-imageview="" data-blogger-escaped-initfxlater="" data-blogger-escaped-int="" data-blogger-escaped-jframe.getcontentpane="" data-blogger-escaped-jfxpanel.addmousemotionlistener="" data-blogger-escaped-jfxpanel.setbounds="" data-blogger-escaped-jfxpanel.setopaque="" data-blogger-escaped-jfxpanel="" data-blogger-escaped-lberto="" data-blogger-escaped-mouseadapter="" data-blogger-escaped-mouseclicked="" data-blogger-escaped-mousedragged="" data-blogger-escaped-mouseevent.button1="" data-blogger-escaped-mouseevent.button1_down_mask="" data-blogger-escaped-mouselistener2="" data-blogger-escaped-mousepressed="" data-blogger-escaped-n="" data-blogger-escaped-new="" data-blogger-escaped-nterface="" data-blogger-escaped-null="" data-blogger-escaped-ontent="" data-blogger-escaped-otext="text.substring(0,size)+" data-blogger-escaped-ouseevent="" data-blogger-escaped-panel.setscene="" data-blogger-escaped-panel="" data-blogger-escaped-path="" data-blogger-escaped-pre="" data-blogger-escaped-private="" data-blogger-escaped-public="" data-blogger-escaped-randomgenerator.nextint="" data-blogger-escaped-return="" data-blogger-escaped-root.getchildren="" data-blogger-escaped-root="" data-blogger-escaped-scene.setfill="" data-blogger-escaped-scene="" data-blogger-escaped-setpos="" data-blogger-escaped-size="0;" data-blogger-escaped-string="" data-blogger-escaped-text.substring="" data-blogger-escaped-text="" data-blogger-escaped-tostring="" data-blogger-escaped-tring="" data-blogger-escaped-true="" data-blogger-escaped-tv.settext="" data-blogger-escaped-tv.setx="" data-blogger-escaped-tv.sety="" data-blogger-escaped-tv="" data-blogger-escaped-verride="" data-blogger-escaped-void="" data-blogger-escaped-witter="" data-blogger-escaped-x-250="" data-blogger-escaped-x="" data-blogger-escaped-xception="" data-blogger-escaped-y-120="" data-blogger-escaped-y="">
Thats all ........This is actually the raw code that i have used in my project so might face some odds...so leave a comment on that...i will reply you to fix it...Thanks....