Java - 如何将 Color.toString() 转换为颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2394388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java - How to convert a Color.toString() into a Color?
提问by Amokrane Chentir
In order to save Color attributes of a graphical object in my application, I saved the string representation of this Color in a data file.
For instance, for red I save: java.awt.Color[r=255,g=0,b=0]
.
How can I convert this string representation into a Color so that I can use it again after loading my data file ?
为了在我的应用程序中保存图形对象的颜色属性,我将此颜色的字符串表示保存在数据文件中。例如,对于红色,我保存:java.awt.Color[r=255,g=0,b=0]
。如何将此字符串表示形式转换为颜色,以便在加载数据文件后可以再次使用它?
Thank you.
谢谢你。
采纳答案by trashgod
Using toString()
"might vary between implementations." Instead save String.valueOf(color.getRGB())
for later reconstruction.
使用toString()
“可能因实现而异”。而是保存String.valueOf(color.getRGB())
以供以后重建。
回答by polygenelubricants
From the documentation of Color#toString
从文档 Color#toString
Returns a string representation of this
Color
. This method is intended to be used only for debugging purposes. The content and format of the returned string might vary between implementations.The returned string might be empty but cannot benull
.
返回 this 的字符串表示形式
Color
。此方法仅用于调试目的。返回字符串的内容和格式可能因实现而异。返回的字符串可能为空,但不能为null
。
In other words, I wouldn't rely on being able to back-convert the string to the Color
. If you insist on doing this, however, you can try to parse the numbers out of the string and hope that it will work with no guarantees.
换句话说,我不会依赖于能够将字符串反向转换为Color
. 但是,如果您坚持这样做,您可以尝试解析字符串中的数字,并希望它可以在没有保证的情况下正常工作。
Something like this seems to work for ME for NOW:
像这样的事情现在似乎对我有用:
Scanner sc = new Scanner("java.awt.Color[r=1,g=2,b=3]");
sc.useDelimiter("\D+");
Color color = new Color(sc.nextInt(), sc.nextInt(), sc.nextInt());
I don't recommend actually doing this, however.
但是,我不建议实际执行此操作。
回答by Peter vdL
The easiest thing is to rethink the way you store the string representation. Get rid of all the labeling, and just store red as the string "0xFF0000". Then you can easily parse that string to get the single value for rgb, and send it to the Color constructor.
最简单的方法是重新考虑存储字符串表示的方式。去掉所有标签,只将红色存储为字符串“0xFF0000”。然后您可以轻松解析该字符串以获取 rgb 的单个值,并将其发送到 Color 构造函数。
The alternative is to parse the more complicated string as you are now saving it "java.awt.Color[r=255,g=0,b=0]".
另一种方法是解析更复杂的字符串,因为您现在将其保存为“java.awt.Color[r=255,g=0,b=0]”。
You can see the constructors for Color here: http://java.sun.com/javase/6/docs/api/(search "all classes" for Color).
您可以在此处查看 Color 的构造函数:http: //java.sun.com/javase/6/docs/api/(在“所有类”中搜索 Color)。
Peter
彼得
回答by crazyscot
I suggest you look into java's built-in serialisation technology instead. (I note that Color implements Serializable.)
我建议您改为查看 java 的内置序列化技术。(我注意到 Color 实现了 Serializable。)
回答by iter
You may wish to use getRGB()
instead of toString()
. You can call
您可能希望使用getRGB()
代替toString()
。你可以打电话
String colorS = Integer.toString(myColor.getRGB());
String colorS = Integer.toString(myColor.getRGB());
Then you can call
然后你可以打电话
Color c = new Color(Integer.parseInt(colorS));
Color c = new Color(Integer.parseInt(colorS));
回答by camickr
Use the getRGB() method to get the int representation of the Color, then you save the int value and recreate the Color using that value. No parsing needed.
使用 getRGB() 方法获取 Color 的 int 表示,然后保存 int 值并使用该值重新创建 Color。不需要解析。
回答by Bozho
Don't use the toString()
. Use getRGB()
/ new Color(rgb)
to save/restore the value of the color.
不要使用toString()
. 使用getRGB()
/new Color(rgb)
来保存/恢复颜色的值。
回答by Christian
Stephan's answer helped me with this. However, I found that I needed to add a 'true' to the syntax in order to restore the color.
斯蒂芬的回答帮助我解决了这个问题。但是,我发现我需要在语法中添加一个“true”才能恢复颜色。
// convert to string
String colorS = Integer.toString(myColor.getRGB());
// restore colour from string
Color c = new Color(Integer.parseInt(colorS), true);