In this section, you will see how to draw colorful shapes in java swing.
Drawing with Color in Java
In this section, you will see how to draw colorful shapes in java swing. There are various colorful shapes have been drawn in the given program. This program has used various types of methods to draw shapes and fill these with the appropriate color. In this tutorial we are going to create JFrame and then write the shapes on it. We are using the draw method to draw these shapes. This tutorial will teach you to use the various methods to draw shapes on the screen.
The drawRect() is the method that helps to draw a rectangle on the JFrame. You can pass the x,y and height, width to this function. You can pass the predefined value to this function or use the dynamic methods to calculate the values and then pass to the drawRect() method. The Graphics object also provides methods like drawOval(), fillRect(), fillOval() and other for drawing various shapes on the screen. You can use these methods to draw shapes on the JFrame as per your program requirements. This class is present in the package java.awt.Graphics and comes with many methods that can be used by developers for graphics programming. This tutorial is presenting example for drawing rectangle and oval shape on the JFrame.
Following are some methods are using in the given program to complete the required result.
g.drawRect(10,10,height,width); g.setColor(Color.gray); g.fillRect(11,11,height,width); g.setColor(Color.red); g.drawOval(250,20, height,width); g.setColor(Color.magenta); g.fillOval(249,19,height,width);
Now see the output of the program. If you run the program you will get screen shown below below:
drawRect():
This is the method of the Graphics class
(The Graphics class is used to drawing different-different type of
shapes). This method draws the rectangle. It takes some integer value as
parameter. This method is written like : Graphics.drawRect(x,
y, height, width);.
x - This is the variable represents the row no. or the
x - coordinate.
y - This is also a variable represents the column no. or the y - coordinate.
drawOval():
This is the method of the Graphics class
which draws the oval on the frame. This method takes argument same as the
drawRect() method. In this method first come the width and then height is specified.
fillRect():
This is the method of the Graphics class which is
used to fill rectangle with the specified color which is set before using the
setColor() method of the Graphics class. It also takes argument same as
the drawRect() method.
fillOval():
This is also the method of the Graphics
class which is used to fill the oval with color specified in the setColor()
method before. This method also takes argument same as the drawOval() method.
Here is the code of program:
import javax.swing.*;
|
Here we are providing the example code which you can download it and run on your computer. The download url is: Download this example.
In this tutorial we have learned to draw rectangle and oval shape on the JFrame in a swing program.