Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can I change the text color without changing the rectangle color ?

Java
private static class RectDraw extends JPanel
	{
		protected void paintComponent(Graphics g) {
	        super.paintComponent(g);  
	         g.drawRect(20,5,330,25);  
	         g.setColor(Color.GRAY);  
	         g.fillRect(20,5,330,25); 
	         g.drawString("movie Sceen", 50, 10);
	        }


What I have tried:

I have drawn a rectangle with gray color. Now I would like to add a "movie screen" text which is blue color inside the rectangle. How can I achieve this ? Thanks.
Posted
Updated 16-May-16 6:20am
Comments
Richard Deeming 16-May-16 12:14pm    
Just a guess - have you tried calling setColor a second time, between the fillRect and drawString calls?
wseng 16-May-16 12:15pm    
Nope. You mean g.fillRect(20,5,330,25);
g.setColor(Color.BLUE);
g.drawString("movie Sceen", 50, 10); ?
Richard Deeming 16-May-16 12:16pm    
That's what I'd try. :)
wseng 16-May-16 12:17pm    
Thanks sir, it worked :)
Richard Deeming 16-May-16 12:18pm    
OK, I'll post it as an answer. :)

1 solution

As mentioned in the comments, a second call to setColor solves the issue:
Java
g.fillRect(20,5,330,25);
g.setColor(Color.BLUE);
g.drawString("movie Sceen", 50, 10);

Not a bad guess for someone who doesn't "do" Java! :)
 
Share this answer
 
Comments
wseng 16-May-16 12:21pm    
Thanks sir :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900