Tuesday, March 31, 2009

Dashed line drawing in java graphics.


Hello Friends,

 While working in my project i was searching how to draw a dashed line
in Graphics not in Graphics2D.

So i get this code and it is superb for drawing dash lines or rectangulars.
Just pass the line points and assign your dash length and space length.
See the below function........

////pass the line point x1,y1 and point 2 as x2 and y2
public void drawDashedLine(Graphics g, int x1, int y1, int x2,
                                                   int y2, double dash_length, double space_length)
{
          ///if both the points are same
          if ((x1 == x2) && (y1 == y2))
          {
                   g.drawLine(x1, y1, x2, y2);
                   return;
          }

          ///calculating line length
          double linelength = Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));

          double yincrement = (y2 - y1) / (linelength / (dash_length + space_length));
          double xincdashspace = (x2 - x1) / (linelength / (dash_length + space_length));
          double yincdashspace = (y2 - y1) / (linelength / (dash_length + space_length));
          double xincdash = (x2 - x1) / (linelength / (dash_length));
          double yincdash = (y2 - y1) / (linelength / (dash_length));
          int counter = 0;
          for (double i = 0; i < linelength - dash_length; i += dash_length + space_length)
          {
                   g.drawLine((int)(x1 + xincdashspace * counter),
                               (int)(y1 + yincdashspace * counter),
                               (int)(x1 + xincdashspace * counter + xincdash),
                                (int)(y1 + yincdashspace * counter + yincdash));
                   counter++;
          }

          if ((dashlength + spacelength) * counter <= linelength)
                   g.drawLine((int)(x1 + xincdashspace * counter),
                               (int)(y1 + yincdashspace * counter), x2, y2);
}
=============================================

Wednesday, March 18, 2009

Closing command prompt from JAVA

Hi,

Many times developer ask question how to close command prompt after
launching the application?
or
How to exit from batch file after execution.

Here is all possible ways to do so.
1. Use javaw.exe for launching your application and exit.
2. Create an executable jar file so that just double clicks for launch.
3. Create exe from jar files.

If you want to exit from command prompt after calling your exe file.
Then apply the same concept which we are using for tracing output
from exe.(see previous post)

Technique:-
1. You have to insert a line on your code which print "Loaded" on output.
2. Crate a jar file then exe from jar file.
3. Again make a java file for launching your exe file.
4. Get the InputStreamReader from your process.
5. Check for string "Loaded" on output.
6. If you get "Loaded" then exit.

See the full source code.

/* Program for closing a command prompt from a executable
* Copyright 2009 @ yuvadeveloper
* Code By:- Prashant Chandrakar*/

import java.io.*;
import javax.swing.JOptionPane;
import java.net.BindException;
class Loader
{
    public static void main(String as[])
    {
        try
        {
            System.out.println("Loading application.........");

             //pass the name of your exe in place of my.exe
             Process p = Runtime.getRuntime().exec("my.exe");

             //getting inputstream reader from process
             BufferedReader reader = new BufferedReader
                                     (newInputStreamReader(p.getInputStream()));

             String line = reader.readLine();

              //exit from this class when get "Loaded" from your application
              while (line.equals("Loaded") == false)
               {
                       line = reader.readLine();
               }
                reader.close();
            }
            catch (Exception e)
            {
            }
    }
}
call exe from a batch file like...
java Loader

Tracing output from Exe in JAVA

Hello Everyone,

Suppose you are creating a exe files from you jar files.
So while running how to trace the output of application.

Here i am putting very small code to do so.
The idea behind this is make a object of process and execute a exe from runtime object.

get a inputstream reader from process.
Then trace the output.

Just see below.

/*
* Program for tracing a output from a executable
* Copyright 2009 @ yuvadeveloper
* Code By:- Prashant Chandrakar
*/
import java.io.*;
import javax.swing.JOptionPane;
import java.net.BindException;

class Loader
{
          public static void main(String as[])
          {
                   try
                   {
                             //pass the name of your exe in place of my.exe
                             Process p = Runtime.getRuntime().exec("DicomViewer.exe");

                             //getting inputstream reader from process
                             BufferedReader reader = new BufferedReader(new                   
                                        InputStreamReader(p.getInputStream()));
                             String line = reader.readLine();
                             while (line != null)
                             {
                                      line = reader.readLine();
                                      System.out.println(line);
                             }
                             reader.close();
                   }
                   catch (Exception e)
                   {
                   }
          }
}

Monday, March 9, 2009

Reading command Line Input in JAVA

Hi,

Developers migrating from c++ to java are first think of how
to pass command line input to java application or how to read command
line input in JAVA.

I am also worried on my early days of learning java.
So friends here I have some example to do so.

We can use BufferedReader class for that.
--------------------------------------------------------------------------------
import java.io.*;

public class ReadString
{
          public static void main(String[] args)
          {
                   //  prompt the user to enter their name
                   System.out.print("Enter your name: ");

                   //  open up standard input
                   BufferedReader br = new BufferedReader(new                
                                                 InputStreamReader(System.in));
                   String userName = null;

                   //  read the username from the command-line
                   try
                   {
                             userName = br.readLine();
                   }
                   catch (Exception e)
                   {
                             System.out.println(e);
                   }
                   System.out.println("UserName =  " + userName);
          }
}
======================================

Java 5.0 gives a new feature for reading inputs.
They launch java.uti.Scanner class, it gives more flexibility for reading
a input.

You can read from any file or you can search by a particular pattern.

import java.util.Scanner;
public class InputTest
{
          public static void main(String as[])
          {
                   String name = "";
                   int age = 0;

                   Scanner sc = new Scanner(System.in);
                   name = sc.nextLine();
                   age = sc.nextInt();
                   sc.close();
                   System.out.println("Name :-" + name);
                   System.out.println("Age :-" + age);
          }
}
=======================================

Reading Command Promt Output from JAVA


Hello Friends,


If sometimes you need reading a cmd.exe's output in java.
Here i am putting the code for doing that.

It is based on Buffered Readers's readLine() method.

But the point of attention is how to get your exe's InputStream.

See the snapshot in example i am reading a drive name from cmd.exe.

for the source code see below

/*

* Program for reading a command prompt output from java

* Copyright 2009 @ yuvadeveloper

* Code By:- Prashant Chandrakar

*

*/

import java.io.*;

public class cmdReader

{

public static void main(String arg[])

{

new test().setup();

}

void setup()

{

System.out.println("Testing getVolName");

////drive which name to be read

String path = "D:";

System.out.println("Volume name for " + path + " is: " + "'" + getDiskVolumeName(path) + "'");

}

public static String getDiskVolumeName(String path)

{

String volname = "Unknown";

String check = "Volume in drive " + path.charAt(0) + " is ";

try

{

//creating a process object and calling a cmd.exe with /c dir option

Process p = Runtime.getRuntime().exec("cmd /c dir " + path);

//getting input stream from a process

InputStreamReader is = new InputStreamReader(p.getInputStream());

//creating a buffered reader object for reading purpose

BufferedReader reader = new BufferedReader(is);

///reading a line

String line = reader.readLine();

while (line != null)

{

//getting substring for drive name

if (line.indexOf(check) != -1)

volname = line.substring(line.indexOf(check) + check.length());

line = reader.readLine();

}

}

catch (Exception e1)

{

System.out.println("Failure: " + e1.toString());

}

return volname;

}

}

Here below i am pointed the read text.

Snapshot:-