Which class provides many methods in Graphics Programming ?? Answer to this question depends on the language used to do graphics programming. Suppose a programmer is doing graphics programming in Java, then java.awt.Graphics class provides many methods for graphics programming. On the other hand, if a programmer is doing graphics programming in the .net framework then these classes provide many methods in graphics programming System.Drawing, System.Drawing.Design, System.Drawing.Drawing2D, System.Drawing.Imaging, System.Drawing.Printing, System.Drawing.Text.
Doing graphics programming using java.awt.Graphics
C language is often used to teach programming. Each and every program in C language is done by including header files such as stdio.h and conio.h. These header files are responsible for functions related to input stream and output stream. If a programmer wants to do graphics programming in C language then it has to include graphics.h header file. This is a C based graphics library having functions that are responsible for making drawing graphics on the console. graphics.h is a pedagogical tool and is appreciated by many programmers. But the problem with graphics.h was that of portability. To solve the issue of portability java graphics library was used instead of graphics.h of C language.
Java graphics library includes object-oriented data model, enhanced type safety and automatic garbage collection. The Graphics library of Java is interactive. java.awt package is used in graphics programming. java.awt is based on an event driven paradigm and the content developed using java.awt has to be refreshed. If graphics are displayed on a window then the java applet redisplays it, java applet do this using paint method.
To draw a line in java, drawline method is used, the draw line method takes absolute coordinates as input. Java uses a coordinate system that is similar to bitmapped monitors. In Java coordinates are integer values that denote pixels. Java coordinate system is resolution-dependent and it is non-Cartesian. The Graphics2D class in java is responsible for scaling and translation using a real coordinate system.
The basic functionality of java.awt class is extended by DBCanvas class. The DBCanvas is a subclass that is responsible for generating bitmap graphics. The DBCanvas class works with canvas dimensions and allocates required bitmaps. This class also uses a paint method that is responsible for offscreen display. The DBCanvas works in association with the XGraphics class.
The XGraphics Class is used to display images that are compatible with the Java standards. The XGraphics Class uses a floating-point coordinate system due to which XGraphics Class is able to scale, translate and flip the images.
Next, the class that is used for graphics in Java is the Widget Class. The Widget Class encloses different subclasses such as LineWidget, ArcWidget, BoxWidget, OvalWidget, PolygonWidget, TextWidget and ImageWidget. Coordinates in the TextWidget use the XGraphics Class. Few of the Widget method that are used in graphics programming includes – moveAbsolute(x,y), moveRelative(dx,dy), moveToFront( ), moveToBack( ), add(widget), add(widget), remove(widget), setColor(Color), color = getColor( ), setFont(font), font = getFont( ), setFill(fill), fill = getFill( ). Widget Class is container class
Graphics Programming using .Net Framework Graphics Library
The .net Framework has common language runtime. This common language runtime uses GDI+ to do graphics programming in .net Framework. This GDI+ is known as Graphic Device Interface. Using GDI+ graphics can be created, text can be drawn and graphical images can be treated as objects which can be manipulated. GDI+ is used to display graphics on windows forms. Graphics can also be displayed on controls using GDI+. GDI+ is not compatible with web forms. To display graphics on web forms Image Web Server is used.
Graphics programming in .net Framework is divided into three broad categories:
- 2D vector graphics
2D Vector Graphics
The 2D vector graphics include lines, curves and figures. Straight Lines in 2D vector graphics are defined by two endpoints and a rectangle is defined by giving the point of the upper left corner and defining the width and height of the rectangle. To define a path array of points are used and to define the Bezier spline curve four control points are used.
To draw a rectangle System.Drawing namespace is used and constructors Rectangle(Int32, Int32, Int32, Int32) and Rectangle(Point, Size) are used. These constructors create new objects of the rectangle class having specified location and size.
To draw lines and curves Pen Class is used. Pen Class draws lines and curves having required width and style. Dashed lines can be drawn using DashStyle property. The line drawn using Pen Class can be filled in with desired color and textures. Few of the method of Pens Class includes: Pen(Brush) this method creates an object of Pen Class with defined Brush, Pen(Brush, Single) this class method creates new object of Pen Class with specified Brush and Width, Pen(Color) this method creates new object of Pen Class with the desired color, Pen(Color, Single) this method also creates new object of the Pen Class with the defined color.
There are pictures that can not be displayed using vector graphics. Examples of these pictures include pictures that are displayed on buttons and pictures that act as icons. These pictures are easily displayed using Bitmaps. In the .net framework Bitmap Class is used to work with bitmaps.
The Bitmap Class has the following methods to create bitmap images: Bitmap(Image) this method creates new objects of the bitmap class with specified image, Bitmap(Image, Int32, Int32) this method creates new object of the Bitmap class using existing image which is scaled to desired size, Bitmap(Int32, Int32, Graphics) this method creates new object of the Bitmap Class with size governed by two integer parameters and resolution with the desired graphics objects, Bitmap(Int32, Int32, PixelFormat) this method creates new Bitmap Class Object with desired size, pixel format and pixel data.
.net graphics programming is also done to improve typography. Typography is defined by fonts, sizes and styles. GDI+ is also used for rendering text on the screen. The TextRender Class has static methods that used to measure and draw text on the windows form control.TextRender Class has the following methods: DrawText(IDeviceContext, Read OnlySpan<Char>, Font, Point, Color) Draws the desired text at the desired location with the desired context, desired font and desired color, MeasureText(IDeviceContext, String, Font, Rectangle, Color, Textformatflags) this methods specify the size in pixel of the desired text having desired font on the desired device context etc.
.net framework uses System.Drawing class to fulfill the functionality of the Graphics programming. .net Framework also uses System.Drawing.Drawing2D, System.Drawing.Imaging and System.Drawing.Text class library to deliver graphics functionality to be used in different contexts.
The System.Drawing class library supports the following class of graphics: Bitmap, Brush, Font, Graphics, Image, Pen, SystemColors, SystemFonts, SystemIcons etc.
System.Drawing.Drawing2D class library provides functionality related to two-dimensional vector graphics. Following methods are supported by System.Drawing.Drawing2D class library: Blend, ColorBlend, GraphicsContainer, GraphicsPath, LinearGradientBrush, Matrix, PathData etc.
System.Drawing.Imaging Class delivers GDI+ functionalities related to imaging. Following are the methods provided by System.Drawing.Imaging Class: BitmapData this method attributes related to bitmap image, ColorMap this method that converts colors of images using ColorMap array structures. ColorMatrix this method provides a 5 x 5 matrix containing coordinates related to RGBAW space. The color matrix is used to adjust colors, FrameDimension this method provides image frame dimensions, ImageFormat output of this method is image format.