Program to draw three concentric circles with three colors pink, red and green
In this program, we have to draw three concentric circles with three colours pink, red and green as shown in output. The setFont( ) method is used to set the font in that you have to pass the Font class's object. You have to pass font name, font style and font size in Font's constructor like:
g.setFont(new Font("Times New Roman", Font.BOLD|Font.ITALIC, 14));
To change the current color use the setColor( ) method of Graphics class as shown below. There are some pre-defined colors in Java like RED, GREEN, WHITE, BLUE, CYAN, BLACK, PINK, etc.
g.setColor(Color.pink);
To use different color you have to pass the RGB values in Color's class constructor as given below.
PROGRAM
OUTPUT
C:\>javac ConcentricCircles2.java
C:\>appletviewer ConcentricCircles2.java
g.setColor(new Color(23,123,101));
PROGRAM
// Draw Concentric Circles.
import java.awt.*;
import java.applet.*;
public class ConcentricCircles2 extends Applet
{
String str = "Concentric Circles";
public void paint(Graphics g)
{
g.setColor(Color.pink);
g.drawOval(20,20,45,45);
g.setColor(Color.red);
g.drawOval(10,10,65,65);
g.setColor(Color.green);
g.drawOval(30,30,25,25);
g.setColor(Color.black);
g.setFont(new Font("Times New Roman",Font.BOLD|Font.ITALIC,20));
g.drawString(str,100,20);
}
}
/* <applet code=ConcentricCircles2 width=300 height=300>
</applet>
*/
OUTPUT
C:\>javac ConcentricCircles2.java
C:\>appletviewer ConcentricCircles2.java