Tuesday, April 27, 2010

Java play wav file  

0 comments

Complete source code below will show you, how to play wav file in 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();
}
}
}

Get last selected JRadioButton  

0 comments

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.

view plainprint?
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();
}
}

Sunday, April 25, 2010

Check word in String  

0 comments

Complete source code below will show you how to check word if exist or not in a String. It will compare a word that we want to check with all word that separate by white space in targeted String. Comparison will ignore case.

view plainprint?
import java.util.StringTokenizer;

public class CheckWordInString
{
public static void main(String[]args)
{
String a="Hello World !";

/*
*Word(hello) that we want
*to check if exist
*or not in String a
*/
String b="hello";

/*
*Get all word that
*separate by white space
*in String a
*/
StringTokenizer st
=new StringTokenizer(a);

while(st.hasMoreTokens())
{
String temp=st.nextToken();

/*
*Compare with
*ignore case
*/
if(temp.equalsIgnoreCase(b))
{
System.out.println
("b exist in a");
break;
}
}
}
}

Saturday, April 24, 2010

Java get binary value from alphabet  

0 comments

view plainprint?
public class JavaGetBinaryFromLetter
{
public static void main(String[]args)
{
String a="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

for(int i=0;i {
char b=a.charAt(i);
int temp=(int)b;
String c=Integer.toBinaryString(temp);
System.out.println("BINARY VALUE FOR LETTER "+b+" IS : "+c);
}
}
}

Java ternary operator  

0 comments

public class JavaTernaryOperator
{
public static void main(String[]args)
{
int a=0;
int b=4;
int c=5;

//THIS IS EXAMPLE OF TERNARY OPERATOR
a=(b>c?b:c);

System.out.println(a);
}
}


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

In the java code above, example of ternary operator is a=(b>c?b:c);.So, what it mean ?

This is the answer :
Value a is equal to b if b larger than c or value a is equal to c if b smaller than c.So, java code above has same meaning with java code below.

::Click Here To Download This Java Source Code::




public class JavaTernaryOperator
{
public static void main(String[]args)
{
int a=0;
int b=4;
int c=5;

if(b>c)
{
a=b;
}
else
{
a=c;
}

System.out.println(a);
}
}


Now, you can choose which one you want to use when write your java program. It seem when we use ternary operator we can make our program more shorter.

Java hexadecimal to binary  

0 comments

Complete source code below is a simple java program that can be use to get binary value from hexadecimal value.


view plainprint?
import java.util.Scanner;

public class JavaHexToBin
{
public static void main(String[]args)
{
String[]hex={"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"};
String[]binary={"0000","0001","0010","0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1101","1110","1111"};

Scanner s=new Scanner(System.in);
System.out.print("Put hex value : ");
String userInput=s.next();
String result="";
for(int i=0;i {
char temp=userInput.charAt(i);
String temp2=""+temp+"";
for(int j=0;j {
if(temp2.equalsIgnoreCase(hex[j]))
{
result=result+binary[j];
}
}
}
System.out.println("IT'S BINARY IS : "+result);
}
}

Java determine pixel was clicked or not  

0 comments

view plainprint?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class JavaDeterminePixelClicked extends Canvas implements MouseListener
{
boolean [] con={ false,false,false,false,false,false,false,false };
int[][]coor=new int[8][2];
Robot r;

public JavaDeterminePixelClicked()
{
addMouseListener(this);
try
{
r=new Robot();
}
catch(Exception exception)
{
exception.printStackTrace();
}

JFrame a=new JFrame(">>>>>TRY CLICK AT BLACK DOT<<<<<");
a.getContentPane().add(this,BorderLayout.CENTER);
a.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
a.setSize(500,500);
a.setLocationRelativeTo(null);
a.setVisible(true);
}

public void paint(Graphics g)
{
setBackground( new Color (255,255,255) );
super.paint(g);
g.setColor( new Color (0,0,0) );
g.drawLine(30,30,30,30);
g.drawLine(40,40,40,40);
g.drawLine(50,50,50,50);
g.drawLine(60,60,60,60);
g.drawLine(70,70,70,70);
g.drawLine(80,80,80,80);
g.drawLine(90,90,90,90);
g.drawLine(100,100,100,100);
}

public void mouseClicked(MouseEvent event)
{
Color temp=r.getPixelColor(event.getLocationOnScreen().x,event.getLocationOnScreen().y);
if(temp.getRed()==0&&temp.getGreen()==0&&temp.getBlue()==0)
{
int upperLeftX=getLocationOnScreen().x;
int upperLeftY=getLocationOnScreen().y;
int currentX=event.getLocationOnScreen().x;
int currentY=event.getLocationOnScreen().y;
if(currentX-upperLeftX==30&¤tY-upperLeftY==30)
{
if(con[0]==false)
{
con[0]=true;
}
else
{
JOptionPane.showMessageDialog(null,"This point was clicked");
}
}
if(currentX-upperLeftX==40&¤tY-upperLeftY==40)
{
if(con[1]==false)
{
con[1]=true;
}
else
{
JOptionPane.showMessageDialog(null,"This point was clicked");
}
}
if(currentX-upperLeftX==50&¤tY-upperLeftY==50)
{
if(con[2]==false)
{
con[2]=true;
}
else
{
JOptionPane.showMessageDialog(null,"This point was clicked");
}
}
if(currentX-upperLeftX==60&¤tY-upperLeftY==60)
{
if(con[3]==false)
{
con[3]=true;
}
else
{
JOptionPane.showMessageDialog(null,"This point was clicked");
}
}
if(currentX-upperLeftX==70&¤tY-upperLeftY==70)
{
if(con[4]==false)
{
con[4]=true;
}
else
{
JOptionPane.showMessageDialog(null,"This point was clicked");
}
}
if(currentX-upperLeftX==80&¤tY-upperLeftY==80)
{
if(con[5]==false)
{
con[5]=true;
}
else
{
JOptionPane.showMessageDialog(null,"This point was clicked");
}
}
if(currentX-upperLeftX==90&¤tY-upperLeftY==90)
{
if(con[6]==false)
{
con[6]=true;
}
else
{
JOptionPane.showMessageDialog(null,"This point was clicked");
}
}
if(currentX-upperLeftX==100&¤tY-upperLeftY==100)
{
if(con[7]==false)
{
con[7]=true;
}
else
{
JOptionPane.showMessageDialog(null,"This point was clicked");
}
}
}
}

public void mouseEntered(MouseEvent event)
{
//NOTHING
}

public void mouseExited(MouseEvent event)
{
//NOTHING
}

public void mousePressed(MouseEvent event)
{
//NOTHING
}

public void mouseReleased(MouseEvent event)
{
//NOTHING
}

public static void main(String[]args)
{
JavaDeterminePixelClicked jdpc = new JavaDeterminePixelClicked ();
}
}

Java receive user input and sort it in ascending order  

0 comments

Complete source code below is a simple java program that will receive user input and after that sort it in ascending order before display it's result.


  1. import java.util.Scanner;
  2. public class AscendingString
  3. {
  4. public static void main(String[]args)
  5. {
  6. Scanner s=new Scanner(System.in);
  7. String[]a=new String[5];
  8. for(int i=0;i<5;i++)
  9. {
  10. System.out.println(i+1+".Put your String");
  11. String temp=s.nextLine();
  12. a[i]=temp;
  13. }
  14. System.out.println("**********");
  15. System.out.println("RESULT");
  16. System.out.println("**********");
  17. for(int i=0;i<5;i++)
  18. {
  19. for(int j=0;j<5;j++)
  20. {
  21. String temp=a[i];
  22. String tempB=a[j];
  23. if(temp.compareTo(tempB)<0)
  24. {
  25. a[i]=tempB;
  26. a[j]=temp;
  27. }
  28. }
  29. }
  30. for(int i=0;i<5;i++)
  31. {
  32. System.out.println(a[i]);
  33. }
  34. }
  35. }

Java recursive method  

0 comments

Sometime we need to solve a problem using the same way. For example you need to build a program that will print number from 1 until number that input by a user. First program will print number without using recursive technique and second program we will use recursive technique to print number from 1 until number that input by a user.You should see how a recursive method will do the same thing until problem solve.


  1. import java.util.Scanner;
  2. public class Test
  3. {
  4. public static void main(String[]args)
  5. {
  6. Scanner s=new Scanner(System.in);
  7. System.out.println("Put a number");
  8. int a=s.nextInt();
  9. System.out.println("IT'S RESULT");
  10. System.out.println("***************");
  11. for(int i=1;i<=a;i++)
  12. {
  13. System.out.println(i);
  14. }
  15. }
  16. }


SECOND PROGRAM :
print method in PrintNumber class is example of recursive method.


  1. import java.util.Scanner;
  2. public class Test
  3. {
  4. public static void main(String[]args)
  5. {
  6. Scanner s=new Scanner(System.in);
  7. System.out.println("Put a number");
  8. int myNumber=s.nextInt();
  9. PrintNumber pn=new PrintNumber();
  10. System.out.println("IT'S RESULT");
  11. System.out.println("***************");
  12. pn.print(myNumber);
  13. }
  14. }
  15. class PrintNumber
  16. {
  17. public void print(int a)
  18. {
  19. if(a==1)
  20. {
  21. System.out.println(a);
  22. }
  23. else
  24. {
  25. int temp=a-1;
  26. print(temp);
  27. System.out.println(a);
  28. }
  29. }
  30. }


For example if you put 2 into second program, when it enter print method in PrintNumber class, firstly program will check if the number is 1 or not. If not, the number will be minus with 1 before it send it's result to print method again. In this case it will send 1 to print method. So System.out.println(a) for a=2 will stop for a while until a=1. Now after it send 1 to print method, condition for a==1 will be true. 1 will be print, and System.out.println(a) for a=2 will continue again by print 2.

I hope it will help.

Extension Factory Builder