2 Graphics in AWT - Core Java Tutorial For Beginners
2 Graphics in AWT - Core Java Tutorial For Beginners
(HTTPS://WWW.STARTERTUTORIALS.COM/COREJAVA)
A tutorial blog which explains different core concepts related to Java along with programming examples
Graphics in AWT
April 11, 2016 teja (https://www.startertutorials.com/corejava/author/teja) Categories: AWT (https://www.startertutorials.com/corejava/category/awt).
No Comments (https://www.startertutorials.com/corejava/graphics-in-awt.html#respond)
Ad closed by
A
Report
N E T F L I X
this ad
O R I G I N A L S E R I E S
Now streaming.
WATC H NOW
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:
1 import java.awt.*;
2 import java.awt.event.*;
3
4 public class MyFrame extends Frame
5 {
6 MyFrame()
7 {
8 setSize(600, 400);
9 setTitle("My Application");
10 setVisible(true);
11 addWindowListener(new WindowAdapter()
12 {
13 public void windowClosing(WindowEvent we)
14 {
15 System.exit(0);
16 }
17 }
18 );
19 }
20 public void paint(Graphics g)
21 {
22 g.drawLine(20, 60, 80, 100);
23 g.drawRect(100, 60, 80, 40);
24 g.fillRect(200, 60, 80, 40);
25 g.drawRoundRect(300, 60, 80, 40, 20, 20);
26 g.fillRoundRect(400, 60, 80, 40, 20, 20);
27 g.drawOval(20, 120, 80, 40);
28 g.fillOval(120, 120, 80, 40);
29 g.drawArc(220, 120, 80, 40, 90, -90);
30 g.fillArc(320, 120, 80, 40, 90, -90);
31 int[] x = {20, 100, 80, 20};
32 int[] y = {200, 180, 240, 260};
33 g.drawPolygon(x, y, 4);
34 int[] fillx = {120, 200, 180, 120};
35 int[] filly = {200, 180, 240, 260};
36 g.fillPolygon(fillx, filly, 4);
37 }
38 public static void main(String[] args)
39 {
40 MyFrame mf = new MyFrame();
41 }
42 }
(http://www.startertutorials.com/corejava/wp-
content/uploads/2016/04/java-awt-graphics.png)
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 rgbValue)
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:
Color getColor()
1 import java.awt.*;
2 import java.awt.event.*;
3
4 public class MyFrame extends Frame
5 {
6 MyFrame()
7 {
8 setSize(600, 400);
9 setTitle("My Application");
10 setVisible(true);
11 addWindowListener(new WindowAdapter()
12 {
13 public void windowClosing(WindowEvent we)
14 {
15 System.exit(0);
16 }
17 }
18 );
19 }
20 public void paint(Graphics g)
21 {
22 g.setColor(Color.GREEN);
23 g.drawLine(20, 60, 80, 100);
24 g.drawRect(100, 60, 80, 40);
25 g.setColor(Color.RED);
26 g.fillRect(200, 60, 80, 40);
27 g.drawRoundRect(300, 60, 80, 40, 20, 20);
28 g.setColor(Color.BLUE);
29 g.fillRoundRect(400, 60, 80, 40, 20, 20);
30 g.drawOval(20, 120, 80, 40);
31 g.setColor(Color.ORANGE);
32 g.fillOval(120, 120, 80, 40);
33 g.drawArc(220, 120, 80, 40, 90, -90);
34 g.setColor(Color.MAGENTA);
35 g.fillArc(320, 120, 80, 40, 90, -90);
36 int[] x = {20, 100, 80, 20};
37 int[] y = {200, 180, 240, 260};
38 g.drawPolygon(x, y, 4);
39 g.setColor(Color.CYAN);
40 int[] fillx = {120, 200, 180, 120};
41 int[] filly = {200, 180, 240, 260};
42 g.fillPolygon(fillx, filly, 4);
43 }
44 public static void main(String[] args)
45 {
46 MyFrame mf = new MyFrame();
47 }
48 }
content/uploads/2016/04/awt-graphics-colors.png)
Related Links:
Basic Coding
Java Programing
And Paint
Ad T imesJobs
Passing Parameters to
Applets - Core java
tutorial for beginners
startertutorials.com
Introduction to Swing -
Core java tutorial for
beginners
startertutorials.com
Event Handling
Overview - Core java
tutorial for beginners
startertutorials.com
Introduction to AWT -
Core java tutorial for
beginners
startertutorials.com
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 *
Your comment
Your name
Post Comment
OOP Basics
Java Basics
String Handling
Inheritance
Interfaces
Packages
Exception Handling
Multi Threading
Applets
Event Handling
AWT
Swing
Useful Links
Java Programs (https://www.startertutorials.com/blog/java-programs-interviews.html)
Starter tutorials (https://www.startertutorials.com)
Sitemap (https://www.startertutorials.com/corejava/sitemap_index.xml)
Search
Pages
About (https://www.startertutorials.com/corejava/)
Contact Me (https://www.startertutorials.com/corejava/contact-me)
Privacy Policy (https://www.startertutorials.com/corejava/privacy-policy)
Resources (https://www.startertutorials.com/corejava/resources)
Terms of Service (https://www.startertutorials.com/corejava/terms-of-service)
Follow us on Facebook
Starter Tutorials
प ृआ वड ले
हेआवडणारे
आप या म ां
पक
ै सव थम
हा
Follow Starter Tutorials
Starter Tutorials
google.com/+Startertutorials
फॉल ो कर ा
Recent Posts
ArrayList in Java (https://www.startertutorials.com/corejava/arraylist-in-java.html)
Introduction to Java Collections Framework (https://www.startertutorials.com/corejava/introduction-java-collections-framework.html)
Swing Controls (https://www.startertutorials.com/corejava/swing-controls.html)
Introduction to Swing (https://www.startertutorials.com/corejava/introduction-to-swing.html)
Layout Managers (https://www.startertutorials.com/corejava/layout-managers.html)
2018 Core java tutorial for beginners (https://www.startertutorials.com/corejava) Powered by WordPress (http://WordPress.org)
Dashboard (https://www.startertutorials.com/corejava/wp-admin/)