Calculate GeoDistance between two points using Java

This program help to determine Geo Distance between two points using java.
you have to pass longitude and latitude of two different points as arguments to function Distance

Java Program to calculate Geo Distance between two points :
package com.anuj.utils;

public class GeoDistance {

    private static Double geomile;
    private static Double geokil;
    private static Double NauMile;

    public static void main(String[] args) {

        geomile = distance(32.9697, -96.80322, 29.46786, -98.53506, "M");
        System.out.println(geomile + " Miles\n");
        geokil = distance(32.9697, -96.80322, 29.46786, -98.53506, "K");
        System.out.println(geokil + " Kilometers\n");
        NauMile = distance(32.9697, -96.80322, 29.46786, -98.53506, "N");
        System.out.println(NauMile + " Nautical Miles\n");

    }

    private static double distance(double lat1, double lon1, double lat2,
            double lon2, String unit) {
        double theta = lon1 - lon2;
        double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2))
                + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2))
                * Math.cos(deg2rad(theta));
        dist = Math.acos(dist);
        dist = rad2deg(dist);
        dist = dist * 60 * 1.1515;
        if (unit.equals("K")) {
            dist = dist * 1.609344;
        } else if (unit.equals("N")) {
            dist = dist * 0.8684;
        }
        return (dist);
    }

    // converts decimal degrees to radians
    private static double deg2rad(double deg) {
        return (deg * Math.PI / 180.0);
    }

    // converts radians to decimal degrees
    private static double rad2deg(double rad) {
        return (rad * 180.0 / Math.PI);
    }

}


How to capture screen shot of current window using Java

This example uses java.awt.Robot class to capture the screen pixels and returns a BufferedImage. Java.awt.Robot class is used to take the control of mouse and keyboard. Once you get the control, you can do any type of operation related to mouse and keyboard through your java code

Key points to be used :
  1. Create Object of Class Robot
  2. Decide size of window to be captured
  3. Call robot.createScreenCapture to capture image

Java Program to capture screenshot of current Window :

package com.anuj.basic;

import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;

public class ScreenShots {
    /**
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ScreenShots shots = new ScreenShots();
        try {
            shots.captureScreen("CapturedImage.png");
            System.out.println("Screen shots created successfully");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    // take screen shots of current window
    public void captureScreen(String fileName) throws Exception {

        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Rectangle screenRectangle = new Rectangle(screenSize);
        Robot robot = new Robot();
        BufferedImage image = robot.createScreenCapture(screenRectangle);
        ImageIO.write(image, "png", new File(fileName));

    }
}


Merge two PDF into one and Split one PDf into two PDF using java

In order to use this code you need open source lib iText.jar.you can download using link mentioned in my one of blog post "Generate PDf using java".

ConcatPDFs function helps you to merger two pdf say anuj1.pdf and anuj2.pdf into anujmerge.

Using function split pdf,you can split existing one pdf( say having 2 pages) into two seperate pdf say,output1.pdf(page 1 to 1) and output2.pdf(page 2 to 2).

Create PDF File using Java

iText is basically PDF library using which one can
  • Generate documents and reports based xml or database
  • One can add bookmarks, page numbers, watermarks, and other features in existing pdf documents
  • Create PDF document with pages
  • Split or merge pages
  • Serve PDF to browsers
There are other features provided by iText. Please refer to  iText Site for more details.

Please note that example mentioned here is only for learning purpose and You should refer to iText licensing before using.

Java program to create PDF using Java and iText follows as :