hey is there any way to make this code to load color from config.properties?
font.setForeground(new Color(configi.getProperty("fonts_colo")));
my config.properties
fonts_colo=44, 44, 44
Is there any good way to do that?
You have 2 options:
Parse the input string from properties
var colFromProp = "44, 44, 44";
var rgb = Arrays.stream(colFromProp.split(","))
.map(String::trim)
.mapToInt(Integer::parseInt)
.toArray();
var c = new Color(rgb[0], rgb[1], rgb[2]);
Use another constructor
Instead of taking a list of ints, you can use a constructor that takes a single int (typically given in hex format)
var colFromProp = "0x2c2c2c";
var c = new Color(Integer.decode(colFromProp));
or
var colFromProp = "2c2c2c";
var c = new Color(Integer.parseInt(colFromProp, 16));
Well if you don't use a library which has some kind of config support you have to do that yourself. For this purpose I'd recommend you to write a own class (Config.java eg) and load the whole config in the constructor (each line consists of key=value, you could use an extra Class "Attribute" for this pair, in which you could even implement to parse the value data, for example:
...
if(value!=null&&key!=null&&key.equals("fonts_colo")){
String[] parts = value.split(", ");
assert(parts!=null&&parts.length == 3);
Color c = new Color(parts[0], parts[1], parts[2]);
//TODO: do something with the color
}
...
This way you are able to load more than one property, it is not too much work and you can reuse that class. If you actually want to use a library you could do something JSON based eg (even xml if your config gets more complicated) I hope I could help:)
EDIT: possible implementation of such a class
public class Config {
private HashMap<String, Property> properties = new HashMap<>();
public Config(File file) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(file));
for(String line; (line = br.readLine()) != null;){
if(line.contains("=")) {
String[] parts = line.split("=");
assert(parts.length==2);
properties.put(parts[0], new Property(parts[1]));
}
}
br.close();
}
public Property getProperty(String key) {
return properties.get(key);
}
/**
* Returns a color represented by the property value of the given key.
* @param key the key of the property
* @return the color, if the value had the form "r, g, b" with r,g,b as integers, else null
*/
public Color getPropertyAsColor(String key) {
Property p = getProperty(key);
if(p == null || p.valueparts == null || p.valueparts.length != 3) return null;
return new Color(Integer.parseInt(p.valueparts[0]),
Integer.parseInt(p.valueparts[1]),
Integer.parseInt(p.valueparts[2]));
}
private class Property{
private String value;
private String[] valueparts;
public Property(String value) {
if(value.contains(","))
valueparts = value.split(",");
this.value = value;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String[] getValues() {
if(valueparts == null)
return new String[] {value};
else return valueparts;
}
}
}
with that you could do that:
Config c = new Config(new File("config.properties"));
...
font.setForeground(c.getPropertyAsColor("fonts_color"));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.