2014-05-05 48 views
-7
JColorChooser.showDialog(tEkran, "Select a Color", selectedColorBG); 

選擇顏色後,我需要將其轉換爲3變量。像這樣:如何從顏色變量獲取RGB值

colorR = selectedColorBG.getR 

colorG = selectedColorBG.getG 

colorB = selectedColorBG.getB 

有沒有辦法做到這一點?

+3

你們倆呃在發佈這個問題之前查看文檔? http://docs.oracle.com/javase/7/docs/api/java/awt/Color.html –

+0

你甚至看過Java API嗎? http://docs.oracle.com/javase/7/docs/api/javax/swing/JColorChooser.html#getColor%28%29 – Brian

回答

4

JColorChoosergetColor方法返回一個Color它擁有一些方法getRedgetGreengetBlue分別

1

你必須處理的返回值:

Color selectedColor = JColorChooser.showDialog(tEkran, "Select the color", initialColor); 
int red = selectedColor.getRed(); 
int green = selectedColor.getGreen(); 
int blue = selectedColor.getBlue(); 
+0

謝謝,很多... – Murat

0

一看java.awt.Color documentation會回答你的問題:

int getAlpha() 
    Returns the alpha component in the range 0-255. 
int getBlue() 
    Returns the blue component in the range 0-255 in the default sRGB space. 
int getGreen() 
    Returns the green component in the range 0-255 in the default sRGB space. 
int getRed() 
    Returns the red component in the range 0-255 in the default sRGB space. 
+0

謝謝。很多... – Murat