Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Can I change attributedstring into string?

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Can I change attributedstring into string?

    I have a maze and I want to change color for the path of the maze. I know that you can add color to a String with addAttribute, but then I need to place the String back into the String array in order to printout the maze. How can I change attributeString into String or is there another way to add color to a String.
    import java.awt.*;
    import java.text.*;
    import javax.swing.*;
    import java.awt.font.*;
    public class Maze extends JPanel
    {
       private final String TRIED = "0";
       private final String PATH = "-";
       private final String SPACE = " ";
     
     
       private String[][] grid = 
                      { {" "," ","*","*","*","*","*","*","*"},
    			      {"*"," "," "," "," "," ","*"," ","*"},
    			      {"*"," ","*","*","*","*","*"," ","*"},
    			      {"*"," ","*"," ","*"," "," "," ","*"},
    			      {"*"," ","*"," ","*","*","*"," ","*"},
    			      {"*"," "," "," ","*"," "," "," ","*"},
    			      {"*"," ","*"," ","*"," ","*"," ","*"},
    			      {"*"," "," "," "," "," ","*"," ","*"},
    			      {"*","*","*","*","*","*","*"," "," "}};
     
     
     
     
       public boolean traverse (int row, int column, Graphics g) 
       {
           AttributedString attributedString = new AttributedString(PATH);
           attributedString.addAttribute(TextAttribute.FOREGROUND, Color.red);
     
           boolean done = false;
          if (valid (row, column))
          {
             grid[row][column] = TRIED;  
             if (row == grid.length-1 && column == grid[0].length-1)
                done = true;  
             else
             {
                done = traverse (row+1, column, g);     
                if (!done)
                   done = traverse (row, column+1,  g);  
                if (!done)
                   done = traverse (row-1, column, g);  
                if (!done)
                   done = traverse (row, column-1, g);  
             }
             if (done) 
                grid[row][column] = PATH;
          } 
          return done;
       }
       private boolean valid (int row, int column)
       {
          boolean result = false;
     
          if (row >= 0 && row < grid.length &&
              column >= 0 && column < grid[row].length)
    	  if (grid[row][column] == SPACE)
    	      result = true;
     
          return result;
       }
       public String toString ()
       {
          String result = "\n";
     
          for (int row=0; row < grid.length; row++)
          {
             for (int column=0; column < grid[row].length; column++)
                result += grid[row][column] + "";
     
    	     result += "\n";
          }
     
          return result;
       }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Can I change attributedstring into string?

    add color to a String
    A String object does not have a color attribute.

Similar Threads

  1. Replies: 7
    Last Post: December 11th, 2011, 11:58 PM
  2. How to change a String value into a number and then back into a String.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 18th, 2011, 01:43 PM
  3. [SOLVED] difference between String Concatenation and String -Buffer/Builder .append(<value>)
    By chronoz13 in forum Java Theory & Questions
    Replies: 5
    Last Post: September 3rd, 2011, 08:16 AM
  4. Replies: 18
    Last Post: March 2nd, 2011, 10:52 AM
  5. [SOLVED] String Matcher finding only char not a whole string
    By Kakashi in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 18th, 2011, 09:58 AM