Showing posts with label JAVA. Show all posts
Showing posts with label JAVA. Show all posts

Monday, 21 July 2014

program to play music in JFrame

import java.io.*;
import java.net.URL;
import javax.sound.sampled.*;
import javax.swing.*;
 
// To play sound using Clip, the process need to be alive.
// Hence, we use a Swing application.
public class sounds extends JFrame {
 
   // Constructor
   public sounds() {
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      this.setTitle("Test Sound Clip");
      this.setSize(300, 200);
      this.setVisible(true);
   
      try {
   
         // Open an audio input stream.
         URL url = this.getClass().getClassLoader().getResource("mus.wav");
         //
          AudioInputStream audioIn = AudioSystem.getAudioInputStream(url);
         // Get a sound clip resource.
          for(int i=0; i<10; i++){   Clip clip = AudioSystem.getClip();
         // Open audio clip and load samples from the audio input stream.
         clip.open(audioIn);
         clip.start();}
      } catch (UnsupportedAudioFileException e) {
     JOptionPane.showMessageDialog(this, e.getMessage());
         e.printStackTrace();
      } catch (IOException e) { JOptionPane.showMessageDialog(this, e.getMessage());
         e.printStackTrace();
      } catch (LineUnavailableException e) { JOptionPane.showMessageDialog(this, e.getMessage());
         e.printStackTrace();
      }
   }
 
   public static void main(String[] args) {
      new sounds();
   }
}

Wednesday, 16 July 2014

Program to play and stop music on your choice in java applet

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class mus3 extends Applet
implements ActionListener{
   Button play,stop;
   AudioClip audioClip;
   public void init(){
      play = new Button("  Play ");
      add(play);
      play.addActionListener(this);
      stop = new Button("  Stop  ");
      add(stop);
      stop.addActionListener(this);
      audioClip = getAudioClip(getCodeBase(), "mus.wav");
   }
   public void actionPerformed(ActionEvent ae){
      Button source = (Button)ae.getSource();
      if (source.getLabel() == "  Play  "){
         audioClip.play();
      }
      else if(source.getLabel() == "  Stop  "){
         audioClip.stop();
      }
   }
}

Tuesday, 15 July 2014

Program to play music in java Applet

import java.applet.*;
import java.awt.*;

public class music1 extends Applet {
   Button play,stop;
   AudioClip audioClip;
   public void init(){
   
      audioClip = getAudioClip(getCodeBase(), "mus.wav");
      audioClip.play();
   }
 
}

Saturday, 28 June 2014

Program to show the example of radio buttons

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
public class Radio extends Applet implements ActionListener
{
Checkbox ch1,ch2;
CheckboxGroup cbg;
Button b1;

public void init()
{
cbg=new CheckboxGroup();
b1=new Button("check");
ch1= new Checkbox("male",cbg,true);
ch2= new Checkbox("female", cbg,true);
add(ch1);
add(ch2);
add(b1);
b1.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
String a=" ", b=" ", c=" ";
if(e.getSource()==b1)
{
if(ch1.getState()==true)
{a="male";}
else if(ch2.getState()==true)
{b="female";}

JOptionPane.showMessageDialog(this, "u have selected "+a+b);

}
}}

output:


Program to show two listed choices and print their table

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Mullist extends Applet implements ItemListener{
Choice ch1,ch2;
List l1;
int c,d;
public void init()
{
ch1=new Choice();
ch2=new Choice();
l1=new List(10);
for(int i=1; i<=100; i++)
{
ch1.add(String.valueOf(i));
ch2.add(String.valueOf(i));
}
add(ch1);
add(ch2);
add(l1);
ch1.addItemListener(this);
ch2.addItemListener(this);

}
public void itemStateChanged(ItemEvent e) {
if(e.getSource()==ch1)
{
c=Integer.parseInt(ch1.getSelectedItem());
}
if(e.getSource()==ch2)
{
d=Integer.parseInt(ch2.getSelectedItem());
}

for(int i=1; i<=d; i++)
{
l1.add(""+c+"x"+i+"="+c*i+"\n");
}
}

}

output:




Friday, 27 June 2014

Make your own KBC(kaun banega crorepati) game

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
public class Kbc extends Applet implements ActionListener{
Label l1,l2,l3;
Panel p1,p2,p3,p4;
CheckboxGroup c1,c2,c3;
Checkbox  ch1,ch2,ch3,ch4,ch5,ch6,ch7,ch8,ch9,ch10,ch11,ch12;
Button b1,b2,b3,b4;


public void init()
{
setLayout(null);
p1=new Panel();
p1.setBounds(100,10,800,500);
add(p1);
c1=new CheckboxGroup();
c2=new CheckboxGroup();
c3=new CheckboxGroup();
b1= new Button("lock the answer");
l1 = new Label();
l1.setText("Q1: what is the capital of india?");
p1.add(l1);
ch1= new Checkbox("delhi",c1,false);
ch2= new Checkbox("chandigarh", c1,false);
ch3= new Checkbox("manipur", c1,false);
ch4= new Checkbox("mumbai", c1,false);
p1.add(ch1);
p1.add(ch2);
p1.add(ch3);
p1.add(ch4);
p1.add(b1);
b1.addActionListener(this);
p2=new Panel();
p2.setBounds(100,10,800,500);
add(p2);
b2= new Button("lock the answer");
l2 = new Label();
l2.setText("Q2: who is the founder of facebook");
p2.add(l2);
ch5= new Checkbox("mark zukerberg",c2,false);
ch6= new Checkbox("james wat", c2,false);
ch7= new Checkbox("sherlock", c2,false);
ch8= new Checkbox("orkut", c2,false);
p2.add(ch5);
p2.add(ch6);
p2.add(ch7);
p2.add(ch8);
p2.add(b2);
b2.addActionListener(this);
p3=new Panel();
p3.setBounds(100,10,800,500);
add(p3);
b3= new Button("lock the answer");
l3 = new Label();
l3.setText("Q2: what is the capital of punjab?");
p3.add(l3);
ch9= new Checkbox("iraq",c3,false);
ch10= new Checkbox("chandigarh", c3,false);
ch11= new Checkbox("crap", c3,false);
ch12= new Checkbox("mumbai", c3,false);
p3.add(ch9);
p3.add(ch10);
p3.add(ch11);
p3.add(ch12);
p3.add(b3);
b3.addActionListener(this);

p1.setBackground(Color.blue);
p2.setBackground(Color.cyan);
p3.setBackground(Color.pink);


p4=new Panel();
p4.setBounds(100,10,800,500);
add(p4);
p4.setBackground(Color.gray);
b4=new Button("done");
p4.add(b4);
b4.addActionListener(this);
p1.setVisible(true);
p2.setVisible(false);
p3.setVisible(false);
p4.setVisible(false);
}
public void actionPerformed(ActionEvent e) {
String a=" ", b=" ";

if(e.getSource()==b1)
{
if(ch1.getState()==true)
a="right";
else if(ch2.getState()==true)
b="wrong";
else if(ch3.getState()==true)
b="wrong";
else if(ch4.getState()==true)
b="wrong";

JOptionPane.showMessageDialog(this, "u have selected "+a+b);
p1.setVisible(false);
p2.setVisible(true);
p3.setVisible(false);
p4.setVisible(false);
}
if(e.getSource()==b2)
{
if(ch5.getState()==true)
a="right";
else if(ch6.getState()==true)
b="wrong";
else if(ch7.getState()==true)
b="wrong";
else if(ch8.getState()==true)
b="wrong";
JOptionPane.showMessageDialog(this, "u have selected "+a+b);
p1.setVisible(false);
p2.setVisible(false);
p3.setVisible(true);
p4.setVisible(false);
}
if(e.getSource()==b3)
{
if(ch9.getState()==true)
b="wrong";
else if(ch10.getState()==true)
a="right";
else if(ch11.getState()==true)
b="wrong";
else if(ch12.getState()==true)
b="wrong";
JOptionPane.showMessageDialog(this,"u have selected "+a+b);
p1.setVisible(false);
p2.setVisible(false);
p3.setVisible(false);
p4.setVisible(true);
}
if(e.getSource()==b4)
System.exit(0);
}}

Wednesday, 25 June 2014

Program to print delayed output using multithreading


public class loading implements Runnable{


public void run() {
// TODO Auto-generated method stub
int i;

for(i=0; i<=5; i++)
{
try
{

Thread.sleep(1000);

}
catch(Exception e){}
System.out.println("i="+i);
}

}
public static void main(String[] args)
{
loading l=new loading();
Thread t=new Thread(l);
t.start();

}}

output:
i=0
i=1
i=2
i=3
i=4
i=5

Program to print delayed output

import javax.swing.*;

public class delayed {

public static void main(String[] args)
{
int i;

for(i=0; i<=5; i++)
{
try
{

Thread.sleep(1000);

}
catch(Exception e){}
System.out.println("i="+i);
}

}}

output:
i=0
i=1
i=2
i=3
i=4
i=5

Tuesday, 24 June 2014

Program to show Panels and Buttons

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class addingpanelonpanel extends Applet implements ActionListener{
Panel p1,p2;
Button b1,b2;

public void init()
{
setLayout(null);
b1= new Button("open panel 2");
b2= new Button("open panel 1");
b1.setBounds(130,130,160,20);
b2.setBounds(450,130,160,20);
p1=new Panel();
p2=new Panel();
p1.setBounds(120,120,220,120);
p2.setBounds(420,120,220,120);
p1.setBackground(Color.red);
p2.setBackground(Color.PINK);
b1.addActionListener(this);
b2.addActionListener(this);
p1.setVisible(true);
p2.setVisible(false);
add(p1);
add(p2);
p1.add(b1);
p2.add(b2);
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==b1)
{
p2.setVisible(true);
p1.setVisible(false);
}
else
{
p1.setVisible(true);
p2.setVisible(false);
}
}

}

output:

Creating a frame having menu

import java.awt.Frame;
import java.awt.Menu;
import java.awt.*;
import java.awt.event.*;
public class Menucreation extends Frame implements WindowListener{
Menucreation()
{
MenuBar mb;

Menu f,e,w;
MenuItem n,c,cpy,pst,h;
mb= new MenuBar();
f=new Menu("file");
e=new Menu("edit");
w=new Menu("window");
n=new MenuItem("new");
c=new MenuItem("cut");
cpy=new MenuItem("copy");
pst=new MenuItem("paste");
h=new MenuItem("help");

this.setMenuBar(mb);
mb.add(f);
mb.add(e);
mb.add(w);
f.add(n);
e.add(c);
e.add(cpy);
e.add(pst);
w.add(h);
this.setVisible(true);
this.setSize(1000, 600);
this.setTitle("mine");
this.addWindowListener(this);
}

public static void main(String[] args) {
new Menucreation();

}

public void windowActivated(WindowEvent arg0) {
// TODO Auto-generated method stub

}

public void windowClosed(WindowEvent arg0) {
// TODO Auto-generated method stub

}

public void windowClosing(WindowEvent arg0) {
// TODO Auto-generated method stub
System.exit(0);
}

public void windowDeactivated(WindowEvent arg0) {
// TODO Auto-generated method stub

}

public void windowDeiconified(WindowEvent arg0) {
// TODO Auto-generated method stub

}

public void windowIconified(WindowEvent arg0) {
// TODO Auto-generated method stub

}

public void windowOpened(WindowEvent arg0) {
// TODO Auto-generated method stub

}

}

output:

Creating a new frame

import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class Framecreation extends Frame implements WindowListener{
Framecreation()
{
this.setVisible(true);
this.setSize(200, 200);
this.setTitle("mine");
this.addWindowListener(this);
}

public static void main(String[] args) {
new Framecreation();

}

public void windowActivated(WindowEvent arg0) {
// TODO Auto-generated method stub

}

public void windowClosed(WindowEvent arg0) {
// TODO Auto-generated method stub

}

public void windowClosing(WindowEvent arg0) {
// TODO Auto-generated method stub
System.exit(0);
}

public void windowDeactivated(WindowEvent arg0) {
// TODO Auto-generated method stub

}

public void windowDeiconified(WindowEvent arg0) {
// TODO Auto-generated method stub

}

public void windowIconified(WindowEvent arg0) {
// TODO Auto-generated method stub

}

public void windowOpened(WindowEvent arg0) {
// TODO Auto-generated method stub

}

}

output: