(Java)让程序显示十六进制颜色代码的所有可能组合.有时可以让字符串显示char和int吗?



我知道标题没有问得很好…抱歉。你可能知道,十六进制颜色代码可以包含数字和字符(#ff0000)我先写了一个小程序它列出了所有可能的组合(16^6)并把它们写进去控制台。这确实有效。现在我想将j帧的背景颜色更改为当前在控制台中以十六进制代码形式编写的颜色。Java似乎只接受RGB组合,所以我使用Color col = Color.decode(HexCode)得到这些RGB值,这是有效的。现在我想把这些组合成"程序"在这一点上,j帧只显示一种颜色,我直接放在代码中Color col = Color.decode(#ff0000)这不是我的最终目标。

我有十六进制代码的位置定义为#abcdef(作为int)但要使解码工作,有时不仅需要数字,还需要字符。这就是我的问题,一个人如何做到这一点?

这是我现在的代码:

package main;
import java.awt.*;
import javax.swing.*;
public class Test3 {

public static void main(String[] args) {
int a,b,c,d,e,f;
JFrame jf = new JFrame("Dima");
jf.setVisible(true);
jf.setSize(100,100);
jf.setLocationRelativeTo(null);

for(a=0;a<=15;a++) {
for(b=0;b<=15;b++) {
for(c=0;c<=15;c++) {
for(d=0;d<=15;d++) {
for(e=0;e<=15;e++) {
for(f=0;f<=15;f++) {

//Hex Indikator
System.out.print("#");         
//Konsolenprint von a              
if( a == 15 ) {
System.out.print("f");}else
if ( a == 14 ) {
System.out.print("e");}else
if ( a == 13 ) {
System.out.print("d");}else
if ( a == 12 ) {
System.out.print("c");}else
if ( a == 11 ) {
System.out.print("b");}else
if ( a == 10 ) {
System.out.print("a");}else{
System.out.print(a);
}
//Konsolenprint von b        
if( b == 15 ) {
System.out.print("f");}else
if ( b == 14 ) {
System.out.print("e");}else
if ( b == 13 ) {
System.out.print("d");}else
if ( b == 12 ) {
System.out.print("c");}else
if ( b == 11 ) {
System.out.print("b");}else
if ( b == 10 ) {
System.out.print("a");}else {
System.out.print(b);
}
//Konsolenprint von c        
if( c == 15 ) {
System.out.print("f");}else
if ( c == 14 ) {
System.out.print("e");}else
if ( c == 13 ) {
System.out.print("d");}else
if ( c == 12 ) {
System.out.print("c");}else
if ( c == 11 ) {
System.out.print("b");}else
if ( c == 10 ) {
System.out.print("a");}else {
System.out.print(c);
}
//Konsolenprint von d        
if( d == 15 ) {
System.out.print("f");}else
if ( d == 14 ) {
System.out.print("e");}else
if ( d == 13 ) {
System.out.print("d");}else
if ( d == 12 ) {
System.out.print("c");}else
if ( d == 11 ) {
System.out.print("b");}else
if ( d == 10 ) {
System.out.print("a");}else {
System.out.print(d);
}
//Konsolenprint von e        
if( e == 15 ) {
System.out.print("f");}else
if ( e == 14 ) {
System.out.print("e");}else
if ( e == 13 ) {
System.out.print("d");}else
if ( e == 12 ) {
System.out.print("c");}else
if ( e == 11 ) {
System.out.print("b");}else
if ( e == 10 ) {
System.out.print("a");}else {
System.out.print(e);
}  
//Konsolenprint von f        
if( f == 15 ) {
System.out.println("f");}else
if ( f == 14 ) {
System.out.println("e");}else
if ( f == 13 ) {
System.out.println("d");}else
if ( f == 12 ) {
System.out.println("c");}else
if ( f == 11 ) {
System.out.println("b");}else
if ( f == 10 ) {
System.out.println("a");}else {
System.out.println(f);
}    
String ColorCode = "#";


//System.out.println(" Hier ist das Problem : " + ColorCode);

//Color col = Color.decode(HexCode);
//jf.getContentPane().setBackground(col);

}
} 
}
}
}       
}
}
}


我希望你能理解我的问题,这对我来说有点困难:/

我尝试让字符串引用其他字符串,并尝试设置字符串在if函数中输入一个字符,但是没有成功。

这是有效的,我将稍后尝试g00se的想法,但这工作得很好:

import java.awt.Color;
import javax.swing.*;
class Test4 {
public static void main(String[] args) {
JFrame jf = new JFrame();
jf.setVisible(true);
jf.setSize(500,500);
for (int red = 0; red <= 255; red++) {
for (int green = 0; green <= 255; green++) {
for (int blue = 0; blue <= 255; blue++) {
String redHex = String.format("%02x", red);
String greenHex = String.format("%02x", green);
String blueHex = String.format("%02x", blue);
String colorCode = "#" + redHex + greenHex + blueHex;
System.out.println(colorCode);

Color col = Color.decode(colorCode);
jf.getContentPane().setBackground(col);
}
}
}
}
}

谢谢你的帮助和想法:)

最新更新