I\'m truly grateful if somebody can help me with this Project :D You will make a
ID: 3683752 • Letter: I
Question
I'm truly grateful if somebody can help me with this Project :D
You will make a class called Smiley.
•Smiley will extend JPanel
•Smiley must have a constructor that will define initial state of the smiley face
•Smiley must have at least 4 methods that change the state of the smiley
•E.g. you could write a method public void wink() that will change one of the eyes to an arc or line to make it look like the face is winking or you could have a method called public void frown() that will change the smile to a frown.
•methods can change any attribute of the smiley face that you choose. Get creative!
•You will use instance data to describe all attributes of the face. These attributes may include but are not limited to the following:
•faceColor
•eyeColor
•isSmiling
•headSize
•eyeSize
•leftEyeX and leftEyeY, rightEyeX and RightEyeY
•hasBeard?
•anything that can describe the face can be turned into instance data
•You must create an applet called SmileyApplet that will use 3 instances of Smiley
•You must use your Swing and GUI skills to create controls to adjust the attributes of each face.
•You mayuse JRadioButtons or JComboBox to choose which face you and controlling
•Your controls must allow for at least 4 different attribute changes. A user should be able to make each smiley look different.
Explanation / Answer
Program Code
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class ActionFace extends JFrame
{
public static final int WINDOW_WIDTH = 400;
public static final int WINDOW_HEIGHT = 400;
public static final int FACE_DIAMETER = 200;
public static final int X_FACE = 100;
public static final int Y_FACE = 100;
public static final int EYE_WIDTH = 20;
public static final int EYE_HEIGHT = 10;
public static final int X_RIGHT_EYE = X_FACE + 55;
public static final int Y_RIGHT_EYE = Y_FACE + 55;
public static final int X_LEFT_EYE = X_FACE + 130;
public static final int Y_LEFT_EYE = Y_FACE + 60;
public static final int MOUTH_WIDTH = 100;
public static final int MOUTH_HEIGHT = 50;
public static final int X_MOUTH = X_FACE + 50;
public static final int Y_MOUTH = Y_FACE + 100;
public static final int MOUTH_START_ANGLE = 180;
public static final int MOUTH_ARC_SWEEP = 180;
private static final int Y_NOSE = 195;
private static final int X_NOSE = 195;
private static final int NOSE_DIAMETER = 10;
private Color eyeColor = Color.BLUE;
private boolean wink;
private boolean frown;
private boolean smile;
private class WinkAction implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
wink = true;
repaint();
}
}
private class FrownAction implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
frown = true;
repaint();
}
}
private class SmileAction implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
smile = true;
repaint();
}
}
public static void main(String[] args)
{
ActionFace drawing = new ActionFace();
drawing.setVisible(true);
}
public ActionFace()
{
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Hello there!");
setLayout(new BorderLayout());
getContentPane().setBackground(Color.WHITE);
JButton winkButton = new JButton("Click for a wink");
JButton frownButton = new JButton("Click for a frown");
JButton smileButton = new JButton("Click for a smile");
winkButton.addActionListener(new WinkAction());
frownButton.addActionListener(new FrownAction());
smileButton.addActionListener(new SmileAction());
add(winkButton, BorderLayout.PAGE_START);
add(frownButton, BorderLayout.PAGE_END);
add(smileButton, BorderLayout.LINE_START);
wink = true;
frown = true;
smile = true;
}
public void paint(Graphics g)
{
super.paint(g);
g.drawOval(X_FACE, Y_FACE, FACE_DIAMETER, FACE_DIAMETER);
drawEyes(g);
drawNose(g);
drawFrown(g);
drawSmile(g);
}
private void drawEyes(Graphics g)
{
g.setColor(eyeColor);
g.fillOval(X_RIGHT_EYE, Y_RIGHT_EYE, EYE_WIDTH, EYE_HEIGHT);
if(wink)
{
g.drawLine(X_LEFT_EYE, Y_LEFT_EYE, X_LEFT_EYE + EYE_WIDTH, Y_LEFT_EYE);
}
else
{
g.fillOval(X_LEFT_EYE, Y_LEFT_EYE, EYE_WIDTH, EYE_HEIGHT);
}
}
private void drawNose(Graphics g)
{
g.setColor(Color.BLACK);
g.fillOval(X_NOSE, Y_NOSE, NOSE_DIAMETER, NOSE_DIAMETER);
}
private void drawFrown(Graphics g)
{
g.setColor(Color.RED);
if(frown)
{
g.drawArc(150,230,100,50,360,180);
}
else
{
g.drawArc(X_MOUTH, Y_MOUTH, MOUTH_WIDTH, MOUTH_HEIGHT,
MOUTH_START_ANGLE, MOUTH_ARC_SWEEP);
}
}
private void drawSmile(Graphics g)
{
g.setColor(Color.RED);
if(smile)
{
g.drawArc(X_MOUTH, Y_MOUTH, MOUTH_WIDTH, MOUTH_HEIGHT,
MOUTH_START_ANGLE, MOUTH_ARC_SWEEP);
}
}
}