How to determine a word contain or not in a sentence using java ???

*******************************************************
COMPLETE SOURCE CODE FOR : DetermineStringContainSubstring.java
*******************************************************


public class DetermineStringContainSubstring
{
public static void main(String[]args)
{
String a="Welcome to java world";

String substring1="Welcome to java world";
String substring2="elc";
String substring3="Wel";
String substring4=" t";
String substring5="rld";

//CHECK MATCH WHOLE WORD
if(a.matches(substring1))
{
System.out.println("substring1 match to a");
}

//CHECK SUBSTRING NOT LOCATE AT START OR END OF STRING
if(a.indexOf(substring2)>0)
{
System.out.println("substring2 contain in a");
}

//CHECK SUBSTRING NOT LOCATE AT START OR END OF STRING
if(a.indexOf(substring4)>0)
{
System.out.println("substring4 contain in a");
}

//CHECK SUBSTRING LOCATE AT START OF STRING
if(a.startsWith(substring3))
{
System.out.println("substring3 contain in a");
}

//CHECK SUBSTRING LOCATE AT END OF STRING
if(a.endsWith(substring5))
{
System.out.println("substring5 contain in a");
}
}
}


*******************************************************
JUST COMPILE AND EXECUTE IT
*******************************************************

Determine what package from a class object ???

Source code below will print name for package that belong to it's own class object. It should print null because class object that create from java file below not belong to any package.

************************************************************
COMPLETE SOURCE CODE FOR : DeterminePackageForClassObject.java
************************************************************


public class DeterminePackageForClassObject
{
public static void main(String[]args)
{
try
{
Class temp=Class.forName("DeterminePackageForClassObject");
System.out.println("PACKAGE FOR THIS CLASS OBJECT IS :"+temp.getPackage());
}
catch(Exception exception)
{
exception.printStackTrace();
}
}
}


************************************************************
JUST COMPILE AND EXECUTE IT
************************************************************

Get all interface from a class object

This program will print name of interface that implemented by this class

**********************************************************
COMPLETE SOURCE CODE FOR : ListingAllInterfaceFromClassObject.java
**********************************************************


import java.awt.event.ActionListener;//INTERFACE 1
//Runnable-INTERFACE 2
import java.awt.event.ActionEvent;

public class ListingAllInterfaceFromClassObject implements ActionListener,Runnable
{
public void actionPerformed(ActionEvent evt)
{
//NO IMPLEMENTATION
}

public void run()
{
//NO IMPLEMENTATION
}

public static void main(String[]args)
{
try
{
Class temp=Class.forName("ListingAllInterfaceFromClassObject");
Class[]allInterface=temp.getInterfaces();
int allInterfaceLength=allInterface.length;
int temp2=0;
while(temp2!=allInterfaceLength)
{
System.out.println("INTERFACE "+(temp2+1)+" :"+(allInterface[temp2].getSimpleName()));
temp2=temp2+1;
}
}
catch(Exception exception)
{
exception.printStackTrace();
}
}
}


**********************************************************
JUST COMPILE AND EXECUTE IT
**********************************************************

Get superclass for an object class ???

Source code below will get superclass for it's own object class. It will print name for it's superclass.

*****************************************************************
COMPLETE SOURCE CODE FOR : GettingSuperclass.java
*****************************************************************


public class GettingSuperclass
{
public static void main(String[]args)
{
try
{
Class temp=Class.forName("GettingSuperclass");
Class thisIsSuper=temp.getSuperclass();

System.out.println("Superclass for this class is : "+thisIsSuper.getSimpleName());
}
catch(Exception exception)
{
exception.printStackTrace();
}
}
}


*****************************************************************
JUST COMPILE AND EXECUTE IT
*****************************************************************

Determine what is represent by a class object, whether an interface or a class ???

This tutorial will use CreateInterface.java to determine whether class object from it, is an interface or a class. You can get it at Java Interface ( Part 1). Before you compile and execute source code below, you must make sure CreateInterface.class is locate same location with this java file.

***********************************************************
COMPLETE SOURCE CODE FOR : DetermineClassObjectIsClassOrInterface.java
***********************************************************


public class DetermineClassObjectIsClassOrInterface
{
public static void main(String[]args)
{
try
{
Class temp=Class.forName("CreateInterface");
if(temp.isInterface())
{
System.out.println("THIS CLASS OBJECT IS REPRESENT AN INTERFACE");
}
else
{
System.out.println("THIS CLASS OBJECT IS REPRESENT AN CLASS");
}
}
catch(Exception exception)
{
exception.printStackTrace();
}
}
}


***********************************************************
JUST COMPILE AND EXECUTE IT
***********************************************************

Java Interface (Part 2)

This tutorial will show you, how to use a java interface that we create in my previous post. When we want to use a java interface, we must overwrite abstract method that contain in the interface. Source code below will overwrite abstract method in java interface (CreateInterface) called print(). Before we can use a java interface we must put implements keyword followed by interface name. You also must import interface into your program. In the example below, i don't import the interface because, it's location is same with this java file.

*******************************************************
COMPLETE SOURCE CODE FOR : UseJavaInterface.java
*******************************************************


public class UseJavaInterface implements CreateInterface
{
public void print()
{
System.out.println("HI");
}
}


*******************************************************
JUST COMPILE AND EXECUTE IT
*******************************************************

Java Interface (Part 1)

Source code below will create a java interface, named CreateInterface. When you want to create an interface, you just change "class" keyword after "public" keyword to "interface" keyword and after that followed by your java file name. In a java interface, all method is only abstract method. You don't need to put access modifier to abstract method in a java interface. This is because all of it will be set to public. So when you want to overwrite an abstract method from an interface you must know, access modifier that you must use is public, not private or protected. After that put return type followed by name for the abstract method. In source code below, abstract method is print(). What is java abstract method ??? The answer is, an abstract method is a method without body. If you can see, abstract method print() end after ")" ,not contain any implementation code. Where to put implementation code for a java abstract method ??? You will put it's implementation code in java concrete class that implements this interface by overwrite it.

*****************************************************************
COMPLETE SOURCE CODE FOR : CreateInterface.java
*****************************************************************


public interface CreateInterface
{
void print();
}


*****************************************************************
JUST COMPILE AND EXECUTE IT
*****************************************************************

Get class name from class object ???

*****************************************************************
COMPLETE SOURCE CODE FOR : GetClassNameFromClassObject.java
*****************************************************************


public class GetClassNameFromClassObject
{
public static void main(String[]args)
{
try
{
//For example of this source code, we will use java predefined class named Object

//You can change class name instead of java.lang.Object
Class a=Class.forName("java.lang.Object");

//Now we will print the name of the class
System.out.println("IT'S NAME IS : "+a.getSimpleName());
}

catch(Exception exception)
{
exception.printStackTrace();
}
}
}


*****************************************************************
JUST COMPILE AND EXECUTE IT
*****************************************************************

Change java radio button text color

*********************************************************************
COMPLETE SOURCE CODE FOR : ChangeJavaRadioButtonTextColor.java
*********************************************************************


import javax.swing.JFrame;
import javax.swing.JRadioButton;

import java.awt.FlowLayout;
import java.awt.Color;

public class ChangeJavaRadioButtonTextColor
{
public static void main(String[]args)
{
JRadioButton a=new JRadioButton("I AM A RADIO BUTTON");
JFrame b=new JFrame("CHANGE RADIO BUTTON TEXT COLOR");
b.setLayout(new FlowLayout());

//METHOD setForeground will change radio button's text color
//You can see more color in Color class on Java API
a.setForeground(Color.GREEN);

b.add(a);

b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b.setSize(400,400);
b.setVisible(true);
}
}


*********************************************************************
JUST COMPILE AND EXECUTE IT
*********************************************************************

Source code to get class object ???

*******************************************************
COMPLETE SOURCE CODE FOR : GetClassObject.java
*******************************************************


public class GetClassObject
{
public static void main(String[]args)
{
try
{
//This program will get class object for Object class
Class a=Class.forName("java.lang.Object");
}
catch(Exception exception)
{
exception.printStackTrace();
}
}
}


*******************************************************
JUST COMPILE AND EXECUTE IT
*******************************************************

How to know all method that contain in a java class file ???

How to know all method that contain in a java class file ???
Source code below will list all method that contain in java predefined class, named Object. For your information ,all java program will be subclass to Object class. If you write a java program, your java program will be subclass to Object class automatically. You may be, not see how it's happen but it is truly happen.

How to create object from primitive data type ???

***************************************************************************
COMPLETE SOURCE CODE FOR : CreateObjectFromPrimitiveDataType.java
***************************************************************************


public class CreateObjectFromPrimitiveDataType
{
public static void main(String[]args)
{
//PRIMITIVE TYPE : boolean
boolean a=true;
Boolean booleanObject=new Boolean(a);//CREATE OBJECT FROM a

//PRIMITIVE TYPE : byte
byte b=(byte)9;
Byte byteObject=new Byte(b);

//PRIMITIVE TYPE : char
char c='a';
Character charObject=new Character(c);

//PRIMITIVE TYPE : short
short d=(short)9;
Short shortObject=new Short(d);

//PRIMITIVE TYPE : integer
int e=9;
Integer intObject=new Integer(e);

//PRIMITIVE TYPE : long
long f=9L;
Long longObject=new Long(f);

//PRIMITIVE TYPE : float
float g=9.9F;
Float floatObject=new Float(g);

//PRIMITIVE TYPE : double
double h=9.9D;
Double doubleObject=new Double(h);

//PRINT VALUE FOR EACH OBJECT
System.out.println("VALUE FOR BOOLEAN : "+booleanObject.booleanValue());
System.out.println("VALUE FOR BYTE : "+byteObject.byteValue());
System.out.println("VALUE FOR CHARACTER : "+charObject.charValue());
System.out.println("VALUE FOR SHORT: "+shortObject.shortValue());
System.out.println("VALUE FOR INTEGER: "+intObject.intValue());
System.out.println("VALUE FOR LONG : "+longObject.longValue());
System.out.println("VALUE FOR FLOAT : "+floatObject.floatValue());
System.out.println("VALUE FOR DOUBLE: "+doubleObject.doubleValue());
}
}


***************************************************************************
JUST COMPILE AND EXECUTE IT
***************************************************************************

How to get hash code from a java object ???

*************************************************
COMPLETE SOURCE CODE FOR : GetObjectHashCode.java
*************************************************


public class GetObjectHashCode
{
public static void main(String[]args)
{
//Create a String object with value Hello
String stringObject=new String("Hello");

//Print hash code for the object
System.out.println("HASH CODE VALUE : "+stringObject.hashCode());
}
}


*************************************************
JUST COMPILE AND EXECUTE IT
*************************************************

How to clone a java object ???

**********************************************************
COMPLETE SOURCE CODE FOR : CloneObjectInJava.java
**********************************************************


//Class for object that you want to clone must overwrite method clone() first

public class CloneObjectInJava implements Cloneable
{
public void printAText()
{
System.out.println("HI");
}

//OVERWRITE METHOD CLONE
public Object clone()
{
Cloneable makeClone=new CloneObjectInJava();
return makeClone;
}

public static void main(String[]args)
{
//Create a CloneObjectInJava object name "coij"
CloneObjectInJava coij=new CloneObjectInJava();

try
{
//Create clone for "coij"
CloneObjectInJava coijClone=(CloneObjectInJava)coij.clone();
//Call method printAText using clone
coijClone.printAText();
}
catch(Exception exception)
{
exception.printStackTrace();
}
}
}


**********************************************************
JUST COMPILE AND EXECUTE IT
**********************************************************

How to get amount of free memory in jvm ???

***********************************************************
COMPLETE SOURCE CODE FOR : FreeMemoryInJvm.java
***********************************************************


public class FreeMemoryInJvm
{
public static void main(String[]args)
{
System.out.println("Free Memory In Jvm is : "+Runtime.getRuntime().freeMemory()+" bytes");
}
}


***********************************************************
JUST COMPILE AND EXECUTE IT
***********************************************************

How to know maximum amount of memory that will be use by jvm ???

******************************************************
COMPLETE SOURCE CODE FOR : MaxMemoryThatJvmWillUse.java
******************************************************


public class MaxMemoryThatJvmWillUse
{
public static void main(String[]args)
{
System.out.println("Max Amount Of Memory That Will Be Use By Jvm is : "+Runtime.getRuntime().maxMemory()+ " bytes");
}
}


******************************************************
JUST COMPILE AND EXECUTE IT
******************************************************

How to get total memory in jvm ???

*********************************************************
COMPLETE SOURCE CODE FOR : GetTotalMemoryInJvm.java
*********************************************************


public class GetTotalMemoryInJvm
{
public static void main(String[]args)
{
//Print total memory in java virtual machine in bytes
System.out.println("TOTAL MEMORY IN JVM : "+Runtime.getRuntime().totalMemory());
}
}


*********************************************************
JUST COMPILE AND EXECUTE IT
*********************************************************

What is java memory heap ???

What is java memory heap ???
It is an area in memory where an object are created

How to make java program output to a file instead of command prompt like usual ???

This source code will show you how to make java output that print in command prompt will be print in a file instead of command prompt.

**************************************************************
COMPLETE SOURCE CODE FOR : PutCommandPromptOutputToFile.java
**************************************************************


import java.io.PrintStream;

public class PutCommandPromptOutputToFile
{
public static void main(String[]args)
{
try
{
PrintStream newOuputChannel=new PrintStream("output.txt");
System.setOut(newOuputChannel);

System.out.println("HI");
}
catch(Exception exception)
{
exception.printStackTrace();
}
}
}


**************************************************************
JUST COMPILE AND EXECUTE IT
**************************************************************

Source code to know how long a java application is running

*********************************************************
COMPLETE SOURCE CODE FOR : HowLongApplicationRunning.java
*********************************************************


public class HowLongApplicationRunning
{
public static void main(String[]args)
{
long startTime=System.currentTimeMillis();

try
{
//MAKE YOUR APPLICATION SLEEP IN 10 SECONDS
Thread.sleep(10000);
}
catch(Exception exception)
{
exception.printStackTrace();
}

long endTime=System.currentTimeMillis();

//GET DURATION IN SECOND
long timeInSecond=(endTime-startTime)/1000;

System.out.println("Duration For Your Application Running Is : "+timeInSecond+" seconds");
}
}


*********************************************************
JUST COMPILE AND EXECUTE IT
*********************************************************

Java source code to determine when application exit by print a line of text on command prompt

*************************************************************
COMPLETE SOURCE CODE FOR : DetermineWhenApplicationExit.java
*************************************************************


public class DetermineWhenApplicationExit
{
public static void main(String[]args)
{
Runtime.getRuntime().addShutdownHook(new Thread()
{
public void run()
{
//Application will print "BYE" when it close
System.out.println("BYE");
}
});

//Make this application terminate
System.exit(0);
}
}


*************************************************************
JUST COMPILE AND EXECUTE IT
*************************************************************

Java source code for application terminate (Exit application)

*****************************************************************
COMPLETE SOURCE CODE FOR : ExitApplication.java
*****************************************************************


public class ExitApplication
{
public static void main(String[]args)
{
//System.exit(0) will make your application exit
System.exit(0);
}
}


*****************************************************************
JUST COMPILE AND EXECUTE IT
*****************************************************************

How to draw rotate string in java ???

*********************************************************************
COMPLETE SOURCE CODE FOR : DrawRotateText.java
*********************************************************************


import java.awt.Frame;
import java.awt.Panel;

import java.awt.Graphics;
import java.awt.Graphics2D;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import java.awt.geom.AffineTransform;

public class DrawRotateText extends Panel
{
public DrawRotateText()
{
Frame a=new Frame("Draw Rotate Text");
WindowListener wl=new WindowAdapter()
{
public void windowClosing(WindowEvent evt)
{
System.exit(0);
}
};

a.addWindowListener(wl);
a.add(this);
a.setSize(400,400);
a.setVisible(true);
}

public void paint(Graphics g)
{
Graphics2D g2d=(Graphics2D)g;
AffineTransform at=new AffineTransform();
at.setToRotation(-Math.PI/4);//SET ROTATION HERE IN radians
g2d.setTransform(at);

g2d.drawString("HI EVERYONE !!",100,200);
}

public static void main(String[]args)
{
DrawRotateText drt=new DrawRotateText();
}
}


*********************************************************************
JUST COMPILE AND EXECUTE IT
*********************************************************************

PART 3 Java Source Code : Draw String In Java

This part will show you why when you set g.drawString("HI EVERYONE",10,50); to
g.drawString("HI EVERYONE",10,10); in PART 1, you can't see the wording in frame. This is because wording "HI EVERYONE " is hide at back of frame title. So if you want to see it, you just call method setUndecorated for frame and set it's argument to true. This will make your frame title hide and you can see the wording.

************************************************************
COMPLETE SOURCE CODE FOR : DrawingText3.java
************************************************************


import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Font;
import java.awt.Color;

import java.awt.event.WindowListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class DrawingText3 extends Frame
{
WindowListener wl=new WindowAdapter()
{
public void windowClosing(WindowEvent evt)
{
System.exit(0);
}
};

public DrawingText3()
{
super("Drawing Text");
addWindowListener(wl);
setUndecorated(true);
setSize(400,200);
setVisible(true);
}

public void paint(Graphics g)
{
super.paint(g);
g.setFont(new Font("Serif",Font.BOLD,12));
g.setColor(Color.BLACK);

g.drawString("HI EVERYONE",10,10);
}

public static void main(String[]args)
{
DrawingText3 dt=new DrawingText3();
}
}


************************************************************
JUST COMPILE AND EXECUTE IT
************************************************************

PART 2 Java Source Code : Draw String In Java

This part of draw string in java is not much different to the previous part. In this part, we just use FontMetrics class to get information about Font like it's height. You can see it at g.drawString("HI EVERYONE",10,fm.getHeight()*3);

************************************************************
COMPLETE SOURCE CODE FOR : DrawingText2.java
************************************************************


import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Font;
import java.awt.Color;
import java.awt.FontMetrics;

import java.awt.event.WindowListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class DrawingText2 extends Frame
{
WindowListener wl=new WindowAdapter()
{
public void windowClosing(WindowEvent evt)
{
System.exit(0);
}
};

public DrawingText2()
{
super("Drawing Text");
addWindowListener(wl);
setSize(400,200);
setVisible(true);
}

public void paint(Graphics g)
{
super.paint(g);
g.setFont(new Font("Serif",Font.BOLD,12));
g.setColor(Color.BLACK);

FontMetrics fm=g.getFontMetrics();
g.drawString("HI EVERYONE",10,fm.getHeight()*3);
}

public static void main(String[]args)
{
DrawingText2 dt=new DrawingText2();
}
}


************************************************************
JUST COMPILE AND EXECUTE IT
************************************************************

PART 1 Java Source Code : Draw String In Java

This is part one of draw string in java. If you change g.drawString("HI
EVERYONE",10,50); in source code below to g.drawString("HI EVERYONE",10,10); you can't see wording. If you want to see it, you can see part three of draw string in
java.So you can know why you can't see it in this part.

*************************************************************
COMPLETE SOURCE CODE FOR : DrawingText.java
*************************************************************


import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Font;
import java.awt.Color;

import java.awt.event.WindowListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class DrawingText extends Frame
{
WindowListener wl=new WindowAdapter()
{
public void windowClosing(WindowEvent evt)
{
System.exit(0);
}
};

public DrawingText()
{
super("Drawing Text");
addWindowListener(wl);
setSize(400,200);
setVisible(true);
}

public void paint(Graphics g)
{
super.paint(g);
g.setFont(new Font("Serif",Font.BOLD,12));
g.setColor(Color.BLACK);
g.drawString("HI EVERYONE",10,50);
}

public static void main(String[]args)
{
DrawingText dt=new DrawingText();
}
}


*************************************************************
JUST COMPILE AND EXECUTE IT
*************************************************************

Image In Java (JPG, GIF, PNG)

Image In Java (JPG, GIF, PNG)
(Copy And Paste Source Code Below Into Wordprocessing Software Like Notepad. Save it as java file. For file name, just use file name like given. Compile and execute it)


sample_picture.jpg

Note : Before you can compile and execute java source code below, save sample_picture.jpg above at same location with your source code and make sure it's name is sample_picture.jpg

READ A PICTURE FILE WITH FORMAT JPG, GIF OR PNG IN JAVA

***********************************************

COMPLETE SOURCE CODE FOR ReadImageFileInJava.java

***********************************************


import java.awt.Image;
import java.awt.Toolkit;

public class ReadImageFileInJava
{
public static void main(String[]args)
{
//READ AN IMAGE FILE, sample_picture.jpg
//METHOD getImage() CAN READ IMAGE FILE WITH FORMAT gif, jpg and png
Image a=Toolkit.getDefaultToolkit().getImage("sample_picture.jpg");
}
}


GET WIDTH AND HEIGHT FOR AN IMAGE IN JAVA

***********************************************

COMPLETE SOURCE CODE FOR ReadImageFileInJava.java

***********************************************


import java.awt.Image;
import javax.swing.ImageIcon;

import java.awt.Toolkit;

public class ReadImageFileInJava
{
public static void main(String[]args)
{
//READ AN IMAGE FILE, sample_picture.jpg
//METHOD getImage() CAN READ IMAGE FILE WITH FORMAT gif, jpg and png
Image a=Toolkit.getDefaultToolkit().getImage("sample_picture.jpg");

ImageIcon b=new ImageIcon(a);

//GET PICTURE'S WIDTH
int c=b.getIconWidth();
System.out.println("WIDTH : "+c+" pixels");

//GET PICTURE'S HEIGHT
int d=b.getIconHeight();
System.out.println("HEIGHT : "+d+" pixels");
}
}


PUT IMAGE INTO A CONTAINER IN JAVA

Note : When you want to draw image into a container, you must overwrite method paint for the container. After that you can use method drawImage from Graphics class to draw image for you. You can see it at the end of source code below.

***********************************************

COMPLETE SOURCE CODE FOR ReadImageFileInJava.java

***********************************************


import java.awt.Graphics;
import java.awt.Frame;
import java.awt.Image;
import java.awt.Toolkit;

import java.awt.event.WindowListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ImageIcon;

public class ReadImageFileInJava
{
public static void main(String[]args)
{
//READ AN IMAGE FILE, sample_picture.jpg
//METHOD getImage() CAN READ IMAGE FILE WITH FORMAT gif, jpg and png
Image a=Toolkit.getDefaultToolkit().getImage("sample_picture.jpg");

ImageIcon b=new ImageIcon(a);

//GET PICTURE'S WIDTH
int c=b.getIconWidth();
System.out.println("WIDTH : "+c+" pixels");

//GET PICTURE'S HEIGHT
int d=b.getIconHeight();
System.out.println("HEIGHT : "+d+" pixels");

FrameWithPicture e=new FrameWithPicture();
e.setSize(c,d);
e.setVisible(true);
e.setResizable(false);
}
}

//CLASS FrameWithPicture
class FrameWithPicture extends Frame
{
//FOR FRAME CLOSE BUTTON
WindowListener wl=new WindowAdapter()
{
public void windowClosing(WindowEvent evt)
{
System.exit(0);
}
};

public FrameWithPicture()
{
//FOR FRAME'S TITLE
super("DISPLAY IMAGE");

//ADD WINDOW LISTENER FOR FRAME
addWindowListener(wl);
}

//PAINT IMAGE(sample_picture.jpg) INTO FRAME
//OVERWRITE METHOD PAINT
public void paint(Graphics g)
{
super.paint(g);
Image a=Toolkit.getDefaultToolkit().getImage("sample_picture.jpg");
g.drawImage(a, 0, 0,null);
}
}


SCALE IMAGE IN JAVA

***********************************************

COMPLETE SOURCE CODE FOR ScaleImageInJava.java

***********************************************


import java.awt.geom.AffineTransform;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Frame;
import java.awt.Image;
import java.awt.Toolkit;

import java.awt.event.WindowListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ImageIcon;

public class ScaleImageInJava
{
public static void main(String[]args)
{
FrameWithPicture e=new FrameWithPicture();
e.setSize(400,400);
e.setVisible(true);
//e.setResizable(false);
}
}

//CLASS FrameWithPicture
class FrameWithPicture extends Frame
{
//FOR FRAME CLOSE BUTTON
WindowListener wl=new WindowAdapter()
{
public void windowClosing(WindowEvent evt)
{
System.exit(0);
}
};

public FrameWithPicture()
{
//FOR FRAME'S TITLE
super("DISPLAY IMAGE");

//ADD WINDOW LISTENER FOR FRAME
addWindowListener(wl);
}

//PAINT IMAGE(sample_picture.jpg) INTO FRAME
public void paint(Graphics g)
{
Graphics2D g2d=(Graphics2D)g;
AffineTransform at=new AffineTransform();

//THIS WILL SCALE THE IMAGE
//YOU CAN TRY CHANGE THIS VALUE
//CURRENT VALUE IS 1 AND 1
at.scale(1,1);

Image a=Toolkit.getDefaultToolkit().getImage("sample_picture.jpg");
g2d.drawImage(a,at,this);
}
}


SHEAR IMAGE IN JAVA

***********************************************

COMPLETE SOURCE CODE FOR ShearImageInJava.java

***********************************************


import java.awt.geom.AffineTransform;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Frame;
import java.awt.Image;
import java.awt.Toolkit;

import java.awt.event.WindowListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ImageIcon;

public class ShearImageInJava
{
public static void main(String[]args)
{
FrameWithPicture e=new FrameWithPicture();
e.setSize(400,400);
e.setVisible(true);
}
}

//CLASS FrameWithPicture
class FrameWithPicture extends Frame
{
//FOR FRAME CLOSE BUTTON
WindowListener wl=new WindowAdapter()
{
public void windowClosing(WindowEvent evt)
{
System.exit(0);
}
};

public FrameWithPicture()
{
//FOR FRAME'S TITLE
super("DISPLAY IMAGE");

//ADD WINDOW LISTENER FOR FRAME
addWindowListener(wl);
}

//PAINT IMAGE(sample_picture.jpg) INTO FRAME
public void paint(Graphics g)
{
Graphics2D g2d=(Graphics2D)g;
AffineTransform at=new AffineTransform();

//THIS WILL SHEAR THE IMAGE
//YOU CAN TRY CHANGE THIS VALUE
//CURRENT VALUE IS 0.2 AND 1
at.shear(0.2,1);

Image a=Toolkit.getDefaultToolkit().getImage("sample_picture.jpg");
g2d.drawImage(a,at,this);
}
}


TRANSLATE IMAGE IN JAVA (CHANGE IMAGE LOCATION)

***********************************************

COMPLETE SOURCE CODE FOR TranslateImageInJava.java

***********************************************


import java.awt.geom.AffineTransform;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Frame;
import java.awt.Image;
import java.awt.Toolkit;

import java.awt.event.WindowListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ImageIcon;

public class TranslateImageInJava
{
public static void main(String[]args)
{
FrameWithPicture e=new FrameWithPicture();
e.setSize(400,400);
e.setVisible(true);
}
}

//CLASS FrameWithPicture
class FrameWithPicture extends Frame
{
//FOR FRAME CLOSE BUTTON
WindowListener wl=new WindowAdapter()
{
public void windowClosing(WindowEvent evt)
{
System.exit(0);
}
};

public FrameWithPicture()
{
//FOR FRAME'S TITLE
super("DISPLAY IMAGE");

//ADD WINDOW LISTENER FOR FRAME
addWindowListener(wl);
}

//PAINT IMAGE(sample_picture.jpg) INTO FRAME
public void paint(Graphics g)
{
Graphics2D g2d=(Graphics2D)g;
AffineTransform at=new AffineTransform();

//THIS WILL TRANSLATE THE IMAGE
//YOU CAN TRY CHANGE THIS VALUE
//CURRENT VALUE IS 20 AND 60
at.translate(20,60);

Image a=Toolkit.getDefaultToolkit().getImage("sample_picture.jpg");
g2d.drawImage(a,at,this);
}
}


ROTATE IMAGE IN JAVA (ANGLE OF ROTATION IS IN RADIANS VALUE)

***********************************************

COMPLETE SOURCE CODE FOR RotateImageInJava.java

***********************************************


import java.awt.geom.AffineTransform;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Frame;
import java.awt.Image;
import java.awt.Toolkit;

import java.awt.event.WindowListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ImageIcon;

public class RotateImageInJava
{
public static void main(String[]args)
{
FrameWithPicture e=new FrameWithPicture();
e.setSize(400,400);
e.setVisible(true);
}
}

//CLASS FrameWithPicture
class FrameWithPicture extends Frame
{
//FOR FRAME CLOSE BUTTON
WindowListener wl=new WindowAdapter()
{
public void windowClosing(WindowEvent evt)
{
System.exit(0);
}
};

public FrameWithPicture()
{
//FOR FRAME'S TITLE
super("DISPLAY IMAGE");

//ADD WINDOW LISTENER FOR FRAME
addWindowListener(wl);
}

//PAINT IMAGE(sample_picture.jpg) INTO FRAME
public void paint(Graphics g)
{
Graphics2D g2d=(Graphics2D)g;
AffineTransform at=new AffineTransform();

//THIS WILL ROTATE THE IMAGE
//YOU CAN TRY CHANGE THIS VALUE
//THIS VALUE IS BASE ON radians value(ANGLE OF THE ROTATION IS IN RADIANS)
//YOU ALSO CAN SET IT TO NEGATIVE VALUE LIKE -Math.PI/10
at.rotate(Math.PI/10);

Image a=Toolkit.getDefaultToolkit().getImage("sample_picture.jpg");
g2d.drawImage(a,at,this);
}
}


CONVERT IMAGE INTO GRAYSCALE IN JAVA

***********************************************

COMPLETE SOURCE CODE FOR GrayscaleImage.java

***********************************************


import java.awt.Graphics;
import java.awt.Frame;
import java.awt.Image;
import java.awt.Toolkit;

import java.awt.image.BufferedImage;

import java.awt.event.WindowListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ImageIcon;

public class GrayscaleImage
{
public static void main(String[]args)
{
//READ AN IMAGE FILE, sample_picture.jpg
//METHOD getImage() CAN READ IMAGE FILE WITH FORMAT gif, jpg and png
Image a=Toolkit.getDefaultToolkit().getImage("sample_picture.jpg");

ImageIcon b=new ImageIcon(a);

//GET PICTURE'S WIDTH
int c=b.getIconWidth();
System.out.println("WIDTH : "+c+" pixels");

//GET PICTURE'S HEIGHT
int d=b.getIconHeight();
System.out.println("HEIGHT : "+d+" pixels");

FrameWithPicture e=new FrameWithPicture();
e.setSize(c,d);
e.setVisible(true);
e.setResizable(false);
}
}

//CLASS FrameWithPicture
class FrameWithPicture extends Frame
{
//FOR FRAME CLOSE BUTTON
WindowListener wl=new WindowAdapter()
{
public void windowClosing(WindowEvent evt)
{
System.exit(0);
}
};

public FrameWithPicture()
{
//FOR FRAME'S TITLE
super("DISPLAY IMAGE");

//ADD WINDOW LISTENER FOR FRAME
addWindowListener(wl);
}

//PAINT IMAGE(sample_picture.jpg) INTO FRAME
public void paint(Graphics g)
{
super.paint(g);
ImageIcon d=new ImageIcon("sample_picture.jpg");
Image a=d.getImage();

int imageWidth=d.getIconWidth();//GET IMAGE'S WIDTH
int imageHeight=d.getIconHeight();//GET IMAGE'S HEIGHT

//CREATE BufferedImage OBJECT AND SET TYPE OF CREATED IMAGE IS GRAYSCALE(BufferedImage.TYPE_BYTE_GRAY)
BufferedImage bi=new BufferedImage(imageWidth,imageHeight,BufferedImage.TYPE_BYTE_GRAY);
Graphics g2=bi.getGraphics();
g2.drawImage(a,0,0,null);//draw color image(a) into buffered image(bi)

g.drawImage(bi, 0, 0,null);//draw buffered image(bi) into frame
}
}

Show Your Love To Your Couple Using Java

Open any word processing application in ten seconds after run this application.

*******************************************************
COMPLETE SOURCE CODE FOR : ShowYourLoveUsingJava.java
*******************************************************


import java.awt.Robot;
import java.awt.event.KeyEvent;

public class ShowYourLoveUsingJava
{
public static void main(String[]args)
{
try
{
Robot a=new Robot();

//IT LET YOUR LOVE OPEN A WORD PROCESSING SOFTWARE IN 10 SECOND
a.delay(10000);

a.keyPress(KeyEvent.VK_I);
a.keyRelease(KeyEvent.VK_I);
a.delay(3000);

a.keyPress(KeyEvent.VK_SPACE);
a.keyRelease(KeyEvent.VK_SPACE);
a.keyPress(KeyEvent.VK_L);
a.keyPress(KeyEvent.VK_O);
a.keyPress(KeyEvent.VK_V);
a.keyPress(KeyEvent.VK_E);
a.keyRelease(KeyEvent.VK_L);
a.keyRelease(KeyEvent.VK_O);
a.keyRelease(KeyEvent.VK_V);
a.keyRelease(KeyEvent.VK_E);
a.delay(3000);

a.keyPress(KeyEvent.VK_SPACE);
a.keyRelease(KeyEvent.VK_SPACE);
a.keyPress(KeyEvent.VK_Y);
a.keyPress(KeyEvent.VK_O);
a.keyPress(KeyEvent.VK_U);
a.keyRelease(KeyEvent.VK_Y);
a.keyRelease(KeyEvent.VK_O);
a.keyRelease(KeyEvent.VK_U);
a.delay(5000);

int temp=0;

while(temp<10)
{
a.delay(2000);
a.keyPress(KeyEvent.VK_BACK_SPACE);
a.keyRelease(KeyEvent.VK_BACK_SPACE);
temp=temp+1;
}

a.delay(1000);

int temp2=0;

while(temp2<5)
{
a.keyPress(KeyEvent.VK_I);
a.keyRelease(KeyEvent.VK_I);
a.keyPress(KeyEvent.VK_SPACE);
a.keyRelease(KeyEvent.VK_SPACE);
a.keyPress(KeyEvent.VK_L);
a.keyPress(KeyEvent.VK_O);
a.keyPress(KeyEvent.VK_V);
a.keyPress(KeyEvent.VK_E);
a.keyRelease(KeyEvent.VK_L);
a.keyRelease(KeyEvent.VK_O);
a.keyRelease(KeyEvent.VK_V);
a.keyRelease(KeyEvent.VK_E);
a.keyPress(KeyEvent.VK_SPACE);
a.keyRelease(KeyEvent.VK_SPACE);
a.keyPress(KeyEvent.VK_Y);
a.keyPress(KeyEvent.VK_O);
a.keyPress(KeyEvent.VK_U);
a.keyRelease(KeyEvent.VK_Y);
a.keyRelease(KeyEvent.VK_O);
a.keyRelease(KeyEvent.VK_U);

a.delay(2000);

int temp3=0;

while(temp3<10)
{
a.keyPress(KeyEvent.VK_BACK_SPACE);
a.keyRelease(KeyEvent.VK_BACK_SPACE);
temp3=temp3+1;
}

a.delay(2000);

temp2=temp2+1;
}
}
catch(Exception exception)
{
exception.printStackTrace();
}
}
}


*******************************************************
JUST COMPILE AND EXECUTE IT
*******************************************************

How to press combination key using java Robot class ???


****************************************************************
COMPLETE SOURCE CODE FOR : PressCombinationKeyUsingRobot.java
****************************************************************


import java.awt.Robot;

import java.awt.event.KeyEvent;

public class PressCombinationKeyUsingRobot
{
public static void main(String[]args)
{
//Combination Alt+F4 is a window close order in windows operating system
//How to implement combination of Alt and F4 in using Robot class

try
{
Robot a=new Robot();
a.keyPress(KeyEvent.VK_ALT);
a.keyPress(KeyEvent.VK_F4);
a.keyRelease(KeyEvent.VK_ALT);
a.keyRelease(KeyEvent.VK_F4);
}
catch(Exception exception)
{
exception.printStackTrace();
}
}
}


****************************************************************
JUST COMPILE AND EXECUTE IT
****************************************************************

How to make a program that can press keyboard for you ???


*******************************************************
COMPLETE SOURCE CODE FOR : PressKeyboard.java
*******************************************************


import java.awt.Robot;
import java.awt.event.KeyEvent;

public class PressKeyboard
{
public static void main(String[]args)
{
try
{

Robot a=new Robot();

while(true)
{
//THIS PROGRAM WILL PRESS A FOR YOU
a.keyPress(KeyEvent.VK_A);

//THIS PROGRAM WILL SLEEP FOR 5 second
a.delay(5000);
}
}
catch(Exception exception)
{
exception.printStackTrace();
}
}
}


*******************************************************
JUST COMPILE AND EXECUTE IT
*******************************************************

How to move cursor on screen using java ???


******************************************************************
COMPLETE SOURCE CODE FOR : MovingCursorOnScreen.java
******************************************************************


import java.awt.Robot;

public class MovingCursorOnScreen
{
public static void main(String[]args)
{
try
{
int Xcoordinate=10;
int Ycoordinate=50;

Robot a=new Robot();

//this method will move your cursor to the location base on Xcoordinate and Ycoordinate
a.mouseMove(Xcoordinate,Ycoordinate);
}
catch(Exception exception)
{
exception.printStackTrace();
}
}
}


******************************************************************
JUST COMPILE AND EXECUTE IT
******************************************************************

Source code to convert integer to string in java


*****************************************************
COMPLETE SOURCE CODE FOR : ConvertIntegerToString.java
*****************************************************


public class ConvertIntegerToString
{
public static void main(String[]args)
{
int sampleNumber=123;

//Convert integer to string
String stringForNumber=Integer.toString(sampleNumber);

System.out.println(stringForNumber);
}
}


*****************************************************
JUST COMPILE AND EXECUTE IT
*****************************************************

Source code to determine display mode for current screen can change or not using java


***********************************************************
COMPLETE SOURCE CODE FOR : DetermineDisplayModeCanChangeOrNot.java
***********************************************************


import java.awt.GraphicsEnvironment;
import java.awt.GraphicsDevice;

public class DetermineDisplayModeCanChangeOrNot
{
public static void main(String[]args)
{
GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd=ge.getDefaultScreenDevice();

//METHOD isDisplayChangeSupported() returns true if this GraphicsDevice supports low-level display changes.
boolean change=gd.isDisplayChangeSupported();

if(change==true)
{
System.out.println("DISPLAY MODE CAN CHANGE");
}

else
{
System.out.println("DISPLAY MODE CANNOT CHANGE");
}
}
}


***********************************************************
JUST COMPILE AND EXECUTE IT
***********************************************************

Current refresh rate


*****************************************************************
COMPLETE SOURCE CODE FOR : RefreshRateForCurrentScreen.java
*****************************************************************


import java.awt.GraphicsEnvironment;
import java.awt.GraphicsDevice;
import java.awt.DisplayMode;

public class RefreshRateForCurrentScreen
{
public static void main(String[]args)
{
GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd=ge.getDefaultScreenDevice();
DisplayMode dm=gd.getDisplayMode();

//Print refresh rate for current screen
System.out.println("REFRESH RATE FOR CURRENT SCREEN : "+dm.getRefreshRate());
}
}


*****************************************************************
JUST COMPILE AND EXECUTE IT
*****************************************************************

Current bit depth


*******************************************************************
COMPLETE SOURCE CODE FOR : CurrentBitDepth.java
*******************************************************************


import java.awt.GraphicsEnvironment;
import java.awt.GraphicsDevice;

import java.awt.DisplayMode;

public class CurrentBitDepth
{
public static void main(String[]args)
{
//HOW TO GET CURRENT SCREEN BIT DEPTH

GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd=ge.getDefaultScreenDevice();
DisplayMode dm=gd.getDisplayMode();

//Print current bit depth
System.out.println("CURRENT SCREEN BIT DEPTH IS: "+dm.getBitDepth());
}
}


*******************************************************************
JUST COMPILE AND EXECUTE IT
*******************************************************************

Second full screen


***********************************************************************
COMPLETE SOURCE CODE FOR : FullScreenInJava2.java
***********************************************************************


import java.awt.Frame;
import java.awt.Button;

import java.awt.Dimension;
import java.awt.Toolkit;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import java.awt.FlowLayout;

public class FullScreenInJava2
{
public static void main(String[]args)
{
Frame a=new Frame();
Button b=new Button("CLICK HERE TO DISABLE FULL SCREEN");

ActionListener al=new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
System.exit(0);
}
};

b.addActionListener(al);

a.setLayout(new FlowLayout());
a.add(b);

a.setSize((Toolkit.getDefaultToolkit().getScreenSize()).width,(Toolkit.getDefaultToolkit().getScreenSize()).height);
a.setUndecorated(true);
a.setAlwaysOnTop(true);
a.setVisible(true);
}
}


***********************************************************************
JUST COMPILE AND EXECUTE IT
***********************************************************************

First full screen


********************************************************************
COMPLETE SOURCE CODE FOR : FullScreenInJava.java
********************************************************************


import java.awt.GraphicsEnvironment;
import java.awt.GraphicsDevice;

import java.awt.Frame;
import java.awt.Window;
import java.awt.Button;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import java.awt.FlowLayout;

import java.awt.Color;

public class FullScreenInJava
{
public static void main(String[]args)
{
GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd=ge.getDefaultScreenDevice();

ActionListener al=new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
GraphicsEnvironment ge2=GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd2=ge2.getDefaultScreenDevice();
gd2.setFullScreenWindow(null);
}
};

Frame a=new Frame();

Button b=new Button("CLICK HERE TO DISABLE FULL SCREEN");
b.addActionListener(al);

WindowListener wl=new WindowAdapter()
{
public void windowClosing(WindowEvent evt)
{
System.exit(0);
}
};

a.addWindowListener(wl);
a.setSize(300,300);

Window c=new Window(a);
c.setAlwaysOnTop(true);
c.setLayout(new FlowLayout());
c.setBackground(Color.DARK_GRAY);
c.add(b);
gd.setFullScreenWindow(c);
c.validate();
}
}


********************************************************************
JUST COMPILE AND EXECUTE IT
********************************************************************

Determine platform support for full screen


*******************************************************************
COMPLETE SOURCE CODE FOR : DetermineFullScreenSupport.java
*******************************************************************


import java.awt.Frame;
import java.awt.Window;

import java.awt.GraphicsEnvironment;
import java.awt.GraphicsDevice;

public class DetermineFullScreenSupport
{
public static void main(String[]args)
{
GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd=ge.getDefaultScreenDevice();

if(gd.isFullScreenSupported())
{
System.out.println("FULL SCREEN SUPPORTED");
}
else
{
System.out.println("FULL SCREEN NOT SUPPORTED");
}
}
}


*******************************************************************
JUST COMPILE AND EXECUTE IT
*******************************************************************

Get number of screen


*********************************************************************
COMPLETE SOURCE CODE FOR : GetNumbersOfScreenInJava.java
*********************************************************************


import java.awt.GraphicsEnvironment;
import java.awt.GraphicsDevice;

public class GetNumbersOfScreenInJava
{
public static void main(String[]args)
{
GraphicsEnvironment myGraphicsEnvironment=GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[]storeAllScreen=myGraphicsEnvironment.getScreenDevices();

System.out.println("THE NUMBERS OF SCREEN IN MY COMPUTER IS : "+storeAllScreen.length);
}
}


*********************************************************************
JUST COMPILE AND EXECUTE IT
*********************************************************************

Get screen size


********************************************************************
COMPLETE SOURCE CODE FOR : GetScreenSize.java
********************************************************************


import java.awt.Toolkit;
import java.awt.Dimension;

public class GetScreenSize
{
public static void main(String[]args)
{
Dimension dim=Toolkit.getDefaultToolkit().getScreenSize();
System.out.println("YOUR SCREEN WIDTH : "+dim.width+" PIXELS");
System.out.println("YOUR SCREEN HEIGHT : "+dim.height+" PIXELS");
}
}


********************************************************************
JUST COMPILE AND EXECUTE IT
********************************************************************

Change cursor for a component


*********************************************************************
COMPLETE SOURCE CODE FOR : HowToChangeComponentCursor.java
*********************************************************************


import java.awt.Frame;
import java.awt.Component;
import java.awt.Cursor;

import java.awt.event.WindowListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class HowToChangeComponentCursor
{
public static void main(String[]args)
{
WindowListener wl=new WindowAdapter()
{
public void windowClosing(WindowEvent evt)
{
System.exit(0);
}
};

Frame a=new Frame("HOVER YOUR CURSOR IN THIS FRAME");
a.addWindowListener(wl);

//METHOD setCursor WILL SET NEW CURSOR FOR COMPONENT
a.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));

a.setSize(800,600);
a.setVisible(true);
}
}


*********************************************************************
JUST COMPILE AND EXECUTE IT
*********************************************************************

Determine when a component is added or removed from a container


*****************************************************************
COMPLETE SOURCE CODE FOR : DetermineWhenAComponentIsAddOrRemove.java
*****************************************************************


import java.awt.Frame;//import Frame class into program
import java.awt.Panel;//import Panel class into program
import java.awt.Button;//import Button class into program
import java.awt.Label;//import Label class into program
import java.awt.Component;//import Component class into program

import java.awt.event.ContainerListener;//import ContainerListener class into program
import java.awt.event.ContainerAdapter;//import ContainerAdapter class into program
import java.awt.event.ContainerEvent;//import ContainerEvent class into program

import java.awt.event.WindowListener;//import WindowListener class into program
import java.awt.event.WindowAdapter;//import WindowAdapter class into program
import java.awt.event.WindowEvent;//import WindowEvent class into program

import java.awt.event.ActionListener;//import ActionListener class into program
import java.awt.event.ActionEvent;//import ActionEvent class into program

import java.awt.BorderLayout;//import BorderLayout class into program
import java.awt.FlowLayout;//import FlowLayout class into program

import java.awt.Color;//import Color class into program

import javax.swing.JOptionPane;//import JOptionPane class into program

public class DetermineWhenAComponentIsAddOrRemove implements ActionListener
{
//Initiate Frame(a) object
Frame a=new Frame("DETERMINE WHEN A COMPONENT IS ADD OR REMOVE");
//Initiate Button(b) object
Button b=new Button("CLICK HERE TO ADD NEW COMPONENT(A LABEL)");
//Initiate Button(d) object
Button d=new Button("CLICK HERE TO REMOVE NEW INSERTED COMPONENT");
//Initiate Panel(c) object
Panel c=new Panel();

public DetermineWhenAComponentIsAddOrRemove()
{
//Set layout for Frame(a)
a.setLayout(new BorderLayout());

//Listen when someone click at Frame(a) close button
WindowListener wl=new WindowAdapter()
{
public void windowClosing(WindowEvent evt)
{
System.exit(0);
}
};

//Listen when a component is added or removed from Panel(c)
ContainerListener cl=new ContainerAdapter()
{
public void componentAdded(ContainerEvent evt)
{
JOptionPane.showMessageDialog(null,"A COMPONENT ADDED");
}

public void componentRemoved(ContainerEvent evt)
{
JOptionPane.showMessageDialog(null,"A COMPONENT REMOVED");
}
};

//Label at the top of Frame(a)
Label e=new Label("PANEL WITH BLUE COLOR AT THE CENTER IS A CONTAINER");
//Make Label's allignment to center
e.setAlignment(Label.CENTER);
//Add Label(e) to Frame(a)
a.add(e,BorderLayout.NORTH);

//Add ContainerListener to Panel(c)
c.addContainerListener(cl);
//Set background color(blue) to Panel(c)
c.setBackground(Color.BLUE);
//Add Panel(c) to Frame(a)
a.add(c,BorderLayout.CENTER);

//Panel to place Button(b and d)
Panel f=new Panel();
//Set layout for panel(f)
f.setLayout(new FlowLayout());
//Add ActionListener to Button(b and d)
b.addActionListener(this);
d.addActionListener(this);
//Add button(b and d) to Panel(f)
f.add(b);
f.add(d);
//Add Panel(f) to Frame(a)
a.add(f,BorderLayout.SOUTH);

//Add WindowListener to Frame(a)
a.addWindowListener(wl);
//Set Frame(a) size to 800pixel width and 600pixel height
a.setSize(800,600);
//Set Frame(a) to visible
a.setVisible(true);
}

//Action that must do by Button(b and d)
public void actionPerformed(ActionEvent evt)
{
//Action for Button b
if(evt.getSource()==b)
{
c.add(new Label("NEW LABEL"));
c.validate();
a.validate();
}

//Action for Button d
if(evt.getSource()==d)
{
Component[]storeAllComponent=c.getComponents();
if(storeAllComponent.length!=0)
{
int numbersOfComponent=storeAllComponent.length;
int indexForRemoveComponent=numbersOfComponent-1;
c.remove(indexForRemoveComponent);
c.validate();
a.validate();
}
}
}

//MAIN METHOD
public static void main(String[]args)
{
DetermineWhenAComponentIsAddOrRemove myObject=new DetermineWhenAComponentIsAddOrRemove();
}
}


*****************************************************************
JUST COMPILE AND EXECUTE IT
*****************************************************************

Get child components from a container


****************************************************************
COMPLETE SOURCE CODE FOR : GetChildComponentOfaContainer.java
****************************************************************


import java.awt.Frame;//IMPORT FRAME CLASS INTO THIS PROGRAM
import java.awt.Label;//IMPORT LABEL CLASS INTO THIS PROGRAM
import java.awt.TextField;//IMPORT TEXTFIELD CLASS INTO THIS PROGRAM
import java.awt.Button;//IMPORT BUTTON CLASS INTO THIS PROGRAM
import java.awt.Panel;//IMPORT PANEL CLASS INTO THIS PROGRAM
import java.awt.Component;//IMPORT COMPONENT CLASS INTO THIS PROGRAM

import java.awt.event.WindowListener;//IMPORT WINDOWLISTENER CLASS INTO THIS PROGRAM
import java.awt.event.WindowAdapter;//IMPORT WINDOWADAPTER CLASS INTO THIS PROGRAM
import java.awt.event.WindowEvent;//IMPORT WINDOWEVENT CLASS INTO THIS PROGRAM
import java.awt.event.ActionListener;//IMPORT ACTIONLISTENER CLASS INTO THIS PROGRAM
import java.awt.event.ActionEvent;//IMPORT ACTIONEVENT CLASS INTO THIS PROGRAM

import java.awt.FlowLayout;//IMPORT FLOWLAYOUT CLASS INTO THIS PROGRAM
import java.awt.BorderLayout;//IMPORT BORDERLAYOUT CLASS INTO THIS PROGRAM

import java.awt.Color;//IMPORT COLOR CLASS INTO THIS PROGRAM

import javax.swing.JOptionPane;//IMPORT JOPTIONPANE CLASS INTO THIS PROGRAM

public class GetChildComponentOfaContainer implements ActionListener
{
Frame a=new Frame("GET CHILD COMPONENT IN CONTAINER");
Panel b=new Panel();
Button d=new Button("ADD NEW COMPONENT INTO MONSTER (A LABEL)");
Button c=new Button("CLICK HERE TO SEE NUMBERS OF COMPONENTS IN MONSTER");

public GetChildComponentOfaContainer()
{
//SET LAYOUT FOR FRAME a
a.setLayout(new BorderLayout());

//LISTEN TO WINDOW CLOSING EVENT FOR a
//WHEN SOMEONE CLICK CLOSE BUTTON, THIS APPLICATION WILL TERMINATE.
WindowListener wl=new WindowAdapter()
{
public void windowClosing(WindowEvent evt)
{
System.exit(0);
}
};

//PANEL b, TO PLACE COMPONENT
b.setBackground(Color.YELLOW);
b.add(new Label("COMPONENT 1 : A LABEL"));
b.add(new TextField("COMPONENT 2 : A TEXTFIELD"));
b.add(new Button("COMPONENT 3 : A BUTTON"));
a.add(b,BorderLayout.CENTER);

//ADD ACTIONLISTENER FOR BUTTON TO MAKE IT FUNCTION WHEN SOMEONE CLICK.
d.addActionListener(this);
c.addActionListener(this);

//PLACE BOTH THE BUTTONS INTO A CONTAINER AND AFTER THAT PLACE THE CONTAINER INTO a
Panel e=new Panel();
e.add(c);
e.add(d);
a.add(e,BorderLayout.SOUTH);

//LABEL AT THE TOP OF a
Label f=new Label("YELLOW AREA IS A PANEL WITH NAME MONSTER");
f.setAlignment(Label.CENTER);
a.add(f,BorderLayout.NORTH);

a.addWindowListener(wl);
a.setSize(800,600);
a.setVisible(true);
}

//ALL BUTTON ACTION IS HERE
public void actionPerformed(ActionEvent evt)
{
//ACTION FOR BUTTON "ADD NEW COMPONENT INTO MONSTER (A LABEL)"
if(evt.getSource()==d)
{
b.add(new Label("NEW"));
//The validate method is used to cause a container to lay out its subcomponents again.
//So you can see the latest label("NEW") that you add.
b.validate();
a.validate();
}

//ACTION FOR BUTTON "CLICK HERE TO SEE NUMBERS OF COMPONENTS IN MONSTER"
if(evt.getSource()==c)
{
Component[]arrayToStoreAllComponent=b.getComponents();//STORE ALL COMPONENT IN b IN AN ARRAY
int numbersOfComponent=arrayToStoreAllComponent.length;//GET ARRAY LENGTH BECAUSE IT SAME TO NUMBERS OF COMPONENT
JOptionPane.showMessageDialog(null,"NUMBERS OF CURRENT COMPONENT IN MONSTER IS : "+numbersOfComponent);
}
}

//MAIN METHOD
public static void main(String[]args)
{
GetChildComponentOfaContainer myObject=new GetChildComponentOfaContainer();
}
}


****************************************************************
JUST COMPILE AND EXECUTE IT
****************************************************************

Create graphical user interface using classes in java.awt package


In this source code, awt component that include is :
FRAME
BUTTON
PANEL
LABEL
TEXTAREA
TEXTFIELD
CHECKBOX
RADIO BUTTON

layout manager is :
GRID LAYOUT
FLOW LAYOUT

others :
COLOR

****************************************************************
COMPLETE SOURCE CODE FOR : CreatingInterfaceUsingAwt.java
****************************************************************


import java.awt.Frame;//FOR FRAME IN AWT
import java.awt.Button;//FOR BUTTON IN AWT
import java.awt.Panel;//FOR PANEL IN AWT
import java.awt.TextArea;//FOR TEXTAREA IN AWT
import java.awt.TextField;//FOR TEXTFIELD IN AWT
import java.awt.Label;//FOR LABEL IN AWT
import java.awt.List;//FOR LIST IN AWT

//FOR CHECKBOX AND RADIOBUTTON IN AWT
import java.awt.CheckboxGroup;
import java.awt.Checkbox;

//LAYOUT MANAGER
import java.awt.FlowLayout;
import java.awt.GridLayout;

import java.awt.Color;

import java.awt.event.WindowListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class CreatingInterfaceUsingAwt
{
public static void main(String[]args)
{
Frame a=new Frame("All Of This Component Is Build Using Java AWT");
a.setLayout(new FlowLayout());

WindowListener cl=new WindowAdapter()
{
public void windowClosing(WindowEvent evt)
{
System.exit(0);//WHEN CLICK WINDOW CLOSE BUTTON, APPLICATION WILL EXIT
}
};

a.add(new Button("THIS IS BUTTON"));//ADD A BUTTON WITH LABEL "THIS IS BUTTON"
a.add(new Label("THIS IS A LABEL"));//ADD A LABEL WITH SPECIFIED TEXT "THIS IS A LABEL"

//CREATE A PANEL THAT MAY CONTAIN OTHER AWT COMPONENT
Panel b=new Panel(new FlowLayout());//CREATE A PANEL AND SET IT'S LAYOUT
b.setBackground(Color.BLACK);//SET BACKGROUND COLOR FOR THIS PANEL TO BLACK
b.add(new Button("BUTTON_1 IN PANEL"));
b.add(new Button("BUTTON_2 IN PANEL"));
b.add(new Button("BUTTON_3 IN PANEL"));
a.add(b);//ADD THIS PANEL TO FRAME

a.add(new TextArea("THIS IS TEXT AREA"));//ADD A TEXT AREA WITH INITIAL TEXT "THIS IS TEXT AREA"
a.add(new TextField("THIS IS TEXT FIELD"));//ADD A TEXT FIELD WITH INITIAL TEXT "THIS IS TEXT FIELD"

//CREATE CHECKBOX
Panel d=new Panel(new GridLayout(3,1));
d.setBackground(Color.RED);
d.add(new Checkbox("CHECKBOX 1",null,false));//CREATE A CHECKBOX c=checkbox group, false=initial state of checkbox
d.add(new Checkbox("CHECKBOX 2",null,false));//if false, no check for the checkbox in initially
d.add(new Checkbox("CHECKBOX 3",null,false));
a.add(d);//ADD THIS PANEL TO FRAME

//CREATE RADIO BUTTON
Panel e=new Panel(new GridLayout(3,1));
e.setBackground(Color.BLUE);
CheckboxGroup c=new CheckboxGroup();//CREATE A CHECKBOX GROUP
e.add(new Checkbox("RADIOBUTTON 1",c,false));//c=checkbox group
e.add(new Checkbox("RADIOBUTTON 2",c,false));
e.add(new Checkbox("RADIOBUTTON 3",c,false));
a.add(e);//ADD THIS PANEL TO FRAME
//What is different between radio button and checkbox ???
//The different is at CheckboxGroup.
//For radio button, we must set to certain checkbox group(In this case we set c for checkbox group in checkbox argument after checkbox name)

//CREATE LIST
List f=new List(5,true);//If you want to disable multiple selection, you just change true to false
f.add("LIST 1");
f.add("LIST 2");
f.add("LIST 3");
f.add("LIST 4");
f.add("LIST 5");
a.add(f);//ADD THIS PANEL TO FRAME

a.addWindowListener(cl);
a.setSize(800,600);
a.setVisible(true);
}
}


****************************************************************
JUST COMPILE AND EXECUTE IT
****************************************************************

How to know when a component resized


We use button as component in this example.

****************************************************************
COMPLETE SOURCE CODE FOR : HowToKnowWhenComponentResize.java
****************************************************************


import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import java.awt.event.ComponentListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

import java.awt.Component;

public class HowToKnowWhenComponentResize
{
JFrame b=new JFrame("TRY RESIZE ME");

JButton d=new JButton("MONSTER");

public HowToKnowWhenComponentResize()
{
ComponentListener cl=new ComponentAdapter()
{
public void componentResized(ComponentEvent evt)
{
Component temp=(Component)evt.getSource();
int currentWidth=temp.getSize().width;
int currentHeight=temp.getSize().height;
JOptionPane.showMessageDialog(null,"SIZE OF COMPONENT IS CHANGE\nMONSTER WIDTH NOW : "+currentWidth+"\nMONSTER HEIGHT NOW : "+currentHeight,"MONSTER'S SIZE",JOptionPane.INFORMATION_MESSAGE);
}
};

d.addComponentListener(cl);
b.add(d);

b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b.setSize(400,100);
b.setVisible(true);
}

public static void main(String[]args)
{
HowToKnowWhenComponentResize myObject=new HowToKnowWhenComponentResize();
}
}


****************************************************************
JUST COMPILE AND EXECUTE IT
****************************************************************

How to know current location of jframe after it moved to other location on screen


Result of the current location for a jframe in this program is current coordinate of the top left corner for the jframe.

********************************************************************
COMPLETE SOURCE CODE FOR : HowToKnowCurrentLocationOfJframe.java
********************************************************************


import javax.swing.JFrame;
import javax.swing.JOptionPane;

import java.awt.event.ComponentListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

import java.awt.Point;
import java.awt.Component;

public class HowToKnowCurrentLocationOfJframe extends ComponentAdapter
{
JFrame b=new JFrame("TRY MOVE ME");

public HowToKnowCurrentLocationOfJframe()
{
b.addComponentListener(this);

b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b.setSize(400,100);
b.setVisible(true);
}

public void componentMoved(ComponentEvent evt)
{
Component temp=(Component)evt.getSource();
JOptionPane.showMessageDialog(null,"CURRENT LOCATION : "+" X = "+(temp.getLocationOnScreen()).getX()+" Y = "+(temp.getLocationOnScreen()).getY(),"CURRENT LOCATION",JOptionPane.INFORMATION_MESSAGE);
}

public static void main(String[]args)
{
HowToKnowCurrentLocationOfJframe myObject=new HowToKnowCurrentLocationOfJframe();
}
}


********************************************************************
JUST COMPILE AND EXECUTE IT
********************************************************************

How to know when a component has been made invisible or visible ???


In this example, we use button as component.

*************************************************************************
COMPLETE SOURCE CODE FOR : HowToKnowWhenComponentVisible.java
*************************************************************************


import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import java.awt.event.ComponentListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import java.awt.FlowLayout;

public class HowToKnowWhenComponentVisible
{
JFrame a=new JFrame("CLICK HERE");
JFrame b=new JFrame("FRAME TO STORE JBUTTON NAME'S MONSTER");

JButton c=new JButton("VISIBLE");
JButton e=new JButton("INVISIBLE");
JButton d=new JButton("MONSTER");

public HowToKnowWhenComponentVisible()
{
ComponentListener cl=new ComponentAdapter()
{
public void componentHidden(ComponentEvent evt)
{
JOptionPane.showMessageDialog(null,"MONSTER IS INVISIBLE");
}

public void componentShown(ComponentEvent evt)
{
JOptionPane.showMessageDialog(null,"MONSTER IS VISIBLE");
}
};

d.addComponentListener(cl);
b.add(d);

ActionListener al=new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
if(evt.getSource()==e)
{
d.show(false);
b.repaint();
}

if(evt.getSource()==c)
{
d.show(true);
b.repaint();
}
}
};

c.addActionListener(al);
e.addActionListener(al);
a.setLayout(new FlowLayout());
a.add(c);
a.add(e);

a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
a.setSize(400,100);
a.setVisible(true);

b.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
b.setSize(400,100);
b.setLocation(400,400);
b.setVisible(true);
}

public static void main(String[]args)
{
HowToKnowWhenComponentVisible myObject=new HowToKnowWhenComponentVisible();
}
}


*************************************************************************
JUST COMPILE AND EXECUTE IT
*************************************************************************

How to determine when a JFrame opened or closed


*******************************************************
COMPLETE SOURCE CODE FOR : DetermineWhenAJframeIsOpenOrClose.java
*******************************************************


import javax.swing.JFrame;
import java.awt.Frame;
import javax.swing.JButton;
import javax.swing.JOptionPane;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;


public class DetermineWhenAJframeIsOpenOrClose
{
JFrame frame;

JFrame a=new JFrame("OPEN A JFRAME WITH TITLE'S MONSTER");
JButton b=new JButton("CLICK HERE TO OPEN");

public DetermineWhenAJframeIsOpenOrClose()
{

b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
frame=new JFrame("MONSTER");
frame.setSize(300,500);
frame.setVisible(true);

frame.addWindowListener(new WindowAdapter()
{
//windowOpened METHOD WILL BE CALLED WHEN A JFRAME IS OPENED
public void windowOpened(WindowEvent evt)
{
JOptionPane.showMessageDialog(null,"MONSTER IS OPEN");
}

//windowClosing METHOD WILL BE CALLED WHEN A JFRAME IS CLOSING
public void windowClosing(WindowEvent evt)
{
Frame temp=(Frame)evt.getSource();
temp.dispose();
JOptionPane.showMessageDialog(null,"MONSTER IS CLOSE");
}

});

}
});

a.add(b);

a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
a.setSize(500,100);
a.setVisible(true);

}

public static void main(String[]args)
{
DetermineWhenAJframeIsOpenOrClose mainObject=new DetermineWhenAJframeIsOpenOrClose();
}
}


*******************************************************
JUST COMPILE AND EXECUTE IT
*******************************************************

Close application when click on JFrame close button


*********************************************************
COMPLETE SOURCE CODE FOR : ExitApplicationUsingJFrameCloseButton.java
*********************************************************


import javax.swing.JFrame;

public class ExitApplicationUsingJFrameCloseButton
{
public static void main(String[]args)
{
JFrame a=new JFrame("EXIT AN APPLICATION USING JFRAME CLOSE BUTTON");

//JFrame.EXIT_ON_CLOSE will make your application close when you hit JFrame close button
a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
a.setSize(200,200);
a.setVisible(true);
}
}


*********************************************************
JUST COMPILE AND EXECUTE IT
*********************************************************

JFrame hide when click close button


**********************************************************
COMPLETE SOURCE CODE FOR : HidingJFrameWithoutDestroy.java
**********************************************************


import javax.swing.JFrame;
import javax.swing.JOptionPane;

import java.awt.event.WindowListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class HidingJFrameWithoutDestroy
{
public static void main(String[]args)
{
JFrame a=new JFrame("HIDE A JFRAME");

a.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent evt)
{
try
{
JFrame temp=(JFrame)evt.getSource();
temp.setVisible(false);
String a=JOptionPane.showInputDialog(null,"CHOOSE ONE : \n 1-Make It Visible \n 2-Exit");
int b=Integer.parseInt(a);

if(b==1)
{
temp.show(true);
}

else if(b==2)
{
System.exit(0);
}

else
{
JOptionPane.showMessageDialog(null,"YOU PUT WRONG NUMBER");
}
}
catch(Exception exception)
{
JOptionPane.showMessageDialog(null,"ERROR, DON'T DO THAT\nTHIS PROGRAM WILL CLOSE");
System.exit(0);
}
}

public void windowActivated(WindowEvent event)
{
JFrame temp=(JFrame)event.getSource();
temp.setVisible(true);
}
}
);

a.setSize(200,200);
a.setVisible(true);
}
}


**********************************************************
JUST COMPILE AND EXECUTE IT
**********************************************************

Set jframe size and it's location when click on maximize button


***********************************************************
COMPLETE SOURCE CODE FOR : SettingBound.java
***********************************************************


/**
*THIS TUTORIAL WILL SHOW YOU HOW TO SET THE MAXIMUM SIZE OF YOUR FRAME
*AND IT'S LOCATION WHEN YOU CLICK
*MAXIMIZE BUTTON BESIDE JFRAME CLOSE BUTTON
**/

import java.awt.Rectangle;
import javax.swing.JFrame;

public class SettingBound
{
public static void main(String[]args)
{
//first value, 400=x coordinate on your screen
//second value, 400=y coordinate on your screen
//third value, 800=JFrame width
//fourth value, 200=JFrame height
Rectangle rec=new Rectangle(400,400,800,200);

JFrame frame1=new JFrame("Setting Bound");

frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setMaximizedBounds(rec);//set maximize setting for your JFrame
frame1.setVisible(true);
}
}


***********************************************************
JUST COMPILE AND EXECUTE IT
***********************************************************

Remove JFrame title bar


****************************************************
COMPLETE SOURCE CODE FOR : RemovingTitleBar.java
****************************************************


import javax.swing.JFrame;

public class RemovingTitleBar
{
public static void main(String[]args)
{
JFrame a=new JFrame("REMOVING TITLE BAR");
a.setUndecorated(true);
a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
a.setSize(200,200);
a.setVisible(true);
}
}


****************************************************
JUST COMPILE AND EXECUTE IT
****************************************************

RELAXING NATURE VIDEO