How to Get Darker Color Version than the Current in Java


In this example we will show how to get darker version of current color with AWT Color class in Java.

Source Code

package com.beginner.examples;

import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class GetDarkerColorExample {

	public static void main(String[] args) {

		Frame frame = new Frame("Show Color");
		frame.setBounds(200, 200, 200, 200);
		frame.setLayout(new FlowLayout());

		// Create a color with RGB
		Color color = new Color(255, 23, 40);
		// Set this color to the background color of the form
		frame.setBackground(color);

		Button btn = new Button("Darker Color");

		// Set the background color of this button to the darker version
		btn.setBackground(color.darker());
		
		btn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				System.exit(0);

			}
		});

		frame.add(btn);

		frame.setVisible(true);

	}

}

Output:

![](Output.png)

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments