import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
public class JSmileyFace2 extends JFrame implements ActionListener
{
private JButton mouth = new JButton("Smile");
public JSmileyFace2()
{
super("Smile");
setVisible(true);
setSize(300,320);
setBackground(Color.WHITE);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(mouth);
mouth.addActionListener(this);
}
public void paint(Graphics gr)
{
super.paint(gr);
Graphics2D gr2d = (Graphics2D)gr;
BasicStroke brush = new BasicStroke(5.0f, BasicStroke.CAP_ROUND,
BasicStroke.JOIN_ROUND);
gr2d.setStroke(brush);
//Draws outer smiley
gr2d.setPaint(Color.YELLOW);
gr2d.fill(new Ellipse2D.Float(30F, 80F, 200F, 200F));
gr2d.setPaint(Color.BLACK);
gr2d.draw(new Ellipse2D.Float(30F, 80F, 200F, 200F));
//draws eyes
gr2d.fill(new Ellipse2D.Float(90F, 110F, 20F, 60F));
gr2d.fill(new Ellipse2D.Float(150F, 110F, 20F, 60F));
//draws mouth based on button text
if(mouth.getText().equals("Smile")) //draws smile
gr2d.draw(new Arc2D.Float(70F, 140F, 120F, 120F, 180F, 180F, Arc2D.OPEN));
else //draws frown
gr2d.draw(new Arc2D.Float(70F, 180F, 120F, 120F, 180F, -180F, Arc2D.OPEN));
mouth.setLocation(20,20);
}
public void actionPerformed(ActionEvent e)
{
if(mouth.getText().equals("Smile"))
mouth.setText("Frown");
else
mouth.setText("Smile");
repaint();
}
public static void main(String[] args)
{
JSmileyFace2 frame = new JSmileyFace2();
}
}