Answer to Question #271342 in Java | JSP | JSF for Her

Question #271342

Design an application that extends JFrame and that displays a blue smiling face on the screen. Save


the file as Smile.java.

1
Expert's answer
2021-11-26T17:44:12-0500



SOLUTION CODE


package com.company;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


class FacePanel extends JPanel
{
    public void paintComponent(Graphics g)
    {
//smiley face
        final int MID=150;
        final int TOP=50;

        g.setColor (Color.red);
        g.drawOval(100,25,175,175);
        g.setColor(Color.black);
        g.drawOval(135,75,25,25);
        g.setColor(Color.black);
        g.drawOval(200,75,25,25);
        g.drawArc(MID+10,TOP+100,60,20,190,175);



    }
}

class Face extends JFrame implements ActionListener
{

    private static boolean SMILE = false;
    FacePanel mmp = new FacePanel();
    private JButton smile = new JButton ("Smile");
    private JButton sad = new JButton ("Sad");


    public Face()
    {
        JPanel p1 = new JPanel();
        p1.setLayout(new GridLayout(4,2));
        add(mmp);
        JButton smile = new JButton("Smile");
        add(smile);
        smile.addActionListener(this);

// The applet has a "Sad" button
        JButton sad = new JButton("Sad");
        add(sad);
        sad.addActionListener(this);

        setTitle("Smiley Face");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(600, 400);
        setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {

        String cmd = e.getActionCommand();

        if (cmd.equals("Smile")) { // "Smile" was pressed
            SMILE = true;
            setBackground(Color.blue);
            repaint();
        }
        else if (cmd.equals("Sad")) { // "Sad"
            SMILE = false;
            setBackground(Color.lightGray);
            repaint();
        }

        Graphics g = null;
        if (SMILE) {
            g.drawArc(70, 145, 60, 60, 45, 90);
        } else {
            g.drawArc(70, 95, 60, 60, 225, 90);
        }

    }


}

//main class
public class Main
{
    public static void main(String args[])
    {
        Face smiley = new Face();

    }
}




Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS