Saturday, February 28, 2009

Layout Managers in JAVA

Hi friends,


While setting Layout in JAVA application, everyone is just
thinking of some common layouts. They are...
  1. FlowLayout
  2. BorderLayout
  3. GridLayout
  4. GridBagLayout
  5. Card Layout
  6. Null Layout or Absolute Positioning.
But they all are belongs to Java.awt.classes

But Swing introduces some of very new and intresting types of
layouts.
They are...
  1. BoxLayout
  2. GroupLayout
  3. OverlayLayout
  4. SpringLayout
  5. ScrollPaneLayout
  6. ViewPortLayout
So friends here i am discussing about all.

Null Layout or Absolute Positioning in JAVA

Hi freinds,

Most of the time every developer is thinking which layout is to be set.
So that all the added component (like button,Text box etc.) can not
affect while resizing the JFrame or their container.

So we that there are number of layout to do so.
And for many of the users GridBagLayout is best for their work.

But many of the user also feel that it is some difficult and takes too
much lines to code.

So my dear friend use NullLayout which kept is sort and sweet.
On this layout you can easily choose X, Y postion and also width and
height of your particular component.

See the snapshot you can also put button overlapping.
Same as other component.

It is not a Layout Manager and not need to add like others.
just setLayout(null); will add this to your component.

I am created a very simple application which implement this method.
just go through it.

For source code see below


/*
* Program for setting null layout or absolute positioning
* you can put button over another button
* Copyright 2009 @ yuvadeveloper
* Code By:- Prashant Chandrakar
*
*/

import java.awt.*;

import javax.swing.*;

import javax.swing.plaf.synth.*;


public class NullLayout extends JFrame

{
NullLayout()
{

super("Null Layout Prashant Chandrakar");

try

{

JFrame.setDefaultLookAndFeelDecorated(true);

this.getContentPane().setBackground(Color.GRAY);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


JLabel uname = new JLabel("UserName");

JLabel pword = new JLabel("Password");

JTextField utxt = new JTextField(20);

JTextField ptxt = new JTextField(20);

this.setLayout(null);

////by using this method set the x,y ,width ,height
uname.setBounds(40, 27, 100, 25);
utxt.setBounds(150, 27, 100, 25);
pword.setBounds(40, 70, 100, 25);
ptxt.setBounds(150, 70, 100, 25);

this.add(uname);

this.add(utxt);

this.add(pword);

this.add(ptxt);

JButton btsave = new JButton("SAVE");

JButton btcancel = new JButton("CANCEL");

btcancel.setBounds(120, 127, 104, 25);
btsave.setBounds(100, 107, 104, 25);

this.add(btsave);

this.add(btcancel);

}

catch (Exception e)

{

System.out.println(e);

}

this.setSize(350, 200);

this.setVisible(true);

}

public static void main(String[] args) throws Throwable

{

new NullLayout();

}
}

Snapshot:-













Thursday, February 26, 2009

Single Instance Application in JAVA

Hello Friends,


One of the most asking question in java world is how to make java
application as a single instance.

I google it and found many of the techniques.
I am posted here some of popular techniques.
Just go ahead and contact if you have problem......

1. By capturing port or through ServerSocket (short code) .
On this method we are creating a object of java.net.ServerSocket class.
And by passing a port number we are captured while first instance so
that if another instance occurred it is throwing a bind Exception and
you can tracked that any more instance is running on system.

Just see the link for code


2. By capturing port or through ServerSocket (Big code).
It is same as the first method but while google i got this big code with
different option just go through the code.

Just see the link for code
See the original source here get from google


3. By accessing file from local file system.
This is also another method for doing the same thing.
But it is not that much preferable because sometime when JVM
crashes or due to some IO error occured then file is not deleted
from hard disk.
note:- Dont put your file (you can use any file) in C drive or where OS exist.

Just see below for code

/*

* Program for setting single instance in JAVA

* Copyright 2009 @ yuvadeveloper

* Code By:- Prashant Chandrakar

*

*/

import java.net.ServerSocket;

import javax.swing.JOptionPane;

import javax.swing.JFrame;

import java.io.IOException;

import java.net.BindException;

class SingleInstance

{

public static ServerSocket serverSocket;

public static String errortype = "Access Error";

public static String error = "Application already running.....";

public static void main(String as[])

{

try

{

//creating object of server socket and bind to some port number

serverSocket = new ServerSocket(15486);

////do not put common port number like 80 etc.

////Because they are already used by system

JFrame jf = new JFrame();

jf.setVisible(true);

jf.setSize(200, 200);

}

catch (BindException exc)

{

JOptionPane.showMessageDialog(null, error, errortype, JOptionPane.ERROR_MESSAGE);

System.exit(0);

}

catch (IOException exc)

{

JOptionPane.showMessageDialog(null, error, errortype, JOptionPane.ERROR_MESSAGE);

System.exit(0);

}

}

}



4. By using java sun.jvmstat package from tools.jar.

Just see the link for code

5. By using Launch4j application.
It is a third party tools for creating a EXE for your application.
It is giving you a facility of creating single instance application.
Just try it. It is perfect tool.

Just see the launch4j application doc

Friday, February 20, 2009

Custom look and feel in JAVA via XML file

Hi Everyone,


If you want to make your own images on button,
Or want to change button style in over event or clicked event.

If you want to rounded corner text box or whatever you want.
Just see the below code.
Here i have three images.The first two are for buttons and last one for text box.
Just save these images on images folder and change their path in xml file.
After my java code i am also posting xml file code.
just save code in "myproperty.xml" file and put aside of java file.

for full source code follow the link



*
* look and feel from your own images
* Supply images via xml file
* apply images on textbox button etc
* Copyright 2009@ yuvadeveloper
* Code by:- Prashant Chandrakar
* */
import java.awt.*;
import javax.swing.*;
import javax.swing.plaf.synth.*;
public class MyDesign extends JFrame
{
MyDesign()
{
super("Look and Feel by Prashant Chandrakar");
try
{
////import xml file
String xmlFile = "myproperty.xml";
SynthLookAndFeel lookandfeel = new SynthLookAndFeel();
lookandfeel.load(MyDesign.class.getResourceAsStream(xmlFile), MyDesign.class);
////set look and feel
UIManager.setLookAndFeel(lookandfeel);
this.getContentPane().setBackground(Color.DARK_GRAY );
this.getContentPane().setLayout(new FlowLayout());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
////Adding labels TextField
JLabel uname = new JLabel("UserName");
JLabel pword = new JLabel("Password");
JTextField utxt = new JTextField(20);
JTextField ptxt = new JTextField(20);
Container contentPane = this.getContentPane();
////setting layout to null
contentPane.setLayout(null);
addComponent(contentPane, uname, 40, 27, 100, 25);
addComponent(contentPane, utxt, 150, 27, 100, 25);
addComponent(contentPane, pword, 40, 70, 100, 25);
addComponent(contentPane, ptxt, 150, 70, 100, 25);
JButton btsave = new JButton("SAVE");
addComponent(contentPane, btsave, 100, 107, 104, 33);
}
catch (Exception e)
{
System.out.println(e);
}
this.setSize(350,200);
this.setVisible(true);
}
private void addComponent(Container container, Component c, int x, int y, int width, int height)
{
c.setBounds(x, y, width, height);
this.add(c);
}
public static void main(String[] args) throws Throwable
{
new MyDesign();
}
}

XML File
Save file as "myproperty.xml" put together with java file"
=============================================================













Related Images:- Just save as it is and put inside images folder









ScreenShot:-


Friday, February 13, 2009

Converting Image to BufferedImage in JAVA

Hi,


While working on some project i was bound in some situation.....
  1. For opening image i have to use java.awt.Image class.
  2. For saving drawn image to jpg i have to use ImageIo class.
  3. So for passing ImageIO.write() method i need image object to bufferedImage object.
It is not only one situation when you need this, Sometime when reading
image java.awt.Image class is very useful for opening images from
particular path.

So their are number of tricks to do so.
I am posting here my favorite way of conversion.
Just go through it.

First i loaded image in Image object via Toolkit class.
Then i created a blank BufferedImage object of same dimension.
Then i just draw Image object into BufferedImage object.
Now your BufferedImage object consist the same image.

For full source code follow the link:-

/*

* Program for Converting Image Object to BufferedImage Object

* Copyright 2009 @ yuvadeveloper

* Code By:- Prashant Chandrakar

*

*/

import java.awt.*;

import java.awt.image.*;

class Convert

{

public static void main(String as[])

{

////opening image in Image Object

Image img = Toolkit.getDefaultToolkit().getImage("test.jpeg");

////taking dimension of image

int width = img.getWidth(null);

int height = img.getHeight(null);

////creating a bufferedImage object of same dimension as image

BufferedImage bimg = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);

////taking graphics object from BufferedImage

Graphics2D gg = bimg.createGraphics();

////drawing Image objects image into graphics of buffered image object

gg.drawImage(img, 0, 0, img.getWidth(null), img.getHeight(null), null);

///disposing graphics.

gg.dispose();

}

}

Thursday, February 12, 2009

Highlight a selected portion of image in JAVA

Hi,

Here i am crerating a small application which just highlight a
area over image.

I am highlight a rectangular area.You can change as you required.
I am setting a color(255,255,0) for highlight.
You can change it according to your requrement.
Also if you want to change beta value of highlight area then change
the value of transparancy variable.

See the below screenshot where i am highlighted in yellow shed with
transparency value 80.

For full source code follow this link:-

/*

* Program for Highlighting a particular portion of image in any color

* Copyright 2009 @ yuvadeveloper

* Code By:- Prashant Chandrakar

*

*/

import java.awt.*;

import java.awt.image.*;

import java.io.*;

import javax.imageio.*;

class HighlightImage

{

public static void main(String a[])

{

try

{

////pass image file path open for drawing

BufferedImage image = ImageIO.read(new File("1.jpg"));

////taking graphics from image

Graphics g = image.getGraphics();

////just change the transparency value for changing beta values of image

int transparency = 50;

/////drawing string on graphics of opened image

g.drawString("We are highlighting a selected portion of image", 10, 10);

Color color = new Color(255, 255, 0, 255 * transparency / 100);

g.setColor(color);

g.fillRect(30, 30, 100, 100);

////saving image by name output.jpg

ImageIO.write(image, "jpg", new File("output.jpg"));

}

catch (Exception e)

{

System.out.println(e);

}

}

}

ScreenShot:-