• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Setting color of a rectangle

 
Mike Enders
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am creating an applet that has two rectangles moving and bouncing of the edges of the applet. I need to have each rectangle be a different color, and I cannot figure out from the Java documentation how the graphic setColor() method works. To illustrate, my code will be something like:
public void paint( Graphics g )
{
g.clearRect( 0,0, 200,200);
g.setColor(WHAT GOES HERE??);
g.drawRect( currX1 , currY1 , imageWidth , imageHeight );
g.setColor(WHAT GOES HERE FOR SECOND COLOR??);
g.drawRect( currX2 , currY2 , imageWidth , imageHeight );
}
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

The parameter is a java.awt.Color object. There are several ways to obtain one; either use a predefined one like "Color.GREEN", or define one by giving the red, green and blue values of the color like "new Color(33, 66, 99)" or "new Color(0.0, 0.5, 1.0)".
 
Mike Enders
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
Talha Bin Hussain
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the Code:

g.setColor(Color.CYAN);
g.fillRoundRect(50,50,100,100,20,20);
g.drawRoundRect(50,50,100,100,20,20);


g.setColor(Color.RED);
g.fillRoundRect(50,50,100,100,20,20);
g.drawRoundRect(50,50,100,100,20,20);
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic