Java Graphics in Applet

Graphics operations can be performed using java.awt package that contains various classes and methods for creating graphics and images.The paint() takes graphic object as an argument and provides reference to graphics drawing methods(setColor(),drawstring()).

 

import java.awt.*;

import java.applet.*;

public class DrawExampl extends Applet

{

   Font bigFontt;

     Color redColoor;

     Color weirdColoor;

     Color bgColoor;

     public void init()

     {

         bigFontt = new Font("Arial",Font.BOLD/Font.ITALIC,26);

          redColoor = Color.red;

          weirdColoor = new Color(60,60,122);

            bgColoor = Color.blue;

           setBackground(bgColoor);

   }

     public void stop()

     {

     }

     public void paint(Graphics gra_ph)

     {

     gra_ph.setFont(bigFontt); //setting the font

     gra_ph.setColor(Color.gray); //set the colours for drawing

     gra_ph.drawString("Different Shapes and Colors Graphics ",80,50);

    gra_ph.setColor(redColoor);

     gra_ph.drawRect(100,100,100,100); //draw a hollow rectangle

   gra_ph.fillRect(110,110,80,80); //draw a filled rectangle

    gra_ph.setColor(weirdColoor);

   gra_ph.fillArc(120,120,60,60,0,360);   //draw a filled arc

   gra_ph.setColor(Color.yellow);

     gra_ph.drawLine(140,140,160,160); //draw a straight line

 gra_ph.setColor(Color.black);

   }

}

/*<applet code="DrawExample.class" height=500 width=500></applet>*/

 Graphics in Applet

Explanation…

The String value can be displayed using drawstring() method of the Graphics class.

Graphics are drawn on the window at the specified screen coordinates(measured in pixels)

clearRect(),drawLine(),drawArc(),drawOval(),drawPolygon(),fillArc(),setFont(),setColor() are drawing methods of the Graphics Class.

About Us

Rekha SetiaRekha Setia is a passionate blogger of Extra Computer Notes. if you have any ideas or any request me @