Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

Manipulating colors is fundamental to graphical programming, especially when designing user interfaces or graphical applications. The setColor() method, provided by Java’s Graphics class, is essential for setting the appearance of shapes, text, or other graphical elements.

To achieve the desired color, programmers often use RGB (Red, Green, Blue) values. In this article, we’ll learn how to add RGB values into setColor() in Java, including how to create custom shades.

2. Understanding RGB in Java

RGB, which stands for Red, Green, and Blue, represents the primary light components used in digital displays. By varying the intensity of each of these components, we can generate a wide range of colors perceivable by the human eye.

In Java, each channel (Red, Green, and Blue) is represented by an integer value ranging from 0 to 255, where:

  • 0 represents no intensity (complete absence of that color)
  • 255 represents full intensity

The combination of these three values determines the final color. For example:

  • Red: (255, 0, 0)
  • Green: (0, 255, 0)
  • Blue: (0, 0, 255)
  • Black: (0, 0, 0)
  • White: (255, 255, 255)

In Java, the Color class encapsulates these RGB values and provides a way to define colors programmatically. Additionally, Java supports transparency through an alpha channel, which allows defining colors with varying degrees of opacity.

3. Adding RGB Values into setColor()

In Java graphics programming, combining RGB values with the setColor() method allows control over the appearance of various graphical elements. To effectively manage colors in our graphics, we need to understand how to create and apply Color objects using RGB values. The following sections will guide us through these steps.

3.1. Creating a Color Object

The first step in using RGB values with the setColor() method is to create a Color object. The Color class in Java provides constructors that accept three parameters corresponding to the red, green, and blue components:

Color myWhite = new Color(255, 255, 255);

In this example, “myWhite” is a Color instance representing the color white. By adjusting the RGB values, we can create various colors to suit our application’s needs. For instance, to create a shade of purple, we can use:

Color myPurple = new Color(128, 0, 128);

3.2. Applying the Color with setColor()

Once we’ve created a Color object, the next step is to apply it to our graphical context using the setColor() method of the Graphics class. This method sets the color for all subsequent drawing operations:

graphic.setColor(myWhite);

In this example, “graphic” is an instance of the Graphics class. After calling setColor(myWhite), any shapes or text drawn will use the specified color. This allows us to manage and apply colors consistently throughout the graphical operations.

3.3. Example of Applying Multiple Colors

Let’s explore how to use multiple colors by creating and applying different Color objects as needed:

Color redColor = new Color(255, 0, 0);
graphic.setColor(redColor); // Sets the drawing color to red
graphic.fillRect(10, 10, 100, 100); // Draws a red rectangle

Color blueColor = new Color(0, 0, 255);
graphic.setColor(blueColor); // Sets the drawing color to blue
graphic.fillRect(120, 10, 100, 100); // Draws a blue rectangle

In this example, redColor and blueColor are Color objects representing red and blue colors, respectively. The setColor() method is used to switch between these colors for different drawing operations. The rectangles drawn by graphic.fillRect() will appear in red and blue according to the order in which the colors are set.

4. Conclusion

To effectively use RGB values with setColor(), we created a Color object with the desired RGB values. Then we applied this color using setColor() to control the appearance of subsequent graphical operations.

By mastering this technique, we can effectively control colors in our Java graphical applications, which helps us enhance our designs’ visual quality and functionality.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW