Hello,

Does anyone know how to set the text colour but for a specific character?

E.g. If I enter "Phillip" I want it to display:

PHillip

So that just 1 character is coloured?

Heres the code I've written:

public void paint(Graphics g)
    {
      String elements;
  	  
  	  elements = nameField.getText();
  	  
  	  char element[] = elements.toCharArray();
     
  	  for(int i=0; (i < element.length); i++)
  	  {
  		  if(element[i] == 'b' || element[i] == 'B')
  		  {
  			  g.setColor(Color.red);
  			  g.drawChars(element, 0, element.length, getWidth()/2, getHeight()/2);
  			
  			    
  		  }else if (element[i] == 's' || element[i] == 'S')
  		  {
  			  g.setColor(Color.yellow);
  		  }
  		  
  		 // g.drawChars(element, 0, element.length, getWidth()/2, getHeight()/2);
  	  }
  	  
   }

Help please? =)

You need to set the color and draw each letter individually as you loop through them. You'll have to increment the position on the screen as you draw as well.

Note that you can specify the offset and length to draw in the method call, so your loop variable can be used like so to draw a single character

g.drawChars(element, i, 1, getWidth()/2, getHeight()/2);

You'll need to shift your x value incrementally as you go.

I don't get you?

public void paint(Graphics g)
    {
      String elements;
  	  
  	  elements = nameField.getText();
  	  
  	  char element[] = elements.toCharArray();
     
  	  for(int i=0; (i < element.length); i++)
  	  {
  		  
  		  
  		  switch(element[i])
		  {
		  	case 'A': g.setColor(Color.black);	
		  			  g.drawChars(element, i, 1, getWidth()/2, getHeight()/2);
		    break;
		  		
		  	case 'B': case 'b': g.setColor(Color.blue);
		  			  g.drawChars(element, i, 1, getWidth()/2+10, getHeight()/2);
		  	break;
		  	
		  	case 'C': g.setColor(Color.blue);
		  			  g.drawChars(element, i, 1, getWidth()/2+20, getHeight()/2);
			break;
			
		  	case 'D': g.setColor(Color.blue);
			  		  g.drawChars(element, i, 1, getWidth()/2+30, getHeight()/2);
			  		  break;
		  	
		  	case 'E': g.setColor(Color.blue);
			  		  g.drawChars(element, i, 1, getWidth()/2+40, getHeight()/2);
			  		  break;
		  
		  	case 'F': g.setColor(Color.blue);
			  		  g.drawChars(element, i, 1, getWidth()/2+50, getHeight()/2);
			  		  break;
		  		
		  	case 'G': g.setColor(Color.blue);
			  		  g.drawChars(element, i, 1, getWidth()/+60, getHeight()/2);
			  		  break;
		  
		  	case 'H': g.setColor(Color.blue);
			  		  g.drawChars(element, i, 1, getWidth()/+70, getHeight()/2);
			  		  break;
		  
		  	case 'I': g.setColor(Color.blue);
		  			  g.drawChars(element, i, 1, getWidth()/+80, getHeight()/2);
			  		  break;
		  
		  	case 'J': g.setColor(Color.blue);
			  		  g.drawChars(element, i, 1, getWidth()/+90, getHeight()/2);
			  		  break;
		  	
		  	case 'K': g.setColor(Color.blue);
			  		  g.drawChars(element, i, 1, getWidth()/+100, getHeight()/2);
			  		  break;
		  	
		  	case 'L': g.setColor(Color.blue);
			  		  g.drawChars(element, i, 1, getWidth()/+110, getHeight()/2);
			  		  break;
		  
		  	case 'M': g.setColor(Color.blue);
			  		  g.drawChars(element, i, 1, getWidth()/+120, getHeight()/2);
			  		  break;
		  
		  	case 'N': g.setColor(Color.blue);
			  		  g.drawChars(element, i, 1, getWidth()/+130, getHeight()/2);
			  		  break;
		  	case 'O': g.setColor(Color.blue);
			  		  g.drawChars(element, i, 1, getWidth()/+140, getHeight()/2);
			  		  break;
		  
		  	case 'P': g.setColor(Color.blue);
			  		  g.drawChars(element, i, 1, getWidth()/+150, getHeight()/2);
			  		  break;
			  		  
		  	case 'Q': g.setColor(Color.blue);
		  			  g.drawChars(element, i, 1, getWidth()/+160, getHeight()/2);
		  			  break;
		  
		  	case 'R': g.setColor(Color.blue);
			  		  g.drawChars(element, i, 1, getWidth()/+170, getHeight()/2);
			  		  break;
	
		  	case 'S': g.setColor(Color.blue);
			  		  g.drawChars(element, i, 1, getWidth()/+180, getHeight()/2);
			  		  break;
	
		  	case 'T': g.setColor(Color.blue);
			  		  g.drawChars(element, i, 1, getWidth()/+190, getHeight()/2);
			  		  break;
	
		  	case 'U': g.setColor(Color.blue);
			  		  g.drawChars(element, i, 1, getWidth()/+200, getHeight()/2);
			  		  break;
	
		  	case 'V': g.setColor(Color.blue);
			  		  g.drawChars(element, i, 1, getWidth()/+210, getHeight()/2);
			  		  break;
	
		  	case 'W': g.setColor(Color.blue);
		  			  g.drawChars(element, i, 1, getWidth()/+220, getHeight()/2);
		  			  break;
	
		  	case 'X': g.setColor(Color.blue);
			  		  g.drawChars(element, i, 1, getWidth()/+230, getHeight()/2);
			  		  break;
	
		  	case 'Y': g.setColor(Color.blue);
			  		  g.drawChars(element, i, 1, getWidth()/+240, getHeight()/2);
			  		  break;
	
		  	case 'Z': g.setColor(Color.blue);
			  		  g.drawChars(element, i, 1, getWidth()/+250, getHeight()/2);
			  		  break;
		  }
  		  
  		  
		  
  			
  	  }
  	  
   }

Something like that?

Do you want each char for different color??? Also, you need to understand what drawChars() method arguments are... You only set color when you found a character which is supposed to be different color; otherwise, you set the color to default color. The drawChars() will be the same for all but need to specify correct location for its drawing.

Can you help me with this please? It doesn't work..

public void paint(Graphics g)
    {
      String elements;
  	  
  	  elements = nameField.getText();
  	  
  	  char element[] = elements.toCharArray();
     
  	  for(int i=0; (i < element.length); i++)
  	  {
  		  if(element[i] == 'B' || element[i] == 'b')
  		  {
  			  g.setColor(Color.BLUE);
  			  
  		  }else if(element[i].green);
  		  }else{
  			 g.setColor(Color.black);
  		  }
  		
  		  g.drawChars(element, 0, element.length, getWidth()/3, getHeight()/2);	
  	  }
  	  
   }

It's just if them two characters are entered..

Thanks =)

...From line 12 to 19, you just need to check whether you want to set the color to whatever color you want... You are almost correct there. From your code, I modified a bit (try to keep the way it is).

if(element[i] == 'B' || element[i] == 'b') {
    g.setColor(Color.BLUE);
  }
  else if(element[i]=='H' || element[i] == 'h') { // or whatever it is
    g.setColor(Color.GREEN);
  }
  else {
    g.setColor(Color.BLACK);
  }

  // read the API doc for Graphics class...
  // 1st argument is the char array
  // 2nd argument is the offset (which index to start in the char array)
  // third argument is how many chars you want to draw from the start
  // forth & fifth is the coordinate x,y of the drawing location
  g.drawChars(element, i, 1, (getWidth()/2)+(i*10), getHeight()/2);

Hopefully, it works. I have no way to test it here...

Hey,

Thank you, it just shows like one character and not the full string..

For example, I want it to.. if I entered Phillip, it shows every character that stars in P (red)

Thanks =)

What is your paint() method definition now? Also, are you sure that the elements string contains what you entered?

commented: You're the best :) +3
import java.awt.*; 
import java.applet.*; 
// import an extra class for the ActionListener 
import java.awt.event.*;
import java.awt.Graphics;


public class main extends Applet implements ActionListener 
{

     Button okButton; 
     TextField nameField; 
     Label label1; 

     public void init()  
     { 
    	 // Now we will use the FlowLayout 
    	  label1 = new Label ("Please enter a string", Label.LEFT);
          setLayout(new FlowLayout()); 
          okButton = new Button("Submit string"); 
          nameField = new TextField("",10); 
          add(label1);
          add(nameField); 
          add(okButton); 

          okButton.addActionListener(this); 
  
     }

    public void paint(Graphics g)
    {
      String elements;
  	  
  	  elements = nameField.getText();
  	  
  	  char element[] = elements.toCharArray();
      System.out.println(elements);
  	  for(int i=0; (i < element.length); i++)
  	  {
  		 if(element[i] == 'B' || element[i] == 'b') {
  		    g.setColor(Color.BLUE);
  		  }
  		  else if(element[i]=='H' || element[i] == 'h') { // or whatever it is
  		    g.setColor(Color.GREEN);
  		  }
  		  else {
  		    g.setColor(Color.BLACK);
  		  }

  		  g.drawChars(element, 0, element.length, (getWidth()/2)+(i*10), getHeight()/2);
  	  }
  	  
   }
   public void actionPerformed(ActionEvent evt)  
   { 
      if (evt.getSource() == okButton)  
      {  
          repaint();
      }
   }
}

Also, the string is the same..

Thanks btw :)

In your line 50, the 2nd argument should be 'i' instead of '0'. Also, the same line... 3rd argument must be 1 not the element's length.

Hey, I've tried that.. Just gives me this error:

"Exception in thread "AWT-EventQueue-1" java.lang.ArrayIndexOutOfBoundsException: bad offset/length"

mmm :(

Read my edited post please...

g.drawChars(element, 0, 1, (getWidth()/2), getHeight()/2);

But that only prints out 1 character, I need the full string

... You need a new pair of glasses...

// MINE

  // read the API doc for Graphics class...
  // 1st argument is the char array
  // 2nd argument is the offset (which index to start in the char array)
  // third argument is how many chars you want to draw from the start
  // forth & fifth is the coordinate x,y of the drawing location
  g.drawChars(element, i, 1, (getWidth()/2)+(i*10), getHeight()/2);

// YOURS
  g.drawChars(element, 0, 1, (getWidth()/2), getHeight()/2);

Compare it yourself...

It's been one of them days ;)

Thank you so, so, so much!!

fof Swing JComponents you never, never use paint, but paintComponent

and why don't use:

1/ Caret

2/ Html

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.