How to create a transparent frame in Java ?

In a GUI programming at some point the transparent windows are very much in need. The transparent frames are mostly used to show some less important content to users. It is basically a JFrame with faded color. It can also used in creating some widget in a very large application. Here is a code snippet.

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

public class TransparentDemo extends JFrame{
public TransparentDemo(){
setSize(200, 200);

/***
* To make a JFrame transparent this method needs to be set to true
* if this method is not set to true this will throw an exception
* java.awt.IllegalComponentStateException
*/

setUndecorated(true);

/***
* The Transparency of the frame is mainly done with the help of
  * class Color of the package java.awt
* there are 3 arguments of the construction first 3 args are of
  * color red, green, and blue and the
* the last 4th argument is called opacity which is an integeral
  * number raging from 0 to 100 which
* will improve the opacity from 100 to 0 means 0 will be no
  * background and 100 will be pure 
* background with color.
*/
setBackground(new Color(10,200,255,90));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String args[]){
new TransparentDemo().setVisible(true);
}
}

Type the above code in notepad or any editor and run it.

Number of Ways to create objects in Java

There are number of ways to create objects in java. But today i am going to show 4 mostly used ways to create objects in java.

The 4 ways to create object are :

    1. Using new operator
    2. Using Class.forName
    3. Cloning
    4. Deserialization
The first way is very common and mostly used by the developers community which using new operator. Almost all objects are created using this new operator. Here is the simple example code snippet to show the way.

MyObject obj = new MyObject();

The second way is using Class.forName. This is not so popular as much the first way but in some areas of codding we need this to be used. When we know the class name and the class the public default constructor then we can create the object using this way. 

MyObject obj = (MyObject)Class.forName("javaprg.coreprg.MyObject").newInstance();

The third way is using cloning where we use clone(). Generally the clone() is used to make a copy of an existing object. But here I will show you how to make a new object using clone(). See the below code snippet

MyObject myObj = new MyObject();
MyObject newObj = (MyObject) myObj.clone();

The fourth way is to create an object is using object deserialization. Well object deserialization is about creating an object form its own serialized form. Below is a small code snippet to show the process:

ObjectInputStream ois = new ObjectInputStream(anyInputStream);
MyObject obj = (MyObject)ois.readObject();

So here are the for mostly used way to create the object in java programming language.
Thanks for watching.

Run Simple DOS Command from java program

Some of us might have question that can we really run DOS command from our own java program? Yes it is true we can do that. For that we need Process class.

Below is the sample program to run the simple dir command from java program


import java.io.*;

public class RunDosCommands
{
public static void main(String args[])
{
try
{
Process p=Runtime.getRuntime().exec("cmd /c dir");
p.waitFor();
BufferedReader reader=new BufferedReader(new InputStreamReader(p.getInputStream()));
String line=reader.readLine();
while(line!=null)
{
System.out.println(line);
line=reader.readLine();
}
}
catch(IOException e1) {}
catch(InterruptedException e2) {}

System.out.println("Done");
}
}

That's it you know the drill. Type the program in your editor. Save it as RunDosCommands.java. and run the class file.

Folks, remember it will print the list of directory where the class file is running. For Ex, if the class file is running from C directory it will print all the directory list of your C drive.

Gujarati Typing in Java Application

This Example shows how to use the 3rd party font (.ttf) in our java application. This sometime need when we want to use strictly to some of the other languages like Gujarati, etc. We just need to have a .ttf(True Type Font) file which is need to be loaded by programming in our application.

import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JOptionPane;
import java.io.IOException;
import java.io.InputStream;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.awt.Font;
import java.awt.FontFormatException;

class GujaratiType extends JFrame
{
public Font font,fontUsed;
public GujaratiType()
{
try
{
InputStream myStream = new BufferedInputStream(new FileInputStream("LMG_LAX1.ttf"));
font = Font.createFont(Font.TRUETYPE_FONT, myStream);
fontUsed = font.deriveFont(Font.PLAIN,20);
}
catch(FontFormatException ex)
{
JOptionPane.showMessageDialog(GujaratiType.this,ex,"Error",JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
catch(IOException ex)
{
JOptionPane.showMessageDialog(GujaratiType.this,"System can not find the file : LMG_LAX1.TTF","Error",JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
JTextArea txt = new JTextArea();
txt.setFont(fontUsed);
JScrollPane jsp = new JScrollPane(txt);
add(jsp);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(0,0,300,300);
//setSize(300,300);
setVisible(true);
}
public static void main(String[] a)
{
new GujaratiType();
}
}

FizzBuzz Program

Following is the program for FizzBuzzProblem. It is program to print numbers from 1 to 100, but if the number is multiple of 3 then number should be replaced with "Buzz", if the number is multiple of 5 then number should be replaced with "Fizz", and if the number is multiple of both 3 and 5 then number should be replaced with "FizzBuzz".

public class FizzBuzzProblem
{
    public static void main(String args[])
    {
        for(int i = 1; i <= 100; i++)
        {
            if((i % (3*5)) == 0)
            {
                System.out.println("FizzBuzz");
            }
            else if ((i % 5) == 0)
            {
                System.out.println("Buzz");
              }
            else if ((i % 3) == 0)
            {
                System.out.println("Fizz");
            }
            else
            {
                System.out.println(i);
              }
        }
    }
}

Methods of Iteration we can perform on List

There are five methods we can iterate the List to view its data. I have wrapped up all five methods in one program.

import java.util.List;
import java.util.ListIterator;
import java.util.Iterator;
import java.util.ArrayList;
import java.util.Arrays;

public class ListDemo
{
public static void main(String[] args)
{
List al = new ArrayList();

al.add(0,100);
al.add(1,20);
al.add(2,8);
al.add(3,65);
al.add(4,102);
al.add(5,88);
al.add(6,88);

System.out.println(al+"\n");

Iterator i = al.iterator(); // First Method to iterate
while(i.hasNext())
System.out.print(i.next() + " ");

System.out.println("\n");
Object ob[] = al.toArray(); // Second Method to iterate
Arrays.sort(ob);
for(Object o : ob)
System.out.print(o + " ");

System.out.println("\n");
ListIterator li = al.listIterator();// Third Method to iterate
while(li.hasNext())
System.out.print(li.next() + " ");


System.out.println("\n\nNow in backward");// iterate previous element
System.out.println("--- -- --------");
ListIterator li1 = al.listIterator();
while(li1.hasNext())li1.next();
while(li1.hasPrevious())
System.out.print(li1.previous() + " ");

System.out.println("\n");
for(int i1 = 0 ;i1 < al.size(); i1++) //Forth Method to iterate
{
System.out.print( al.get(i1) + " " );
}

al.set(3,"pranav");
System.out.println("\n");
System.out.println(al+"\n");

}
}