public class ForActAsWhile
{
public static void main(String[]args)
{
/*
* while(true)
* {
* System.out.println("HELLO WORLD");
* }
*
*
*Now we will create for loop
*that will act like while loop at above
*/
/*
*For loop at below is an unstopable loop.
*It will act like while loop at above.
*It has no i++ at last.So (i) value is always 0.
*And 0 is less then 1.This condition is always true.
*NOTE : <<DON'T EXECUTE IT!!>>.This is only for learning.
*/
for(int i=0;i<1;)
{
System.out.println("HELLO WORLD");
}
}
}
For loop act as while loop
Java play wav file
Complete source code below will show you, how to play wav file in java.
Click here to download Star-Wars-4118.wav that will use with this code. Place this wav file same location with this source code file.
***********************************************************
COMPLETE SOURCE CODE FOR : PlayWavFile.java
***********************************************************
***********************************************************
JUST COMPILE AND EXECUTE IT
***********************************************************
Click here to download Star-Wars-4118.wav that will use with this code. Place this wav file same location with this source code file.
***********************************************************
COMPLETE SOURCE CODE FOR : PlayWavFile.java
***********************************************************
import java.io.File;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;
public class PlayWavFile
{
public static void main(String[]args)
{
String filename="Star-Wars-4118.wav";
int EXTERNAL_BUFFER_SIZE = 524288;
File soundFile = new File(filename);
if (!soundFile.exists())
{
System.err.println("Wave file not found: " + filename);
return;
}
AudioInputStream audioInputStream = null;
try
{
audioInputStream = AudioSystem.getAudioInputStream(soundFile);
}
catch(Exception e)
{
e.printStackTrace();
return;
}
AudioFormat format = audioInputStream.getFormat();
SourceDataLine auline = null;
//Describe a desired line
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
try
{
auline = (SourceDataLine) AudioSystem.getLine(info);
//Opens the line with the specified format,
//causing the line to acquire any required
//system resources and become operational.
auline.open(format);
}
catch(Exception e)
{
e.printStackTrace();
return;
}
//Allows a line to engage in data I/O
auline.start();
int nBytesRead = 0;
byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];
try
{
while (nBytesRead != -1)
{
nBytesRead = audioInputStream.read(abData, 0, abData.length);
if (nBytesRead >= 0)
{
//Writes audio data to the mixer via this source data line
//NOTE : A mixer is an audio device with one or more lines
auline.write(abData, 0, nBytesRead);
}
}
}
catch(Exception e)
{
e.printStackTrace();
return;
}
finally
{
//Drains queued data from the line
//by continuing data I/O until the
//data line's internal buffer has been emptied
auline.drain();
//Closes the line, indicating that any system
//resources in use by the line can be released
auline.close();
}
}
}
***********************************************************
JUST COMPILE AND EXECUTE IT
***********************************************************
Java invisible cursor
Complete source code below will show you, how to create invisible cursor in java.
************************************************************************
COMPLETE SOURCE CODE FOR : MakeCursorInvisible.java
************************************************************************
************************************************************************
JUST COMPILE AND EXECUTE IT
************************************************************************
************************************************************************
COMPLETE SOURCE CODE FOR : MakeCursorInvisible.java
************************************************************************
import java.awt.Toolkit;
import java.awt.Cursor;
import java.awt.Point;
import java.awt.Image;
import javax.swing.JFrame;
public class MakeCursorInvisible
{
//Create an empty byte array
byte[]imageByte=new byte[0];
Cursor myCursor;
Point myPoint=new Point(0,0);
//Create image for cursor using empty array
Image cursorImage=Toolkit.getDefaultToolkit().createImage(imageByte);
public MakeCursorInvisible()
{
//Create cursor
myCursor=Toolkit.getDefaultToolkit().createCustomCursor(cursorImage,myPoint,"cursor");
JFrame frame=new JFrame("Put your cursor into me");
//Set cursor for JFrame using created cursor(myCursor)
frame.setCursor(myCursor);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[]args)
{
MakeCursorInvisible mci=new MakeCursorInvisible();
}
}
************************************************************************
JUST COMPILE AND EXECUTE IT
************************************************************************
Set file to Read-only in java
Complete source code below will show you, how to set a file to Read-only in java.
******************************************************************
COMPLETE SOURCE CODE FOR : SetFileReadOnly.java
******************************************************************
******************************************************************
JUST COMPILE AND EXECUTE IT
******************************************************************
Click here to download MyFile.txt
Note : Put MyFile.txt with java file above at the same location.
******************************************************************
COMPLETE SOURCE CODE FOR : SetFileReadOnly.java
******************************************************************
import java.io.File;
public class SetFileReadOnly
{
public static void main(String[]args)
{
File myFile=new File("MyFile.txt");
//Set MyFile.txt to Read-only
myFile.setReadOnly();
}
}
******************************************************************
JUST COMPILE AND EXECUTE IT
******************************************************************
Click here to download MyFile.txt
Note : Put MyFile.txt with java file above at the same location.
Java palindrome checker
What is palindrome ?
-Palindrome is a word or sequence that reads the same backwards as forwards.
For example :
MADAM
***************************************************************
COMPLETE SOURCE CODE FOR : JavaPalindromeChecker.java
***************************************************************
***************************************************************
JUST COMPILE AND EXECUTE IT
***************************************************************
-Palindrome is a word or sequence that reads the same backwards as forwards.
For example :
MADAM
***************************************************************
COMPLETE SOURCE CODE FOR : JavaPalindromeChecker.java
***************************************************************
import java.util.Scanner;
import java.util.StringTokenizer;
public class JavaPalindromeChecker
{
public static void main(String[]args)
{
System.out.println("Put your text below");
Scanner sc=new Scanner(System.in);
String a=sc.nextLine();
StringTokenizer st=new StringTokenizer(a);
String b="";
while(st.hasMoreTokens())
{
b=b+st.nextToken();
}
int stringLength=b.length();
String benchMark="Palindrome";
if(stringLength%2!=0)
{
int c=((stringLength-1)/2);
for(int i=0;i<c;i++)
{
char before=b.charAt(c-(i+1));
char after=b.charAt(c+(i+1));
if(before!=after)
{
benchMark=new String("Not Palindrome");
}
}
}
else
{
int c=stringLength/2;
for(int i=0;i<c;i++)
{
char before=b.charAt(i);
char after=b.charAt(stringLength-(i+1));
if(before!=after)
{
benchMark=new String("Not Palindrome");
}
}
}
System.out.println(benchMark);
}
}
***************************************************************
JUST COMPILE AND EXECUTE IT
***************************************************************
Java transparent splash screen

Sample
**********************************************************************
TRANSPARENT IMAGE THAT WILL BE USE IN SOURCE CODE (batman.png)
Note : Make sure batman.png locate at same location with java file below.You can use other location, but for this simple tutorial, we will use same location.
*******************************************************************
COMPLETE SOURCE CODE FOR : TransparentSplashScreen.java
*******************************************************************
import javax.swing.JWindow; import javax.swing.ImageIcon; import javax.swing.JOptionPane; import java.awt.Graphics; import java.awt.Image; import java.awt.Toolkit; public class TransparentSplashScreen extends JWindow { //Get transparent image that will be use as splash screen image. Image bi=Toolkit.getDefaultToolkit().getImage("batman.png"); ImageIcon ii=new ImageIcon(bi); public TransparentSplashScreen() { try { setSize(ii.getIconWidth(),ii.getIconHeight()); setLocationRelativeTo(null); show(); Thread.sleep(10000); dispose(); JOptionPane.showMessageDialog(null,"This program will exit !!!","<>",JOptionPane.INFORMATION_MESSAGE); } catch(Exception exception) { exception.printStackTrace(); } } //Paint transparent image onto JWindow public void paint(Graphics g) { g.drawImage(bi,0,0,this); } public static void main(String[]args) { TransparentSplashScreen tss=new TransparentSplashScreen(); } }
*******************************************************************
JUST COMPILE AND EXECUTE IT
*******************************************************************
Java print screen
Complete source code below will show you, how to implement print screen or capture screen image in java. Program below will capture screen during it's executing time and after that, it will paint the image on a canvas before add it into a JFrame to display it's result.
*************************************************************
COMPLETE SOURCE CODE FOR : CaptureScreen.java
*************************************************************
*************************************************************
JUST COMPILE AND EXECUTE IT
*************************************************************
*************************************************************
COMPLETE SOURCE CODE FOR : CaptureScreen.java
*************************************************************
import java.awt.Robot;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
public class CaptureScreen extends Canvas
{
Rectangle screenRectangle=new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
Robot myRobot;
BufferedImage screenImage;
public CaptureScreen()
{
try
{
myRobot=new Robot();
}
catch(Exception exception)
{
exception.printStackTrace();
}
screenImage=myRobot.createScreenCapture(screenRectangle);
JFrame myFrame=new JFrame("Capture Screen");
myFrame.add(this);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setSize(Toolkit.getDefaultToolkit().getScreenSize().width,Toolkit.getDefaultToolkit().getScreenSize().height);
myFrame.setVisible(true);
}
public void paint(Graphics g)
{
g.drawImage(screenImage,0,0,this);
}
public static void main(String[]args)
{
CaptureScreen cs=new CaptureScreen();
}
}
*************************************************************
JUST COMPILE AND EXECUTE IT
*************************************************************
Change default metal look and feel color

*********************************************************************
COMPLETE SOURCE CODE FOR : ChangeMetalLookAndFeelColor.java
*********************************************************************
import javax.swing.plaf.metal.DefaultMetalTheme;
import javax.swing.plaf.metal.MetalLookAndFeel;
import javax.swing.plaf.ColorUIResource;
import javax.swing.*;
import java.awt.FlowLayout;
public class ChangeMetalLookAndFeelColor
{
public ChangeMetalLookAndFeelColor()
{
JFrame.setDefaultLookAndFeelDecorated(true);
MetalLookAndFeel.setCurrentTheme(new OrangeTheme());
JFrame frame=new JFrame("Orange Theme");
frame.getContentPane().setLayout(new FlowLayout());
frame.getContentPane().add(new JButton("JButton"));
frame.getContentPane().add(new JCheckBox("JCheckBox"));
frame.getContentPane().add(new JTextField("JTextField"));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[]args)
{
ChangeMetalLookAndFeelColor cmlafm=new ChangeMetalLookAndFeelColor();
}
class OrangeTheme extends DefaultMetalTheme
{
//NEW COLOR FOR METAL LOOK AND FEEL
ColorUIResource primary1=new ColorUIResource(255,215,76);
ColorUIResource primary2=new ColorUIResource(255,198,0);
ColorUIResource primary3=new ColorUIResource(205,162,11);
ColorUIResource secondary1=new ColorUIResource(255,187,57);
ColorUIResource secondary2=new ColorUIResource(255,168,0);
ColorUIResource secondary3=new ColorUIResource(255,214,104);
//NEW COLOR FOR METAL LOOK AND FEEL
protected ColorUIResource getPrimary1()
{
return primary1;
}
protected ColorUIResource getPrimary2()
{
return primary2;
}
protected ColorUIResource getPrimary3()
{
return primary3;
}
protected ColorUIResource getSecondary1()
{
return secondary1;
}
protected ColorUIResource getSecondary2()
{
return secondary2;
}
protected ColorUIResource getSecondary3()
{
return secondary3;
}
}
}
*********************************************************************
JUST COMPILE AND EXECUTE IT
*********************************************************************
Get last selected JRadioButton
Complete source code below will show you, how to get last selected JRadioButton when we click on other JRadioButton. I hope you will understand what i want to tell you, or you can try to compile and execute source code below.
******************************************************************
COMPLETE SOURCE CODE FOR : GetLastSelectedJRadioButton.java
******************************************************************
******************************************************************
JUST COMPILE AND EXECUTE IT
******************************************************************
******************************************************************
COMPLETE SOURCE CODE FOR : GetLastSelectedJRadioButton.java
******************************************************************
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class GetLastSelectedJRadioButton implements ItemListener
{
JRadioButton jrd1=new JRadioButton("a");
JRadioButton jrd2=new JRadioButton("b");
JRadioButton jrd3=new JRadioButton("c");
JRadioButton jrd4=new JRadioButton("d");
JRadioButton last=null;
ButtonGroup bg=new ButtonGroup();
JFrame myFrame=new JFrame("Get last selected JRadioButton");
public GetLastSelectedJRadioButton()
{
jrd1.addItemListener(this);
jrd2.addItemListener(this);
jrd3.addItemListener(this);
jrd4.addItemListener(this);
bg.add(jrd1);
bg.add(jrd2);
bg.add(jrd3);
bg.add(jrd4);
myFrame.getContentPane().setLayout(new GridLayout(4,1));
myFrame.getContentPane().add(jrd1);
myFrame.getContentPane().add(jrd2);
myFrame.getContentPane().add(jrd3);
myFrame.getContentPane().add(jrd4);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setSize(400,400);
myFrame.setLocationRelativeTo(null);
myFrame.setVisible(true);
}
public void itemStateChanged(ItemEvent event)
{
JRadioButton temp=(JRadioButton)event.getItem();
if(event.getStateChange()==ItemEvent.DESELECTED)
{
last=temp;
}
else if(event.getStateChange()==ItemEvent.SELECTED)
{
if(last!=null)
{
JOptionPane.showMessageDialog(myFrame,"LAST : "+last.getText(),"Last",JOptionPane.INFORMATION_MESSAGE);
}
}
}
public static void main(String[]args)
{
GetLastSelectedJRadioButton glsjrb=new GetLastSelectedJRadioButton();
}
}
******************************************************************
JUST COMPILE AND EXECUTE IT
******************************************************************
Set JFrame decoration to MetalLookAndFeel decoration

************************************************************************
COMPLETE SOURCE CODE FOR : SetJFrameDecorationToMetalLookAndFeel.java
************************************************************************
import javax.swing.*;
import java.awt.FlowLayout;
public class SetJFrameDecorationToMetalLookAndFeel
{
public static void main(String[]args)
{
//Set JFrame decoration to current look and feel decoration
//You can try to make this line as comment and
//after that try compile and execute it to see it's differential
JFrame.setDefaultLookAndFeelDecorated(true);
JButton myButton=new JButton("Click On Me");
JFrame myFrame=new JFrame("Set JFrame window decoration to current look and feel");
myFrame.setLayout(new FlowLayout());
myFrame.add(myButton);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setSize(400,400);
myFrame.setLocationRelativeTo(null);
myFrame.setVisible(true);
}
}
************************************************************************
JUST COMPILE AND EXECUTE IT
************************************************************************
Subscribe to:
Posts (Atom)