• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Eclipse - changing color output elements  RSS feed

 
Lennart van Ham
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

Can someone tell me how to change the color of an output element in Eclipse? Can't seem to find it anywhere on the internet.
For example, a response to my question 'What color do you want?' could be 'red'. I added an if-statement, which states if input = red, start using a red colored 'X' as string for further output.
How do I obtain the color red for my X??

Or isn't it possible changing the color of output elements?

Many thanks.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lennart van Ham wrote:Can someone tell me how to change the color of an output element in Eclipse? Can't seem to find it anywhere on the internet.
For example, a response to my question 'What color do you want?' could be 'red'. I added an if-statement, which states if input = red, start using a red colored 'X' as string for further output.

I'm afraid we'll need a lot more specifics than that. What do you mean by an "ouput element"? And I presume you want a solution that will work anywhere, not just in Eclipse ... or is this something specific to Eclipse?

How do I obtain the color red for my X??

Well, Java has a Color class (java.awt.Color (←click)), and that class has a constant called Color.RED.

Or isn't it possible changing the color of output elements?

See above...

Winston
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lennart van Ham wrote:I added an if-statement, which states if input = red,

I'm being a little pedantic here but that's an assignment not an equality check. also if 'red' is a string you almost certainly want to use input.equalsIgnoreCase("red") and not input == "red".
 
Lennart van Ham
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I need to apologize here for my way of questioning :p. Thanks for your replies, but it was more meant as a simple description about the story around my actual question. So also with input = red I didn't mean I actually have that in my program..

In fact, my only question was how to obtain a red (or other color) X.

Winston Gutkowski wrote:Well, Java has a Color class (java.awt.Color (←click)), and that class has a constant called Color.RED.


So I will try it with this. Can you tell me how to use that constant with my X? My thoughts right now are like this, where colorChip is a string with the name of some color:



But for example I don't know what to use between symbolx and (Color.RED). (It's just a beginning, but actually don't know if it's any good at all :$)

Winston Gutkowski wrote:And I presume you want a solution that will work anywhere, not just in Eclipse ... or is this something specific to Eclipse?


But no, it actually is specifically for eclipse.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lennart van Ham wrote:In fact, my only question was how to obtain a red (or other color) X.

Color myColor = Color.RED;

Lennart van Ham wrote:
Winston Gutkowski wrote:Well, Java has a Color class (java.awt.Color (←click)), and that class has a constant called Color.RED.


So I will try it with this. Can you tell me how to use that constant with my X? My thoughts right now are like this, where colorChip is a string with the name of some color:



You can't do that. A String knows about characters not colours. If you want to output a String in a particular colour you have to tell the output mechanism what colour you want to use. For example If it's a Swing component you will have to call setForeground() passing in the colour you want to use. If you are drawing directly onto the screen the Graphics object has a setColor() method which again you pass in the colour you want to use.

Lennart van Ham wrote:
But no, it actually is specifically for eclipse.
In what way is it specifically for eclipse. Eclipse is just an IDE.
 
Lennart van Ham
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:
Lennart van Ham wrote:
Winston Gutkowski wrote:Well, Java has a Color class (java.awt.Color (←click)), and that class has a constant called Color.RED.


So I will try it with this. Can you tell me how to use that constant with my X? My thoughts right now are like this, where colorChip is a string with the name of some color:



You can't do that. A String knows about characters not colours. If you want to output a String in a particular colour you have to tell the output mechanism what colour you want to use. For example If it's a Swing component you will have to call setForeground() passing in the colour you want to use. If you are drawing directly onto the screen the Graphics object has a setColor() method which again you pass in the colour you want to use.


I think I'm starting to get it, but could you be a little more specific? How do I tell the output mechanism what color I want to use and how do I implement it on my String?

I tried too with setForeground(), but to me it seemed that would only work if I was working a JFrame of some kind.

Tony Docherty wrote:
In what way is it specifically for eclipse. Eclipse is just an IDE.

Well I meant it only needs to work in Eclipse because it is the only IDE I need to use for this program.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lennart van Ham wrote:
I think I'm starting to get it, but could you be a little more specific? How do I tell the output mechanism what color I want to use and how do I implement it on my String?

I'm not sure how I can be more specific unless you tell us what exactly you want to do ie how you are trying to display the string.
And as I said before, you can't 'implement' it on your String because strings have no concept of colour or font or font size for that matter,

I tried too with setForeground(), but to me it seemed that would only work if I was working a JFrame of some kind.

I did say that would only work if you were using a Swing component to display the string, how are you trying to display the String?

Lennart van Ham wrote:
Tony Docherty wrote:
In what way is it specifically for eclipse. Eclipse is just an IDE.

Well I meant it only needs to work in Eclipse because it is the only IDE I need to use for this program.

I think you are getting confused about what an IDE is. It's just a tool to make writing, compiling and testing programs easier. Eclipse like many IDE's is not limited to Java it can be used to write many different programming languages. What you are doing is writing a Java application, how you run it is irrelevant. It will make no difference whether you run it from Eclipse, netbeans, the command line or any other way the same code will run and the output will be the same. The question Winston was asking was were you trying to set a colour in one of Eclipse's windows/views etc or a colour that your Java code will output ie is this an Eclipse or a Java question (we can now see it is a Java question).
 
Lennart van Ham
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:I'm not sure how I can be more specific unless you tell us what exactly you want to do ie how you are trying to display the string.


All right, I will try to explain without sending the whole 400 lines of my current code. What I'm supposed to do (exercise for school) is create the software version of the classic game Connect Four. Before the game starts, two players should have the opportunity to choose a color (by entering a string representing their color), enter their name, set the panel size and specify the number of chips that a player should have in a row in order to win a game. Well, the part I'm having difficulties with is letting them choose a color. What I have now, regarding that part, is as follows:



And in addition the following 'chip' class:



As you'll see, this will result in taking the first letter of the word (color) that the program will receive and placing it in a preferred column (using another part of the code). However, what I'm trying to do is, instead of the first letter of some word (or could even be a number), placing colored X's (player 1) and O's (player 2) in the columns, where the colors are determined by the answer to '...desired color of your chips: '. I think this explains what I meant with my if-statement and so on. So in other words, where I get stuck is creating these X's and O's in the first place.

I did say that would only work if you were using a Swing component to display the string, how are you trying to display the String?

Hope that's clear now .
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not so easy to print in different colors on the console (with System.out.println(...)). As far as I know, Java does not have any built-in support to print text in different colors on the console.

There are some libraries which can do this, I found for example Java Curses and AnsiColoring. I haven't used these myself, so I can't say anything about how useful these libraries are.
 
Lennart van Ham
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah allright.. Afraid I ain't allowed to use that kind of additions . Have to keep it like this then..

But thanks for your time anyway.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!