Applet program to play audio/sound

This applet program plays an audio. The program declares objects sf1 and sf2 of AudoClip class. The program uses two audio clips. The getAudioClip() function is used to fetch the audio file from the disk, it will be stored in the objects sf1 and sf2.  The play() function is used to play the audio clip. When the program first loads the function sf1.play(); will be invoked and the first audio clip will be played. When the user clicks somewhere on the window, the first audio clip will be stopped and the second audio clip will be played. Also both the audio files must be stored in the same directory where the source file if stored. 


Program


//Applet program in java to add sound/Audio

//<Applet code=SoundApplet height=400 width=400></Applet>

import java.applet.*;

import java.awt.*;

import java.awt.event.*;

public class SoundApplet extends Applet implements MouseListener

{

AudioClip sf1,sf2;

public void start()

{

System.out.println("Applet Started");

}

public void init()

{

sf1=getAudioClip(getCodeBase(),"doorbell.wav");

sf2=getAudioClip(getCodeBase(),"doorbell.wav");

addMouseListener(this);

setBackground(Color.BLUE);

sf1.play();

}

public void paint(Graphics g)

{

g.drawString("Sound",100,200);

}

public void mouseClicked(MouseEvent e)

{

sf2.play();

}

public void mouseReleased(MouseEvent e)

{

System.out.println("Mouse Released");

}

public void mousePressed(MouseEvent e)

{

System.out.println("Mouse Pressed");

}

public void mouseEntered(MouseEvent e)

{

System.out.println("Mouse Entered");

}

public void mouseExited(MouseEvent e)

{

System.out.println("Mouse Exited");

}

}


Java applet program to display image

This applet program displays an image on the applet viewer. The program declares an object of the Image class. Then the program calls the getImage() function to specify the image and drawImage() function to draw the image to the output screen. The image to be displayed must be stored in the same directory where the source code is stored. 


Program



//Applet program to display image

//<Applet code=ImageApplet height=400 width=400></Applet>

import java.awt.*;

import java.applet.*;

public class ImageApplet extends Applet

{

Image img;

public void paint(Graphics g)

{

img=getImage(getCodeBase(),"lotus.jpg");

g.drawImage(img,100,100,this);

}

}


Output


image applet in java
Applet program to display image

Java program to divide two numbers

This program divides two numbers in Java, using command line arguments. The dividend and divisor are read as input from the command line. The program uses a try catch statement to read the inputs and perform the division. The statement a=Integer.parseInt(s[0]); stores the first argument to the variable a and the statement b=Integer.parseInt(s[1]); stores the second argument to the variable b. If the divisor is 0 the program will throw an ArithmeticException also if there is any problem in reading the arguments the program throws an ArrayIndexOutOfBoundsException. Otherwise the program prints the result of the division to the output screen. 

Program


 

 // command line arguments in java

// Java program to find the quotient 

// Use command line arguments find the quotient of two numbers

// Demonstrate divide by zero exception

class Quotient
{ public static void main(String s[]) throws Exception { int a,b,c; try { a=Integer.parseInt(s[0]); // getting data b=Integer.parseInt(s[1]); // from command line try { c=a/b; System.out.println("Quotient is "+c); } catch(ArithmeticException ex) { System.out.println("Error in division !"); } } catch(ArrayIndexOutOfBoundsException e) { System.out.println("Array out of bound!"); } finally { System.out.println("End of the progarm!!!"); } } }


Output


java program to divide two numbers, command line

Java program to find the average of n numbers

This Java program finds the average on n numbers using command line arguments. The program uses a try catch statement to read the arguments form the command line. Each arguments are stored to the array a[] using a while loop. Then the program uses another while loop to calculate the sum of all the elements in the array a[] and it will be stored in the variable sum. The statement avg=sum/n; outside the while loop calculates the average and it will be displayed in the output screen.

Program



 //Java program to calculate the average of n numbers using  //command line arguments

import java.io.*; public class Average { public static void main(String s[]) throws Exception { int a[]=new int[10]; int i=0,n,sum=0,avg; n=s.length; try { while(i<n) { a[i]=Integer.parseInt(s[i]); //taking values from command line i++; } } catch(Exception e) { System.out.println("Error"); } n=i; i=0; while(i<n) { sum=sum+a[i]; i++; } avg=sum/n; System.out.println("Average is: "+avg); } }


Output


average command line n numbers java


Applet program to draw national flag

This applet program draws Indian national flag. The program uses four fillRect() function. The first one is used to draw the stand and the other three are used to draw the red, white and the green parts of the flag. The program uses a g.drawOval(); function to definer the border of the Ashoka Chakra. Then a for loop is used to draw the 24 spokes. The setColor(); function is used to specify the color to be used in the objects.

Program


// Draw national flag using Java applet program

//<applet code=Flag.class height=400 width=400></applet>

import java.applet.*;

import java.awt.*;

public class Flag extends Applet

{

public void paint(Graphics g)

{

Color c;

c=Color.black;

g.setColor(c);

g.fillRect(237,114,10,500);

c=Color.red;

g.setColor(c);

g.fillRect(248,125,200,25);

c=Color.white;

g.setColor(c);

g.fillRect(248,150,200,25);

c=Color.green;

g.setColor(c);

g.fillRect(248,175,200,25);

c=Color.blue;

g.setColor(c);

g.drawOval(342,149,25,25);
int l=0;
int x=355,y=162;
double x1,y1;
double d;

c=Color.black;

g.setColor(c);
for(int i=1;i<=24;i++)
{
d=(double)l*3.14/180.0;
x1=x+(double)10*Math.cos(d);
y1=y+(double)10*Math.sin(d);
g.drawLine(x,y,(int)x1,(int)y1);
l=l+(360/24);
}

}

}


Output


National flag drawing in applet
Applet program to draw national flag