Thursday, 27 February 2014

INTRODUCING THE AWT:Working With Graphics

AWT supports a rich assortment of graphics method.All graphics are drawn reative to window..........

Working with Graphics

The AWT supports a rich assortment of graphics method.All graphics are drawn reative to window.This can be a main wi ndow of an applet,a child window of an applet ,or stand alone appliction of window.The origin of each window is at the top-left corner and is 0,0. Coordinates are specified in pixels. All output to a window takes place through a graphics context. 

A graphics context is encapsulated by the Graphics class and is obtained in two ways:

• It is passed to a method, such as paint( ) or update( ), as an argument.

• It is returned by the getGraphics( ) method of Component.

The Graphics class defines a number of drawing functions.Each shape can be drawn edge only or filled .several drawing methods are:

  • Drawing Lines

      Lines are drawn by means of the drawLine() method,shown here:

                               void drawLine(int startX,int startY,int endX,int endY)

simple applet program to drawn a line


import java.applet.*;
import java.awt.*;
public class DrawingLines extends Applet
 {
   int width, height;
   public void init()
 {
      width = getSize().width;
      height = getSize().height;
      setBackground( Color.black );
   }
   public void paint( Graphics g ) 
{
      g.setColor( Color.green );
      for ( int i = 0; i < 10; ++i ) 
{
         g.drawLine( width, height, i * width / 10, 0 );
      }
   }
}


Sample output from this program is shown here:


















  • Drawing Rectangles

        The drawRect() and fillRect() methods display an outlined and filled rectangle, respectively.

 syntax:
               void drawRect(int top,int left,int width,int height)
               void fillRect(int top,int left,int width,int height)

 The upper left corner of the rectangle is at top,left.The dimension of the rectangle are specified by the width,height.To draw rounded rectangle,use drawRoundRect()or fillRoundRect().

syntax
           void drawRoundRect(int top,int left,int width,int height,int xDiam,int yDiam)
            void fillRoundRect(int top,int left,int width,int height,int xDiam,int yDiam)

A rounded rectangle has rounded corners. The upper-left corner of the rectangle is at top,left. The dimensions of the rectangle are specified by width and height.The diameter of the rounding arc along the X axis is specified by Diam. The diameter of the rounding arc along the Y axis is specified by yDiam.

 applet program to draws several rectangles:

// Draw rectangles
import java.awt.*;
import java.applet.*;
/*
<applet code="Rectangles" width=300 height=200>
</applet>
*/
public class Rectangles extends Applet 
{
public void paint(Graphics g)
 {
g.drawRect(10, 10, 60, 50);
g.fillRect(100, 10, 60, 50);
g.drawRoundRect(190, 10, 60, 50, 15, 15);
g.fillRoundRect(70, 90, 140, 100, 30, 40);
}
}

Sample output from this program is shown here:
















  • Drawing Ellipses and Circles

                To draw an ellipse, use drawOval( ). To fill an ellipse, use fillOval( ). 
syntax:
        void drawOval(int top, int left, int width, int height)
        void fillOval(int top, int left, int width, int height)


The ellipse is drawn within a bounding rectangle whose upper-left corner is specified by top,left and whose width and height are specified by width and height. To draw a circle, specify a square as the bounding rectangle.

The following program draws several ellipses:

// Draw Ellipses
import java.awt.*;
import java.applet.*;
/*
<applet code="Ellipses" width=300 height=200>
</applet>
*/
public class Ellipses extends Applet {
public void paint(Graphics g) {
g.drawOval(10, 10, 50, 50);
g.fillOval(100, 10, 75, 50);
g.drawOval(190, 10, 90, 30);
g.fillOval(70, 90, 140, 100);
}
}

Sample output from this program is shown here:





  • Drawing Arcs

          Arcs can be drawn with drawArc( ) and fillArc( ), shown here:

       void drawArc(int top, int left, int width, int height, intstartAngle,intsweepAngle)
        void fillArc(int top, int left, int width, int height, intstartAngle,intsweepAngle)


The arc is bounded by the rectangle whose upper-left corner is specified by top,left and whose width and height are specified by width and height. The arc is drawn from startAnglethrough the angular distance specified by sweepAngle. Angles are specified in degrees. Zero degrees is on the horizontal, at the three o’clock position. The arc is drawn counterclockwise ifsweepAngleis positive, and clockwise ifsweepAngleis negative. Therefore, to draw an arc from twelve o’clock to six o’clock, the start angle would be 90 and the sweep angle 180.


The following applet draws several arcs:

// Draw Arcs
import java.awt.*;
import java.applet.*;
/*
<applet code="Arcs" width=300 height=200>
</applet>
*/
public class Arcs extends Applet 
{
public void paint(Graphics g)
 {
g.drawArc(10, 40, 70, 70, 0, 75);
g.fillArc(100, 40, 70, 70, 0, 75);
g.drawArc(10, 100, 70, 80, 0, 175);
g.fillArc(100, 100, 70, 90, 0, 270);
g.drawArc(200, 80, 80, 80, 0, 180);
}
}

Sample output from this program is shown here:

















  • Drawing Polygons

         It is possible to draw arbitrarily shaped figures using drawPolygon( ) and fillPolygon( ),
shown here:

           void drawPolygon(int x[ ], int y[ ], int numPoints)
           void fillPolygon(int x[ ], int y[ ], int numPoints)


The polygon’s endpoints are specified by the coordinate pairs contained within the x and y arrays. The number of points defined by x and y is specified by numPoints. There are alternative forms of these methods in which the polygon is specified by a Polygon object.


The following applet draws an polygon

// Draw Polygon
import java.awt.*;
import java.applet.*;
/*
<applet code="HourGlass" width=230 height=210>
</applet>
*/
public class HourGlass extends Applet 
{
public void paint(Graphics g)
 {
int xpoints[] = {30, 200, 30, 200, 30};
int ypoints[] = {30, 30, 200, 200, 30};
int num = 5;
g.drawPolygon(xpoints, ypoints, num);
710 Java™ 2: The Complete Reference
}
}

Sample output from this program is shown here:







1 comment :

  1. Thank you for this brief explanation and very nice information. Well, got good knowledge.
    Best graphic design company chennai

    ReplyDelete