Core java tutorial for beginners
A tutorial blog which explains different core concepts related to Java along with programming examples

April 11, 2016 Categories: AWT. No Comments on Graphics in AWT

In this article we will look at how to work with graphics in AWT. We will look at various AWT classes that help us with creating graphics in our applications.

 

Graphics Class

In GUI applications, we can use Graphics class of java.awt package to create various graphics like lines, rectangles, circles, polygons etc. Let’s look at some of the methods available in the Graphics class:

void drawLine(int startX, startY, endX, endY) – Used to draw a line between twi points.

void drawRect(int startX, int startY, int width, int height) – Used to draw a rectangle starting from the top left corner with the given width and height. The coordinates of the top left corner of the rectangle are startX and startY.

void fillRect(int startX, int startY, int width, int height) – Used to draw a solid (colored) rectangle with the given parameters.

void drawRoundRect(int startX, int startY, int width, int height, int xDiam, int yDiam) – Used to draw a rounded rectangle whose x-diameter and y-diameter of the corners is given by xDiam and yDiam respectively.

void fillRoundRect(int startX, int startY, int width, int height, int xDiam, int yDiam) – Used to draw a solid (colored) rounded rectangle whose x-diameter and y-diameter of the corners is given by xDiam and yDiam respectively.

void drawOval(int startX, int startY, int width, int height) – Used to draw an ellipse inside an imaginary rectangle whose dimensions are specified by the given parameters. We can get a circle by giving the same value for both width and height.

void fillOval(int startX, int startY, int width, int height) – Used to draw a solid (colored) ellipse inside an imaginary rectangle whose dimensions are specified by the given parameters. We can get a circle by giving the same value for both width and height.

void drawArc(int startX, int startY, int width, int height, int startAngle, int sweepAngle) – Used to draw an arc inside an imaginary rectangle. The start angle and the sweep angle are specified by using startAngle and sweepAngle respectively.

void fillArc(int startX, int startY, int width, int height, int startAngle, int sweepAngle) – Used to draw a solid (colored) arc inside an imaginary rectangle. The start angle and the sweep angle are specified by using startAngle and sweepAngle respectively.

void drawPolygon(int x[], int y[], int numPoints) – Used to draw a polygon whose x coordinates and y coordinates of the points are specified using the x array and y array respectively. Number of points are specified using numPoints.

void fillPolygon(int x[], int y[], int numPoints) – Used to draw a solid (colored) polygon whose x coordinates and y coordinates of the points are specified using the x array and y array respectively. Number of points are specified using numPoints.

Following Java code demonstrates various methods from the Graphics class:

Output of the above program is as shown below:

java-awt-graphics

 

Color Class

To use colors in our GUI applications, AWT provides the Color class. We can create a Color object by using any of the following constructors:

Color(int red, int green, int blue)

Color(int rgbValue)

Color(float red, float green, float blue)

We can get values of red component, green component or blue component of the color by using the following methods:

int getRed()

int getGreen()

int getBlue()

We can apply or get the color of the graphic object by using the following methods:

void setColor(Color newColor)

Color getColor()

After applying colors the above Java program is as follows:

Output of the above program is as shown below:

awt-graphics-colors

 

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll Up
Thank you for visiting startertutorials.com! If you like this website do share with your friends by clicking on the social icons available on the left hand side.

As the domain name is quite large to remember, use this short domain to visit this site. Short domain: stuts.me