Results 1 to 8 of 8
  1. #1
    veen is offline Member
    Join Date
    Jun 2010
    Posts
    6
    Rep Power
    0

    Default How to print words in different colours?

    is it really possible?
    like everyone knows, System.err.println("prints in red");
    it prints in red
    but
    now theres one issue

    if i use this


    public class Testing {
    public static void main(String[] args) {

    System.out.println("Standard output Stream message");
    System.err.println("Standard error stream message");
    System.out.println("Standard output Stream message");

    }
    }
    my output becomes
    Standard output Stream message
    Standard output Stream message
    Standard error stream message


    the err message goes all the way down...

    so i googled about printing words in different colours and came across this import java.awt.Color;

    but i don't know how to use it.

    how can i print out words in different colours?

    i'm using eclipse

  2. #2
    veen is offline Member
    Join Date
    Jun 2010
    Posts
    6
    Rep Power
    0

    Default

    sometimes, when i re run the code, the err.println line prints sometimes in these order:

    Standard error stream message
    Standard output Stream message
    Standard output Stream message



    Standard error stream messageStandard output Stream message

    Standard output Stream message



    Standard output Stream message
    Standard error stream message
    Standard output Stream message


    it keeps jumping around! confused!!!

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by veen View Post
    it keeps jumping around! confused!!!
    Stdout (System.out) is line bufferd and normally stderr (System.err) isn't buffered at all but it can be different per system. Don't count on any order w.r.t stderr and stdout.

    kind regards,

    Jos

  4. #4
    veen is offline Member
    Join Date
    Jun 2010
    Posts
    6
    Rep Power
    0

    Default

    is it possible to manually print it out in a different colour?

    using that import java.awt.Color; ?

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    The import statement is an extension of the classpath used by the compiler to find a class definition. It does not generate any code. If you use a class that is in a package, you need to tell the compiler where it is. One way is to use the import statement. The other is to explicitly code the package path:
    java.awt.Color aColor = java.awt.Color(Color.blue);

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by veen View Post
    is it possible to manually print it out in a different colour?

    using that import java.awt.Color; ?
    If you want to print in color on your terminal/console you don't need that awt.Color class. Each and every type of terminal has its own capabilities; some can't even print in differnt colors; if they can each and every terminal type has its own way to actually do that. Unix solved this issue by its termcap file or later its terminal database. Java can do it by using jcurses which is an ncurses clone.

    Better use a Swing component for that purpose if possible.

    kind regards,

    Jos

  7. #7
    veen is offline Member
    Join Date
    Jun 2010
    Posts
    6
    Rep Power
    0

    Default

    Java Code:
    import java.awt.Color;
    
    public class FinalTry {
    	public static void main(String[] args) {
    
    		System.out.println("Standard output Stream message");
    		System.err.println("Standard error stream message");
    		System.out.println("Standard output Stream message");
    		java.awt.Color aColor = Color.blue;
    	}
    }
    how do i print out in that colour?
    PS: i'm trying to learn some "advance" stuff here...
    i'm just few weeks in java...

    Better use a Swing component for that purpose if possible.
    can u show me an example on how to do it?

    please?

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by veen View Post
    can u show me an example on how to do it?

    please?
    Using Swing components there are numerous ways to print text in a certain color; most of the components have a method that sets the foreground color. All text contained in the component will be printed in that particular color. Read some Swing tutorials and you'll see.

    kind regards,

    Jos

Similar Threads

  1. Replies: 0
    Last Post: 01-27-2010, 03:52 PM
  2. Help: Num to words
    By MyOnlineChurva in forum New To Java
    Replies: 16
    Last Post: 01-08-2010, 01:41 AM
  3. PDFBOX change text colours and remove background
    By gstkein in forum Advanced Java
    Replies: 1
    Last Post: 11-20-2009, 11:54 AM
  4. Print the text file and print preview them
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-22-2008, 11:04 PM
  5. help w words
    By Gilgamesh in forum New To Java
    Replies: 5
    Last Post: 11-21-2007, 06:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •