import java.awt.*;
import java.applet.Applet;
// Figures displays circles and squares and calculates
their areas.
public class Figures extends Applet
{
private Circle redCircle, blueCircle;
private Square greenSquare, orangeSquare;
public void init ()
{
redCircle
= new Circle (Color.red, 100, 100);
blueCircle
= new Circle (Color.blue, 200, 100);
greenSquare
= new Square (Color.green, 100, 200);
orangeSquare
= new Square (Color.orange, 200, 200);
} // method init
public void paint (Graphics g)
{
redCircle.drawCircle
(g);
blueCircle.drawCircle
(g);
blueCircle.drawAreaString
(g);
greenSquare.drawSquare
(g);
orangeSquare.drawSquare
(g);
orangeSquare.drawAreaString
(g);
} // method paint
} // class Figures
class Circle
{
private Color color;
private int x, y, diameter;
Circle (Color c, int xPosition, int
yPosition)
{
color = c;
x = xPosition;
y = yPosition;
diameter =
50;
} // constructor
public void drawCircle (Graphics g)
{
g.setColor
(color);
g.fillOval
(x, y, diameter, diameter);
} // method drawCircle
private int calculateArea ()
{
int area,
radius = diameter / 2;
area = (int)(Math.PI
* radius * radius);
return area;
} // method calculateArea
public void drawAreaString (Graphics
g)
{
g.setColor
(Color.black);
int area =
calculateArea ();
g.drawString
("Area of Circle: "+ area, 100, 75);
} // method drawAreaString
} // class Circle
class Square
{
private Color color;
private int x, y, side;
Square (Color c, int xPosition, int
yPosition)
{
color = c;
x = xPosition;
y = yPosition;
side = 50;
} // constructor
public void drawSquare (Graphics g)
{
g.setColor
(color);
g.fillRect
(x, y, side, side);
} // method drawSquare
private int calculateArea ()
{
int area;
area = side
* side;
return area;
} // method calculateArea
public void drawAreaString (Graphics
g)
{
g.setColor
(Color.black);
int area =
calculateArea ();
g.drawString
("Area of Square: "+area, 100, 275);
} // method drawAreaString
}// class Square