Complete the paint method in the given codes to complete the “drawing” of the mo
ID: 3765439 • Letter: C
Question
Complete the paint method in the given codes to complete the “drawing” of the mouth.Use the drawArc method under the Graphics class. In the paint method, you only have to worry about drawing themouth.Codes to draw the rest of the face are already there. Insert the missing codes please where it said "insert codes"
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ChangingFace extends JFrame{
//for window
public static final int WINDOW_LENGTH = 400;
public static final int WINDOW_WIDTH = 400;
//for face
public static final int FACE_DIAMETER = 200;
public static final int X_FACE = 100;
public static final int Y_FACE = 100;
//for eyes
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 + 65;
public static final int X_LEFT_EYE = X_FACE + 130;
public static final int Y_LEFT_EYE = Y_FACE + 60;
//for mouth
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;
JButton winkButton;
JButton frownButton;
JPanel panel;
private boolean wink;
private boolean frown;
public ChangingFace()
{
super("Changing Face");
setSize(WINDOW_WIDTH, WINDOW_LENGTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setBackground(Color.white);
panel = new JPanel();
winkButton = new JButton("click to wink");
winkButton.setActionCommand("wink");
frownButton = new JButton("click to frown");
frownButton.setActionCommand("frown");
winkButton.addActionListener(new winkfrownRespond());
frownButton.addActionListener(new winkfrownRespond());
panel.setLayout(new GridLayout(0,2));
panel.add(winkButton);
panel.add(frownButton);
setLayout(new BorderLayout());
add(panel,BorderLayout.SOUTH);
wink = false;
frown=false;
setVisible(true);
}
public void paint(Graphics g)
{
super.paint(g);
g.drawOval(X_FACE, Y_FACE, FACE_DIAMETER, FACE_DIAMETER);
g.fillOval(X_RIGHT_EYE, Y_RIGHT_EYE, EYE_WIDTH, EYE_HEIGHT);
if(wink == false)
{
g.fillOval(X_LEFT_EYE, Y_LEFT_EYE, EYE_WIDTH, EYE_HEIGHT);
if(frown == false)
{
//insert code here
}
else
{
//insert code here
}
}
else
{
if(frown == false)
{
//insert code here
}
else
{
//insert code here
}
g.drawLine(X_LEFT_EYE, Y_LEFT_EYE, X_LEFT_EYE + EYE_WIDTH, Y_LEFT_EYE);
}
}
private class winkfrownRespond implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//insert code here
repaint();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ChangingFace face = new ChangingFace();
}
}
Explanation / Answer
I will provide you much short and crisp program for the same question
import java.awt.*;
import java.applet.*
public class Face extends Applet
{
public void paint(Graphics g)
{
g.drawOval(40,40,120,250); //head
g.drawOval(57,75,30,20); //left eye
g.drawOval(110,75,30,20); //right eye
g.fillOval(68,81,10,10); //pupil(l)
g.fillOval(121,81,10,10); //pupil(r)
g.drawOval(85,100,30,30); //nose
g.fillArc(60,125,80,40,180,180); //mouth
g.drawOval(25,92,15,30); //ear(l)
g.drawOval(160,92,15,30); //ear(r)
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.