Wednesday, April 14, 2021

[Java Code Sample] How to use RGB color

 

Here's a sample code to use RGB color for objects:


Video tutorial:



Code:


import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JFrame;

public class Main {
	
	JFrame window;
	JButton button;

	public static void main(String[] args) {
		
		new Main();

	}
	public Main(){
		
		window = new JFrame();
		window.setSize(800,600);
		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		window.getContentPane().setBackground(Color.black);
		window.setLayout(null);
		window.setVisible(true);
				
		button = new JButton();
		button.setBounds(250, 250, 300, 100);
        // Type RGB color code
		button.setBackground(new Color(127,55,34));
		window.add(button);
	}
}


Here's the result:


The button background is painted by the specified RGB color (127,55,34)

No comments:

Post a Comment