AWT Graphics Class

Advertisements


Introduction

The Graphics class is the abstract super class for all graphics contexts which allow an application to draw onto components that can be realized on various devices, or onto off-screen images as well.

A Graphics object encapsulates all state information required for the basic rendering operations that Java supports. State information includes the following properties.

  • The Component object on which to draw.

  • A translation origin for rendering and clipping coordinates.

  • The current clip.

  • The current color.

  • The current font.

  • The current logical pixel operation function.

  • The current XOR alternation color

Class declaration

Following is the declaration for java.awt.Graphics class:

public abstract class Graphics
   extends Object

Class constructors

S.N.Constructor & Description
1Graphics() ()
Constructs a new Graphics object.

Class methods

S.N.Method & Description
1abstract void clearRect(int x, int y, int width, int height)
Clears the specified rectangle by filling it with the background color of the current drawing surface.
2abstract void clipRect(int x, int y, int width, int height)
Intersects the current clip with the specified rectangle.
3abstract void copyArea(int x, int y, int width, int height, int dx, int dy)
Copies an area of the component by a distance specified by dx and dy.
4abstract Graphics create()
Creates a new Graphics object that is a copy of this Graphics object.
5Graphics create(int x, int y, int width, int height)
Creates a new Graphics object based on this Graphics object, but with a new translation and clip area.
6abstract void dispose()
Disposes of this graphics context and releases any system resources that it is using.
7void draw3DRect(int x, int y, int width, int height, boolean raised)
Draws a 3-D highlighted outline of the specified rectangle.
8abstract void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle)
Draws the outline of a circular or elliptical arc covering the specified rectangle.
9void drawBytes(byte[] data, int offset, int length, int x, int y)
Draws the text given by the specified byte array, using this graphics context's current font and color.
10void drawChars(char[] data, int offset, int length, int x, int y)
Draws the text given by the specified character array, using this graphics context's current font and color.
11abstract boolean drawImage(Image img, int x, int y, Color bgcolor, ImageObserver observer)
Draws as much of the specified image as is currently available.
12abstract boolean drawImage(Image img, int x, int y, ImageObserver observer)
Draws as much of the specified image as is currently available.
13abstract boolean drawImage(Image img, int x, int y, int width, int height, Color bgcolor, ImageObserver observer)
Draws as much of the specified image as has already been scaled to fit inside the specified rectangle.
14abstract boolean drawImage(Image img, int x, int y, int width, int height, ImageObserver observer)
Draws as much of the specified image as has already been scaled to fit inside the specified rectangle.
15abstract boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, Color bgcolor, ImageObserver observer)
Draws as much of the specified area of the specified image as is currently available, scaling it on the fly to fit inside the specified area of the destination drawable surface.
16abstract boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, ImageObserver observer)
Draws as much of the specified area of the specified image as is currently available, scaling it on the fly to fit inside the specified area of the destination drawable surface.
17abstract void drawLine(int x1, int y1, int x2, int y2)
Draws a line, using the current color, between the points (x1, y1) and (x2, y2) in this graphics context's coordinate system.
18abstract void drawOval(int x, int y, int width, int height)
Draws the outline of an oval.
19abstract void drawPolygon(int[] xPoints, int[] yPoints, int nPoints)
Draws a closed polygon defined by arrays of x and y coordinates.
20void drawPolygon(Polygon p)
Draws the outline of a polygon defined by the specified Polygon object.
21abstract void drawPolyline(int[] xPoints, int[] yPoints, int nPoints)
Draws a sequence of connected lines defined by arrays of x and y coordinates.
22void drawRect(int x, int y, int width, int height)
Draws the outline of the specified rectangle.
23abstract void drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)
Draws an outlined round-cornered rectangle using this graphics context's current color.
24abstract void drawString(AttributedCharacterIterator iterator, int x, int y)
Renders the text of the specified iterator applying its attributes in accordance with the specification of the TextAttribute class.
25abstract void drawString(String str, int x, int y)
Draws the text given by the specified string, using this graphics context's current font and color.
26void fill3DRect(int x, int y, int width, int height, boolean raised)
Paints a 3-D highlighted rectangle filled with the current color.
27abstract void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle)
Fills a circular or elliptical arc covering the specified rectangle.
28abstract void fillOval(int x, int y, int width, int height)
Fills an oval bounded by the specified rectangle with the current color.
29abstract void fillPolygon(int[] xPoints, int[] yPoints, int nPoints)
Fills a closed polygon defined by arrays of x and y coordinates.
30void fillPolygon(Polygon p)
Fills the polygon defined by the specified Polygon object with the graphics context's current color.
31abstract void fillRect(int x, int y, int width, int height)
Fills the specified rectangle.
32abstract void fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)
Fills the specified rounded corner rectangle with the current color.
33void finalize()
Disposes of this graphics context once it is no longer referenced.
34abstract Shape getClip()
Gets the current clipping area.
35abstract Rectangle getClipBounds()
Returns the bounding rectangle of the current clipping area.
36Rectangle getClipBounds(Rectangle r)
Returns the bounding rectangle of the current clipping area.
37Rectangle getClipRect()
Deprecated. As of JDK version 1.1, replaced by getClipBounds().
38abstract Color getColor()
Gets this graphics context's current color.
39abstract Font getFont()
Gets the current font.
40FontMetrics getFontMetrics()
Gets the font metrics of the current font.
41abstract FontMetrics getFontMetrics(Font f)
Gets the font metrics for the specified font.
42boolean hitClip(int x, int y, int width, int height)
Returns true if the specified rectangular area might intersect the current clipping area.
43abstract void setClip(int x, int y, int width, int height)
Sets the current clip to the rectangle specified by the given coordinates.
44abstract void setClip(Shape clip)
Sets the current clipping area to an arbitrary clip shape.
45abstract void setColor(Color c)
Sets this graphics context's current color to the specified color.
46abstract void setFont(Font font)
Sets this graphics context's font to the specified font.
47abstract void setPaintMode()
Sets the paint mode of this graphics context to overwrite the destination with this graphics context's current color.
48abstract void setXORMode(Color c1)
Sets the paint mode of this graphics context to alternate between this graphics context's current color and the new specified color.
49String toString()
Returns a String object representing this Graphics object's value.
50abstract void translate(int x, int y)
Translates the origin of the graphics context to the point (x, y) in the current coordinate system.

Methods inherited

This class inherits methods from the following classes:

  • java.lang.Object

Graphics Example

Create the following java program using any editor of your choice in say D:/ > AWT > com > tutorialspoint > gui >

AWTGraphicsDemo.java
package com.tutorialspoint.gui;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;

public class AWTGraphicsDemo extends Frame {
       
   public AWTGraphicsDemo(){
      super("Java AWT Examples");
      prepareGUI();
   }

   public static void main(String[] args){
      AWTGraphicsDemo  awtGraphicsDemo = new AWTGraphicsDemo();  
      awtGraphicsDemo.setVisible(true);
   }

   private void prepareGUI(){
      setSize(400,400);
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      }); 
   }    

   @Override
   public void paint(Graphics g) {
      g.setColor(Color.GRAY);
      Font font = new Font("Serif", Font.PLAIN, 24);
      g.setFont(font);
      g.drawString("Welcome to TutorialsPoint", 50, 150);      
   }
}

Compile the program using command prompt. Go to D:/ > AWT and type the following command.

D:\AWT>javac com\tutorialspoint\gui\AWTGraphicsDemo.java

If no error comes that means compilation is successful. Run the program using following command.

D:\AWT>java com.tutorialspoint.gui.AWTGraphicsDemo

Verify the following output

AWT Graphics

Advertisements
Advertisements