Graphics Class

Package : Java.awt

Class: java.awt.Graphics

Note :The Graphics class is the abstract base class for all graphics contexts that allow an application to draw onto components that are realized on various devices, as well as onto off-screen images.

Constructors of Graphics Class
Name Description
Graphics() This constructor creates instance of Graphics class .

Methods of Panel Class
Method Description
create() This method create the object of Graphics class which is the copy of another Graphics object.
draw3DRect(int x, int y, int width,
int height, boolean raised)
This method draw a 3-D outline of the rectangle.
drawArc(int x, int y, int width,
int height, int startAngle, int arcAngle)
This method draw outline of circular and elliptical rectangle.
drawBytes(byte[] data, int offset,
int length, int x, int y)
This method draw the specified String at the specified position.
drawChars(char[] data, int offset,
int length, int x, int y)
This method draw the character at specific position.
drawImage(Image img, int x, int y,
Color bgcolor, ImageObserver observer)
This method draw image at specific position.
drawLine(int x1, int y1, int x2,
int y2)
This method draw line with specified width and height.
drawOval(int x, int y, int width,
int height)
This method draw Oval with specified width and height.
drawPolygon(int[] xPoints,
int[] yPoints, int nPoints)
This method draw Polygon .
drawRect(int x, int y, int width,
int height)
This method draw Rectangle with specified width and height.
drawRoundRect(int x, int y, int width,
int height, int arcWidth, int arcHeight)
This method draw Round Rectangle with specified width and height.
drawString (AttributedCharacterIterator iterator, int x, int y) This method draw String with specified width and height.
fill3DRect(int x, int y, int width,
int height, boolean raised)
This method draw a filled rectangle with specified width and height.
fillOval(int x, int y, int width,
int height)
This method draw a filled Oval with specified width and height.
setColor(Color c) This method sets the color.
setFont(Font font) This method sets the font.
Example :
package com.jtechies.graphics;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.SystemColor;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class GraphicsDemo extends Frame
{
	public GraphicsDemo()
	{
		try
		{
			jbInit();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	private void jbInit() throws Exception
	{
		this.setLayout( null );
		this.setSize(new Dimension(390, 232));
		this.setBackground( SystemColor.control );
		this.setVisible(true);
		this.setTitle("Graphics Class Demo");
		this.addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				this_windowClosing(e);
			}
		});
		this.setLocation(250,200);
	}
	public void paint(Graphics g)
	{
		g.drawRect(10,50,50,50);
		g.drawString("jtechies Heights ",70,70);
		g.fillRect(100,100,50,50);
		g.setColor(Color.RED);
		g.fillOval(150,150,50,50);
	}
	public static void main(String args[])
	{
		new GraphicsDemo();
	}
	private void this_windowClosing(WindowEvent e)
	{
		System.exit(0);
	}
}
Output :
AWT Graphics Class