Friday, 5 July 2019

Java Graphics - Online Help

Java graphics:

Java graphics class is an abstract class. When java is implemented on each platform, a subclass of graphics is created that implement the capabilities. This implementation is hidden from us by class graphics.

This class supplies the interface that enable us to use graphics in a platform-independent manner.

Every application we have seen in text that performs drawing on the screen has used the graphics object to manage the application's graphics context.


Graphics contexts in java enable us to draw on the screen. A graphics object manages a graphics context and draws pixels on the screen that represent text and another graphical object. For example lines, rectangles, ellipse, and polygons.

Graphics objects contain a method for drawing,font-manipulation, and color manipulation.

The graphics class component is the superclass for many of the class in the java. awt package.

For Example,

Class JComponent, is a java graphics class, which identifies indirectly from the class component, It contains a paint component method that can be used to draw the graphics object as an argument. This object is passed to the paint component method by the system.

The header for the paintComponent method is

public void paintComponent(Graphics g).

here,

The parameter g receives a reference to an instance of the system-specific subclass that graphics extents.

Methods available in java graphics class:

The graphics class provide different methods to draw and fill the various shapes. The shapes are as follow:

  • Line
  • Rectangle
  • Ovals and
  • Arc

Line:

-Draw lines:
The draw line method is used to draw a line between the point (x1,y1) and (x2,y2).The general form to draw the line is:

void drawLine(int x1,int y1,int x2,int y2)

This method takes two pair of coordinates(x1,y1) and (x2,y2) as arguments and draw a line between them.


For example,

the statement gra.drawLine(10,200,80,250)

will draw a straight line from the coordinate point (10,200) and (80,250)

Sample program to draw line:
import java.awt.*;
import java.applet.*;
/*<applet code="drawlinedemo" width=500,height=500>
</applet>*/
public class drawlinedemo extends Applet
{
public void paint(Graphics g)
{
g.drawLine(0,0,200,100);
g.drawLine(20,120,250,120);
}
}

Rectangle:

To draw and fill the rectangle, graphics provide three types of a rectangle. They are
   1.Sharp corner rectangle
   2.Round corner rectangle
   3.3D rectangle

1.Sharp corner rectangle:

drawRect():
The drawRect() method is used to draw a rectangle with an upper-left corner at coordinates x and y width w and height h,. The general form is,

void drawRect(int x, int y,int w,int h)

Here,

The first two arguments x and y represent the x and y coordinates of the top-left corner of the rectangle. The last two arguments w and h represent the width and height of the rectangle.


fillRect()
The fillRect() method is used to draw a filled rectangle with the specified width and height from top left corner coordinates x and y. The general form is,

void fillRect(int x,int y,int w,int h)

2.Round corner Rectangle:

drawRoundRect()
The drawRoundRect() method is used to draw around the corner rectangle with the specified width w and height h from (x,y). Arc width aw and arc height ah determines the rounding of the corner. The general is

void drawRoundRect(int x,int y,int w,int h,int aw,int ah)

fillRoundRect()
The fillRoundRect() method is used to draw a filled rounded corner rectangle with the specified width w and height h. The arc width aw and arc height ah determines the rounding of the corner. The general form is

fillRoundRect(int x,int y,int w,int h,int aw,int ah)


3.3D rectangle:

draw3DRect():
The draw3DRect() method is used to draw a three-dimensional rectangle with the specified width w and height h from the left corner of x and y. The general form is,

void draw3DRect(int x,int y,int w,int h,Boolean b)

The first two parameters x and y represents the top left corner of the rectangle the third and forth arguments h and w represent the width and height of the rectangle and last parameter, b indicates whether the rectangle has to be raised or not.

fill3DRect():
The fill3DRect() method is used to draw a filled three-dimensional rectangle with the specified width w and height h from the top left corner at coordinates x and y.

void fill3DRect(int x,int y,int w,int h,Boolean b)

The Boolean value indicates whether the rectangle has to be raised or not.

Sample program to draw rectangles:
import java.awt.*;
import java.applet.*;
/*<applet code="rectdemo" width=250,height=250>
</applet>*/
public class rectdemo extends Applet
{
public void paint(Graphics g)
{
g.drawRect(10,10,40,50);
g.fillRect(10,10,40,50);
g.drawRoundRect(10,10,40,50,10,20);
g.fillRoundRect(10,10,40,50,10,20);
}
}

Oval:

drawOval():
The drawOval() method is used to draw an oval with the specified width and height from(x,y). The general form is

void drawOval(int x,int y,int w,int h)

here, the first two parameters x and y represent the bounding the top left corner at the coordinate(x,y). The last two parameters width w and height h specify the width and height of the
oval.

fillOval():
The fillOval() method is used to draw filled oval with the specified width and height. The general form is,

void fillOval(int x,int y,int w,int h)

Sample program to draw Oval:
import java.awt.*;
import java.applet.*;
/*<applet code="ovaldemo" width=250 height=250>
</applet>*/
public class ovaldemo extends Applet
{
public void paint(Graphics g)
{
g.drawOval(0,0,30,30);
g.fillOval(0,60,40,20);
}
}

Arcs:

drawArc():
An arc can be drawn using the drawarc() method. This method takes six arguments in which the first four are the same as the argument of the drawOval() method and the next two arguments represent the starting angle of the arc and the sweep angle around the arc respectively.

The general format of the  drawArc() method is

void drawArc(int a1,int b1,int w,int h,int strt_angle,int sweep_angle)

Where,

a1, b1 is the coordinate of the top left corner of the bounding rectangle.

w is the width of the bounding rectangle.

h is the height of the bounding rectangle.

strt_angle is the starting angle of the arc.

sweep_angle is the number of degrees around the arc.

fillArc():
The fillArc() method is used to draw the filled the arc.The general form is,

void drawArc(int a1,int b1,int w,int h,int strt_angle,int sweep_angle)

Sample program to draw and fill arc in java graphics:
import java.awt.*;
import java.applet.*;
/*<applet code="arcdemo" width=250 height=250>
</applet>*/
public class arcdemo extends Applet
{
public void paint(Graphics g)
{
g.drawAr(240,125,80,40,0,180);
g.fillArc(370,125,80,40,0,180);
}
}


No comments:

Post a Comment