Java 2D Graphics Example Tutorial On Paint, Gradient and Stroke

Java 2D is an API for drawing two-dimensional graphics using the Java programming language. Every Java 2D drawing operation can ultimately be treated as filling a shape using a paint and compositing the result onto the screen. The following example will demonstrate:

  • — How to define JAVA 2D Shape
  • — How to define Gradient Paint
  • — How to set Graphics paint
  • — How to relocate the java 2D frame for redrawing
  • — How to set the strokes in the Graphics
  • — How to fill shapes with color or gradient

You will also be learning

  • — How to center your GUI on the screen automatically.

 * Java 2D Graphics Example Tutorial On Paint, Gradient and Stroke
 * @author Kushal Paudyal
 * Last Modified On 10th August 2009

import java.awt.*;
import java.awt.geom.*;

import javax.swing.JFrame;

public class JavaPaintAndStroke extends JFrame {

	private static final long serialVersionUID = 7944800805864455013L;

	public static void main(String[] args) {
		JavaPaintAndStroke paintFrame = new JavaPaintAndStroke();
		paintFrame.setTitle(" - GUI Tutorial");
		paintFrame.setSize(350, 200);


	public void paint(Graphics g) {
		Graphics2D g2 = (Graphics2D) g;
		double x = 20, y = 60, w = 80, h = 80;
		/** Defining a rectangle and a gradient **/
		Rectangle2D r = new Rectangle2D.Double(x, y, w, h);
		GradientPaint gp = new GradientPaint(75, 75,, 95, 95,, true);

		 * Filling the rectangle with a gradient paint

		 * Shifting the frame to the right by 100
		 * Then using Stroke With a Solid Color
		 * setFrame method below sets the location and size of
		 * the outer bounds of this Rectangle2D to the
		 * specified rectangular values.
		 * x, y locations
		 * w, h width and height of new Rectangle frame
		r.setFrame(x + 100, y, w, h);
		g2.setStroke(new BasicStroke(8));

		 * Shifting the original rectangle frame to the right by 200
		 * Then using a Stroke With a Gradient Color
		r.setFrame(x + 200, y, w, h);

	 * This method is used to center the GUI
	 * @param frame - Frame that needs to be centered.
	public void centerTheGUIFrame(JFrame frame) {
		int widthWindow = frame.getWidth();
		int heightWindow = frame.getHeight();

		Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
		int X = (screen.width / 2) - (widthWindow / 2); // Center horizontally.
		int Y = (screen.height / 2) - (heightWindow / 2); // Center vertically.

		frame.setBounds(X, Y, widthWindow, heightWindow);


The following is the output of this program.

[My Video Promotion]
  1. Java Object Serialization and Deserialization In MySQL Database
  2. Flipping An Image Horizontally and Vertically In Java
  3. How To Set Conditional Debug Breakpoints in Eclipse or IBM RAD?
  4. Rotating An Image In Java Graphics 2D By Specified Degree
  5. Java – How To Overlay One Image Over Another Using Graphics2D [Tutorial]
  6. Quick SQL Reference – Find Duplicate Data In A Table Using Having Clause
  7. Splitting PDF File Using Java iText API Into Multiple PDFs
  8. Object Serialization And De-Serialization In Java To Filesystem
  9. Finding Java Image Pixels Information – ARGB (Alpha, Red, Green Blue)
  10. Creating A 3D Pie Chart In Java Using JFreeChart [Example Source Code]
  11. How to capture a screenshot using Java and save it to a file?
  12. Java Tutorial – Swing Text Drag And Drop – Sample Example
  13. Serialzing A Java Object Into XML and De-Serialzing using XMLEncoder And XMLDecoder
  14. Six Stages Of Debugging in Software Engineering
  15. Java 2D Graphics Example Tutorial On Paint, Gradient and Stroke
  16. Complete Tutorial On Using SOAP-UI to Mock Web Service Request / Response
  17. Listing The Content Of Zip File With Zip Information In Java
  18. Joke – Why do Java Programmers wear glasses?
  19. How to receive files via bluetooth in your MacBook Pro
  20. Calculating Folder Size Graphically and Generating Directory Size Chart In Java
Tagged , , , , , , , . Bookmark the permalink.

6 Responses to Calculating Folder Size Graphically and Generating Directory Size Chart In Java

  1. nocdib says:

    Thank you for this! It is just what I need

  2. d says:

    would that help you tocreate the file from ubunt server

  3. komal gaikwad says:

    in this code i got problem of array out of bound plz tell me solution.

  4. komal gaikwad says:

    In this code i got error
    Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 1
    plz send me proper code….

  5. Neethu says:

    Thank you very much for the solution. This is the one which i was searching.

  6. Manikant says:

    I need to copy to the shared folder which is shared and can be authenticate by using credentials.

    but here the code is giving to write .

    I need copy my folder from my local to shared location

    Please help

Leave a Reply to komal gaikwad Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.