How to set string color?

hi,

How do i set the color of a string without not having to do anything like this.

publicvoid paintComponent(Graphics g)

{

g.setColor(Color.RED);

g.drawString("Just a test");

It would be easier if i could do something like this:

String s ="just a test";

//s.setColor(Color.RED);

//or Color.setColor(s);

//I'm just brainstorming :)

Greetz Daan

[654 byte] By [D-a-a-na] at [2007-11-27 3:35:33]
# 1

Color is not a String property. You will have to continue to do things like the code you posted or use components that support styled attributes like JEditorPane or JTextPane.

Tutoiral: http://java.sun.com/docs/books/tutorial/uiswing/components/editorpane.html

Message was edited by:

DrLaszloJamf

DrLaszloJamfa at 2007-7-12 8:38:44 > top of Java-index,Java Essentials,New To Java...
# 2
something like this?jLabel1.setForeground(new java.awt.Color(f, 0, 0));}/Mattias
javaExceptiona at 2007-7-12 8:38:44 > top of Java-index,Java Essentials,New To Java...
# 3
We could give a better answer if you explained your problem. What are you trying to do? What is your goal?
DrLaszloJamfa at 2007-7-12 8:38:44 > top of Java-index,Java Essentials,New To Java...
# 4

Thx for all responses,

I'm trying to return a string with different colors in it;

//I make a string

String s = "";

//a condition

if (tdr == 0)

{

s += "Enemy missed!\n";// >>>>This should look green

}

//and at some other point:

if (generate.nextInt(101) > 75)

{

s +=("Your armor is reduced with 1. \n");//>>>This should look red.

}

//and at the end:

return s;

//s is afterwards displayed in a JTextPane

Greetz Daan

D-a-a-na at 2007-7-12 8:38:44 > top of Java-index,Java Essentials,New To Java...
# 5
Rather than require the method to return just a String, you could pass the method the JTextPane or its StyledDocument, and have the method operate on that.
DrLaszloJamfa at 2007-7-12 8:38:44 > top of Java-index,Java Essentials,New To Java...