Pageviews

Showing posts with label Applets. Show all posts
Showing posts with label Applets. Show all posts

Sunday, 7 July 2013

Playing audio in Java Applet

by Unknown  |  in Java at  Sunday, July 07, 2013
Applets provide GUI,which is preferred by all users.Java applet supports another good feature of playing music files within the applet.Here are the easy steps,you need to follow in order to run your music files in your Applets.

1.Firstly create a Applet,or you can use the applet code used in the previous post.Then you need to declare the audio you like to output from your Applet using keyword 'AudioClip'.The syntax will be like this;

AudioClip variable_name;

2.Now within the start(),you need to declare your AudioClip using its variable and define its path,where it is located.The syntax will be;

sound=getAudioClip(getCodeBase(),"AudioFile_Path.wav");       //sound-variable name

3.If the audio which you want to play lies within the folder,where you are writing the code,then you need not to specify the path,just simply write its name in " ".But,if you are importing your audio from another drive or folder,then you need to specify its path.

4.In the next step,we need to play our audio clip using play().

Source code for playing audio in Java Applet:

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

public class applet4 extends Applet
{
AudioClip soundFile1;
Image p;
public void init()
{
p=getImage(getDocumentBase(),"6.jpg");
}
public void start()
{
soundFile1 = getAudioClip (getCodeBase()," 123.wav ");
soundFile1.loop();
soundFile1.play();
}
public void paint(Graphics g)
{
g.drawImage(p,10,10,this);
g.drawString("Music Player",20,20);
}
}

Then you need to compile your java file like this:

Applet

The output will be like this:

Applet music



The work is all done,now you can play your audio files in your Applets.You will not be able to hear anything from this applet,since Java is not installed on our blog,but you will definately hear audio in your device.  The most important point to remember is that,you can only play .wav files in Applets,if you want to play another format,then you can convert it first,then you can play.

Display images in Java Applets

by Unknown  |  in Java at  Sunday, July 07, 2013
Displaying background images is the another powerful functionality provided by Java Applets.You can choose any of your favorite wallpapers as your background screen in Applets.So,here are some easy steps to display background images in Applets.


  • Firstly create your Applet.
  • In the declaration section,you need to declare a variable for your image,like;


Image variable_name;

In the init(),you need to initialize your image and define its path,where it is located in you system,using the following syntax;

p=getImage(getDocumentBase(),"Image_path.jpg");

If your image is located within the folder,where you are writing the codes,then you need not to specify the path of the image just need to mention its name,but if your image located in the another folder/drive,then you need to specify the path within the double quotes(" ").

Now we need to invoke our image in the paint(),where your image will be displayed using below syntax;

g.drawImage(p,10,10,this);                                        //g-object of Graphics class.

Here is the complete code for displaying a image in your Applet.

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

public class applet4 extends Applet
{
Image p;
public void init()
{
p=getImage(getDocumentBase(),"4.png");
}
public void paint(Graphics g)
{
g.drawImage(p,10,10,this);
}
}

Applet


The output of the applet is :





Now you are all done with the coding part,now you can display your defined images as background image in your Applet.

Tuesday, 25 June 2013

Graphics in Java Applets

by Unknown  |  in Java at  Tuesday, June 25, 2013
Graphics class in Applets helps us to draw different shapes in Applets,for drawing these shapes each shape has its own method.These methods are as follows:

Most Common Graphic class methods:


  • drawString(String Str,int x,int y): This method is used to draw the required String.
  • drawOval(int x,int y,int width,int height): It is used to draw Oval of the specified width and the height.
  • drawRect(int x,int y,int width,int height): It is used to draw Rectangle of the specified height and width.
  • drawLine(int x1,int y1,int x2,int y2): It is used to draw line between the points(x1,y1) and (x2,y2).
  • drawArc(int x,int y,int width,int height,int start angle,int arc angle ): It is used to draw an arc of the specified angle.
  • drawImage(Image img,int x,int y,Image observer): It is used to draw an Image of the specified parameters.
  • fillOval(int x,int y,int width,int height): It is used to fill the Oval with specified Color and the parameters specified.
  • fillRect(int x,int y,int width,int height): It is used to fill the Rectangle with specified Color and the parameters specified.
  • fillArc(int x,int y,int width,int height): It is used to fill the Arc with specified Color and the parameters specified.
  • setColor(Color c): This method is used to set the Color specified by the User.
  • setFont(Font font): This method is used to set the Font type and size according to the specified parameters.


Now,to implement these functions,we had created a simple program which will not use these functions fully,but will cover most of them.

WAP to implement the use of Graphics class in Java Applets:

import java.awt.*;
import java.applet.*;
public class Applet3 extends Applet
{
Thread t;                                                                                           //Thread is declared
AudioClip soundFile1;

public void start()
{
soundFile1 = getAudioClip (getCodeBase()," 123.wav ");                 //Audio file

soundFile1.loop();


soundFile1.play();

}
public void paint(Graphics g)
{
g.setFont(new Font("VERDANA",2,50));
g.drawString("Keep Smiling!!!",50,50);
g.setColor(Color.yellow);
g.fillOval(70,200,230,230);
g.setColor(Color.black);
try

{
g.drawOval(170,320,55,55);
for(int i=0;i<30;i+=2)
{
g.setColor(Color.black);
g.drawOval(110,250,30,i);
g.drawOval(210,250,30,i);
t.sleep(50);
g.setColor(Color.yellow);
}
g.setColor(Color.black);
g.drawOval(110,250,30,30);

g.drawOval(210,250,30,30);

g.drawArc(100,230,50,50,0,180);//main arc
g.drawArc(101,231,50,50,0,180);
g.drawArc(102,232,50,50,0,180);
g.drawArc(200,230,50,50,0,180);//main arc
g.drawArc(201,231,50,50,0,180);
g.drawArc(202,232,50,50,0,180);

g.fillOval(110,250,30,30);
g.fillOval(210,250,30,30);
}
catch(InterruptedException e)
{
}
}
}

This is the output of our program:



Here is the Applet design:





Note: We had also used Audio file in our Applet,using Audio in Applet will be discussed in later posts.You just need to see the Graphic methods used.




Sunday, 23 June 2013

Applets in Java

by Unknown  |  in Java at  Sunday, June 23, 2013
Applets: Applets are Window-based programs.They are a special type of Java programs that are designed for the transmission over the Internet,Applets can run on Web browsers as well as they can be used for developing Console based applications.From the Applets,we enter into the GUI(Graphical User Interface) environment.

Applets initialization and Termination methods:

1.init(): it is the first method to be called.In init(),our applet will initialize variables and perform other startup activities.

2.start():The Start() method is called after init().It is also called to restart an applet after it has been stopped,start can be called more than once during the life of an applet.

3.paint(): This method is called each time our applet's output must be redrawn

4.stop(); This method is used to suspend any child threads created by the applet and to perform any other activities required to put the applet in safe state.

5. destroy(); This method is called when the applet is no longer required.It is basically used to perform the Shutdown operations required of the applet.

Now we are going to make our first applet,to build your own applet you can try the below code,for executing Applet you must have a Java browser.

WAP in JAVA to print String in Applet

import java.applet.*;
import java.awt.*;
public class Applet1 extends Applet                             //Applet class is inherited
{
public void paint(Graphics g)
{
g.setFont(new Font("VERDANA",2,30));                        //Method to change Font size and type
g.drawString("Coding Garage!!!",150,150);                     //Method to draw String
}
}

As our applet run in browser so we need to provide a HTML file which will make it run in browser.So,here is the HTML code we need to write in our Text editor and save it in .html extension.

<html>
<body>
<applet code="Applet1.class" width="300", height="300">
</applet>
</body>
</html>

How to run Applets in Java?

1.Firstly we need to compile our code,after compiling Filename.class file will be created which we will use in .html file.
2.Secondly,we need to write the following command i.e on the cmd to run our Applet

appletviewer Filename.html

3.You can also directly open Filename.html to open your Applet.



Applets

Here is the output of our first Applet:

Applets

  

Proudly Powered by Blogger.