import javax.swing.JFrame;
import javax.swing.JPanel;

import java.awt.Graphics;
import java.awt.BorderLayout;
import java.awt.Color;

public class SmileyFrame extends JFrame {

  private SmileyFace smiley;         // An object to render (or "paint")
  private DisplayPanel display;      // Display panel used to render it

  public SmileyFrame () {
    setSize (300, 300);              // Width, height
    setLocationRelativeTo (null);    // This centers frame on screen
    setTitle ("Smiley Frame");       // Displays a frame title

    setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); // You want this...

    smiley = new SmileyFace();            // Create the object
    display = new DisplayPanel (smiley);  // Create the display panel

    setLayout (new BorderLayout());       // Set layout manager
    add (display, BorderLayout.CENTER);   // Add display panel to frame
  }

  public static void main (String [] args) {
    SmileyFrame frame = new SmileyFrame();
    frame.setVisible(true);
  }
}

// -------------------------------------------------------------------

class DisplayPanel extends JPanel {
  private SmileyFace smiley;

  public DisplayPanel (SmileyFace smiley) {
    this.smiley = smiley;
    setBackground (Color.BLACK);
  }

  protected void paintComponent (Graphics g) {
    super.paintComponent (g);                  // DO NOT FORGET!

    // First find the center of the DisplayPanel:

    int xcenter = getWidth() / 2;     
    int ycenter = getHeight() / 2;

    // Now we just tell the SmileyFace to render itself, using
    // the same "graphics context" (g) that the DisplayPanel
    // has been given by the system. We also design the render()
    // method so that it can be told where to do the rendering
    // in the DisplayPanel's coordinate system:
   
    smiley.render (g, xcenter, ycenter);  // Does all the dirty work
  }
}

// -------------------------------------------------------------------

class SmileyFace {
  private static final int RADIUS = 100;           // Size of head...
  private static final int DIAMETER = 2 * RADIUS;

  public void render (Graphics g, int xcenter, int ycenter) {
    g.setColor (Color.YELLOW);
    g.fillOval (xcenter - RADIUS, ycenter - RADIUS, DIAMETER, DIAMETER);

    // The following section draws the eyes...

    g.setColor (Color.BLACK);
    int eyeRadius = RADIUS / 8;
    int eyeDiameter = 2 * eyeRadius;
    int leftEyeXCenter = xcenter - RADIUS / 3;
    int leftEyeYCenter = ycenter - RADIUS / 3;
    int rightEyeXCenter = xcenter + RADIUS / 3;
    int rightEyeYCenter = ycenter - RADIUS / 3;

    g.fillOval (leftEyeXCenter - eyeRadius, leftEyeYCenter,
                eyeDiameter, eyeDiameter);
    g.fillOval (rightEyeXCenter - eyeRadius, rightEyeYCenter,
                eyeDiameter, eyeDiameter);

    // This section draws the mouth...

    int mouthRadius = 5 * RADIUS / 4;
    int mouthDiameter = 2 * mouthRadius;
    int mouthXCenter = xcenter;
    int mouthYCenter = ycenter;

    g.drawArc (mouthXCenter - mouthRadius, mouthYCenter - 2 * RADIUS,
               mouthDiameter, mouthDiameter,
               240, 60);
  }
}
