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:
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.
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.
0 comments: