Example 1 Draw and Fill a Rectangle Using Java

suggest change

This is an Example which print rectangle and fill color in the rectangle. https://i.stack.imgur.com/dlC5v.jpg

Most methods of the Graphics class can be divided into two basic groups:

  1. Draw and fill methods, enabling you to render basic shapes, text, and images
  2. Attributes setting methods, which affect how that drawing and filling appears

Code Example: Let us start this with a little example of drawing a rectangle and filling color in it. There we declare two classes, one class is MyPanel and other Class is Test. In class MyPanel we use drawRect( ) & fillRect( ) mathods to draw rectangle and fill Color in it. We set the color by setColor(Color.blue) method. In Second Class we Test our graphic which is Test Class we make a Frame and put MyPanel with p=new MyPanel() object in it.By running Test Class we see a Rectangle and a Blue Color Filled Rectangle.

First Class: MyPanel

import javax.swing.*;
import java.awt.*;
// MyPanel extends JPanel, which will eventually be placed in a JFrame
public class MyPanel extends JPanel { 
    // custom painting is performed by the paintComponent method
    @Override
    public void paintComponent(Graphics g){
        // clear the previous painting
        super.paintComponent(g);
        // cast Graphics to Graphics2D
        Graphics2D g2 = (Graphics2D) g;
        g2.setColor(Color.red); // sets Graphics2D color
        // draw the rectangle
        g2.drawRect(0,0,100,100); // drawRect(x-position, y-position, width, height)
        g2.setColor(Color.blue);
        g2.fillRect(200,0,100,100); // fill new rectangle with color blue
    } 
}

Second Class: Test

import javax.swing.;
import java.awt.;    
public class Test { //the Class by which we display our rectangle    
    JFrame f;    
    MyPanel p;    
    public Test(){    
        f = new JFrame();    
        // get the content area of Panel.    
        Container c = f.getContentPane();    
        // set the LayoutManager
        c.setLayout(new BorderLayout());        
        p = new MyPanel();    
        // add MyPanel object into container    
        c.add(p); 
        // set the size of the JFrame
        f.setSize(400,400);    
        // make the JFrame visible
        f.setVisible(true);    
        // sets close behavior; EXIT_ON_CLOSE invokes System.exit(0) on closing the JFrame
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    
    }       
    
    public static void main(String args[ ]){    
        Test t = new Test();     
    } 
}

For More Explanation about Border Layout: https://docs.oracle.com/javase/tutorial/uiswing/layout/border.html

paintComponent( )

• It is a main method for painting

• By default, it first paints the background

• After that, it performs custom painting (drawing circle, rectangles etc.)

Graphic2D refers Graphic2D Class

Note: The Java 2D API enables you to easily perform the following tasks:

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


2D graphics:
* Example 1 Draw and Fill a Rectangle Using Java

Table Of Contents
8 Arrays
10 Maps
11 Strings
25 JAXB
29 Enums
32 Audio
41 Scanner
63 Logging
75 Lists
78 Sets
89 JAX-WS
96 XJC
98 Process
106 Modules
114 Applets
122 JNDI
139 JavaBean
141 Literals
144 Packages
150 JMX
153 JShell
159 Sockets
164 2D graphics
167 Enum Map
175 Hashtable
177 SortedMap