eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

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 – LJB – NPI EA (cat = Core Java)
announcement - icon

Code your way through and build up a solid, practical foundation of Java:

>> Learn Java Basics

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.
Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

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 – LS – NPI (cat=Java)
announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

eBook Jackson – NPI EA – 3 (cat = Jackson)