Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Below is my original question. I received an answer for part a, but not part b.

ID: 3771897 • Letter: B

Question

Below is my original question. I received an answer for part a, but not part b.

a.Write an application that extends JFrame and that displays a yellow smiling

face on the screen. Save the file as JSmileFace.java.

b. Add a JButton to the JSmileFace program so the smile changes to a frown

This is the code that I received for part a:

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;        
private DisplayPanel display;     
public SmileyFrame ()
{
    setSize (300, 300);             
    setLocationRelativeTo (null);  
    setTitle ("Smiley Frame");      
    setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    smiley = new SmileyFace();           
    display = new DisplayPanel (smiley);
    setLayout (new BorderLayout());      
    add (display, BorderLayout.CENTER);  
}
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);               
    int xcenter = getWidth() / 2;    
    int ycenter = getHeight() / 2;
       smiley.render (g, xcenter, ycenter);
}
}
class SmileyFace {
private static final int RADIUS = 100;          
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);
    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);
    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);
}
}

when the user clicks the JButton. Save the file as JSmileFace2.java.

Explanation / Answer

public class JSmileFace2 extends JApplet implements ActionListener
{

    JButton frownButton = new JButton("Sad Face");
    boolean happy = true ;
    public void init()
    {
        Container con = getContentPane();
        con.setLayout(new FlowLayout() );
        con.add(frownButton);
        frownButton.addActionListener(this);
        frownButton.requestFocus();
        this.setBackground(Color.black);
    }

public void paint(Graphics gr)
{
    if (happy)
    {
        gr.setColor(Color.yellow);
        gr.fillOval(50,50,300,300);
        gr.setColor(Color.black);
        gr.fillOval(110,140,50,50);
        gr.fillOval(230,140,50,50);
        gr.drawArc(135,185,130,130,180,180);
        frownButton.setBackground(Color.yellow);
        frownButton.requestFocus();
    }
    else
    {
        gr.setColor(Color.yellow);
        gr.fillOval(50,50,300,300);
        gr.setColor(Color.black);
        gr.fillOval(110,140,50,50);
        gr.fillOval(230,140,50,50);
        gr.drawArc(135,220,130,130,180,-180);
        frownButton.setBackground(Color.yellow);
    frownButton.requestFocus();
    }
}

public void actionPerformed(ActionEvent e)
{

    Object source = e.getSource();
    if(source == frownButton)
    {
        if (happy)
        {
            happy = false;
            frownButton.setText("Make Happy");
            repaint();
    }
    else
    {
            happy =true;
            frownButton.setText("Sad Face");
            repaint();
    }
    }
    }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote