
/**
 *
 * a simple applet
 *
 * Written by: Roger Garside
 *
 * First Written: 1/Feb/97
 * Last Rewritten: 30/May/97
 *
 */

import java.awt.* ;
import java.awt.event.* ;
import java.applet.* ;

public class Chapter20n1 extends Applet implements ActionListener
    {
    Canvas1 canvas ;
    Button change ;
    String welcomeName ;
    boolean isSmiling ;

    /**
     * init
     */

    public void init()
	{
	setLayout(new BorderLayout()) ;
	welcomeName = "World" ;
	isSmiling = true ;

        setBackground(Color.green) ;

	// set up area for smiley face
	canvas = new Canvas1(this) ;
	add("Center", canvas) ;

	// set up panel for button
	Panel p = new Panel() ;
	p.setLayout(new FlowLayout()) ;
	change = new Button("Change") ;
	p.add(change) ;
        change.addActionListener(this) ;
	add("South", p) ;
	} // end of method init

    /**
     * actionPerformed
     */

    public void actionPerformed(ActionEvent event)
        {
	// deal with "Change" button
	isSmiling = !isSmiling ;
	canvas.repaint() ;
        } // end of method actionPerformed
    } // end of class Chapter20n1

class Canvas1 extends Canvas
    {
    Chapter20n1 parent ;

    /**
     * constructor
     */

    public Canvas1(Chapter20n1 f)
        {
        parent = f ;
        } // end of constructor method

    /**
     * paint
     */

    public void paint(Graphics g)
        {
	// set up some dimensions for the drawing
        Dimension d = getSize() ;
        int cx = d.width / 2,
	    cy = d.height /2,
	    faceRadius = 50,
	    noseLength = 20,
	    mouthRadius = 30,
	    mouthAngle = 50,
	    eyeRadius = 5 ;

	// draw the frame
        g.setColor(Color.black) ;
        g.drawRoundRect(2, 2, d.width - 5, d.height - 5, 20, 20) ;

	// draw the face
        g.setColor(Color.red) ;
        g.drawOval(cx - faceRadius,
		   cy - faceRadius,
		   faceRadius * 2,
		   faceRadius * 2) ;
        g.setColor(Color.blue) ;
        g.fillOval(cx - 30 - eyeRadius,
		   cy - 20,
		   eyeRadius * 2,
		   eyeRadius * 2) ;
        g.fillOval(cx + 30 - eyeRadius,
		   cy - 20,
		   eyeRadius * 2,
		   eyeRadius * 2) ;
        g.setColor(Color.red) ;
        g.drawLine(cx, cy - (noseLength / 2), cx, cy + (noseLength / 2)) ;
        if (parent.isSmiling)
            g.drawArc(cx - mouthRadius,
		      cy - mouthRadius,
		      mouthRadius * 2,
		      mouthRadius * 2,
		      270 - mouthAngle,
		      mouthAngle * 2) ;
        else
            g.drawArc(cx - mouthRadius,
		      cy - mouthRadius + 50,
		      mouthRadius * 2,
		      mouthRadius * 2,
		      90 - mouthAngle,
		      mouthAngle * 2) ;

	// write the text
        Font f1 = new Font("TimesRoman", Font.PLAIN, 14) ;
        Font f2 = new Font("TimesRoman", Font.ITALIC, 14) ;
        FontMetrics fm1 = g.getFontMetrics(f1) ;
        FontMetrics fm2 = g.getFontMetrics(f2) ;
        String s1 = "Hello, " ;
        String s2 = parent.welcomeName ;
        int w1 = fm1.stringWidth(s1) ;
        int w2 = fm2.stringWidth(s2) ;
        g.setColor(Color.black) ;
        g.setFont(f1) ;
        int ctx = cx - ((w1 + w2) / 2) ;
        int cty = cy + faceRadius + 30 ;
        g.drawString(s1, ctx, cty) ;
        ctx += w1 ;
        g.setFont(f2) ;
        g.drawString(s2, ctx, cty) ;
        } // end of method paint
    } // end of class Canvas1

