Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setBackground(new color()); in java does not understand the given RGB value

I have a program with some gui, on the JFrame I set,

 setBackground( new Color(107, 106, 104) );

The issue is that I get a greyish color, but not the right one. If I check it in PhotoShop, it gives me the RGB values (126, 125, 123)

Ps. I have tried with HEX value, the same result.

like image 711
JW_ Avatar asked Mar 14 '12 08:03

JW_


People also ask

How do you create a new color in Java?

To create your own color: Paint - Double click on any color at the bottom of the screen. - Choose "Define Custom Colors". - Select a color and/or use the arrows to achieve the desired color. are the numbers needed to create your new Java color.

What is the RGB () color function?

Definition and Usage. The rgb() function define colors using the Red-green-blue (RGB) model. An RGB color value is specified with: rgb(red, green, blue). Each parameter defines the intensity of that color and can be an integer between 0 and 255 or a percentage value (from 0% to 100%).

Why is RGB out of 255?

Since 255 is the maximum value, dividing by 255 expresses a 0-1 representation. Each channel (Red, Green, and Blue are each channels) is 8 bits, so they are each limited to 256, in this case 255 since 0 is included.


2 Answers

I have a program with some gui, on the JFrame I set,

 setBackground( new Color(107, 106, 104) );

[The problem] It gives a greyish color, but not the right one! 
If I check the gui's color in Photo Shop, it gives me the RGB 
values (126, 125, 123)

you can not set setBackground for JFrame, this is only possible for ContentPane, for example

JFrame#getContentPane.setBackground(new Color(107, 106, 104));

EDIT

enter image description here

from code

import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class Check extends JFrame {

    private static final long serialVersionUID = 1L;

    public void makeUI() {
        JFrame f = new JFrame();
        f.getContentPane().setBackground(new Color(107, 106, 104));
        f.setDefaultCloseOperation(EXIT_ON_CLOSE);
        f.setSize(new Dimension(300, 200));
        f.setVisible(true);
    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Check().makeUI();
            }
        });
    }
}
like image 118
mKorbel Avatar answered Nov 15 '22 00:11

mKorbel


check with Adam's comment and even if not worked then without any working code I am just guessing that this scenario is getting raised due zero ordering or saying layout of the JFrame. Actually in java swing , setting the background color needs a little bit of more attention, check Swing Java Docs.

like image 20
Abhishek Choudhary Avatar answered Nov 14 '22 22:11

Abhishek Choudhary