Draw rectangle using Java2D

Draw rectangle using Java2D


Posted in : Core Java Posted on : October 29, 2010 at 5:07 PM Comments : [ 0 ]

This section contains the detail about the Java2D in java swing.

Draw rectangle using Java2D

In Java2D, for creating rectangle we can use "drawRect()" method. The "fillRect()" method can be used to fill rectangle with the current color. For setting the current color, we can use "setColor()" method.

Example

In the given below example, we draw nine colored rectangles :

package corejava;

import java.awt.*;
import javax.swing.*;

public class Java2DRectangle extends JPanel {
	public void paintComponent(Graphics g) {
		super.paintComponent(g);

		Graphics2D g2d = (Graphics2D) g;

		g2d.setColor(new Color(212, 212, 212));
		g2d.drawRect(10, 15, 90, 60);
		g2d.drawRect(130, 15, 90, 60);
		g2d.drawRect(250, 15, 90, 60);
		g2d.drawRect(10, 105, 90, 60);
		g2d.drawRect(130, 105, 90, 60);
		g2d.drawRect(250, 105, 90, 60);
		g2d.drawRect(10, 195, 90, 60);
		g2d.drawRect(130, 195, 90, 60);
		g2d.drawRect(250, 195, 90, 60);

		g2d.setColor(new Color(125, 167, 116));
		g2d.fillRect(10, 15, 90, 60);

		g2d.setColor(new Color(42, 179, 231));
		g2d.fillRect(130, 15, 90, 60);

		g2d.setColor(new Color(70, 67, 123));
		g2d.fillRect(250, 15, 90, 60);

		g2d.setColor(new Color(130, 100, 84));
		g2d.fillRect(10, 105, 90, 60);

		g2d.setColor(new Color(252, 211, 61));
		g2d.fillRect(130, 105, 90, 60);

		g2d.setColor(new Color(241, 98, 69));
		g2d.fillRect(250, 105, 90, 60);

		g2d.setColor(new Color(217, 146, 54));
		g2d.fillRect(10, 195, 90, 60);

		g2d.setColor(new Color(63, 121, 186));
		g2d.fillRect(130, 195, 90, 60);

		g2d.setColor(new Color(31, 21, 1));
		g2d.fillRect(250, 195, 90, 60);

	}

	public static void main(String[] args) {

		Java2DRectangle rects = new Java2DRectangle();
		JFrame frame = new JFrame("Rectangles");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.add(rects);
		frame.setSize(360, 300);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
	}

}

Output :

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics