Graphics Contexts and Graphics Objects Assignment Help

Assignment Help: >> AWT GUI Components - Graphics Contexts and Graphics Objects

Graphics Contexts & Graphics Objects:

A graphics context enables drawing on the screen in Java program. A Graphics object maintain a graphics context through controlling how information is drawn. A Graphics objects hold methods for drawing, color manipulation font manipulation, and many more. Each applet we have seen in the text which performs drawing on the screen has used the Graphics object g to maintain the applet's graphics context.

A Graphics class is an abstract class. That is Graphics object cannot be instantiated. Class Component is the super class for several of the classes in the AWT. Component method paint takes a Graphics object as an argument. This object is passed into the paint methods through the system whenever a paint operation occurs for a Component. A header for the paint method is:

public void paint(Graphics g)

The Graphics object g gets a reference to an object of the system's derived Graphics class. Whenever an applet is initially executed the paint method is automatically called. a call is made to the Component class repaint method if paint method is to be called once again. The repaint function requests a call to the Component class update method as soon as possible to clear the  Component's background of any previous drawing, then update calls paint directly. The common form of repaint and update is public void repaint( )

public void update (Graphics g)

Here both methods are public and have a void return type. The update method takes a Graphics object as an argument that is supplied automatically through method repaint. A Graphics method for drawing string and bytes and characters. A Method drawstring draws a String. A method takes three arguments - String to be drawn, an x coordinate, and a y coordinate. A String is drawn in the current font and color. A current color is the color in that text is drawn. A point(x,y) corresponds to the lower left corner of the string.

The common form is

public abstract void drawString ( String string, int x, int y)

string = string to be drawn int x = x coordinate

int y =  y coordinate

Method drawChars draws a series of characters. A method takes five arguments. A first argument is an array of characters. The second argument specifies the subscript in the array of the first character to be drawn. The third argument specifies the number of character to be drawn. The last two arguments specify the coordinates where drawing is to starts.  A point (x, y) corresponds to the lower-left corner of the first character drawn.

The common form is

public void drawChars(char chars[ ], int offset, int number, int x, int y)

chars[ ] = array to be drawn

offset  = starting subscript (index) number = number of elements to draw x and y = x and y coordianates

A Method drawBytes draws a series of byes. Such as the drawChars method, the drawBytes method takes five arguments. The 1st argument is an array of bytes. The 2nd argument specifies the subscript in the array of the first byte to be drawn. The third argument specifies the number of parts to be drawn. The last two arguments specify the coordinates where drawing is to starts. The point(x,y) corresponds to the lower-left corner of the bytes drawn

The general form is

// declaration

public void drawBytes ( byte bytes [ ], int offset, int number, int x, int y)

bytes [ ]  = array of bytes

offset  = starting subscript

number  =  number of elements to draw

x and y =  x and y coordinate

// Demonstrating drawString, drawChars and drawBytes

import java. applet.Applet;

import java.awt.Graphics;

public class DrawSCB extends Applet

{

private String s = "using drawstring";

private char c [ ] = { 'c', 'h', 'a','r', 's', ' ' ,' 8'}

private byte b[ ] = { 'b','y','t','e', 1,2,3}

pubic void paint (Graphics g)

{

g.drawstring(s,100,25);

g.drawChars(c,2,3,100,50);

g.drawBytes(b,0,5,100,50);

}

}

In the given program the drawstring method shows "using drawstring" at location 100,25 The statement

g.drawString(c,2,3,100,50);

Displays "ars" at location of (100, 50). A second argument, 2, specifies that drawing is to begin with subscript 2 of the character array c.  A third argument, 3,  specifes in which three elements will be drawn.  A  Method drawBytes displays a set of byte sat location (100,75).

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd