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

OUTPUT:::::::::::::::::::::::::: JAVA PROGRAMMING Purpose: This purpose of this

ID: 3771138 • Letter: O

Question

OUTPUT::::::::::::::::::::::::::

JAVA PROGRAMMING

Purpose: This purpose of this project is to reinforce the knowledge from Chapter 9 of the textbook. It also gives students a chance to do self-study on JApplet. Problem Description: The students are asked to write JApplet GUl to display "Hear No Evil", "See NO Evil", and "Speak No Evil" faces based on user's choice. The sample run of the program is at the end of this file. Although the textbook doesn't include JApplet, it is not difficulty to self learn how to write a class that extends JApplet. The class ShowFaces has the following class Diagranm ShowFaces -hearNoEvilButton: JButton -seeNoEvilButton: JButton -speakNoEvilButton: JButton -control: int +init(): void +paint(g: Graphics): void +actionPerformed(event: ActionEvent): void +drawFace(g: Graphics, color: Color, x: int, y: int):void +drawHand(g: Graphics, x: int, y: int): void

Explanation / Answer

ShowFaces.java

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JPanel;

public class ShowFaces extends JApplet implements ActionListener{
  
   public static final int FACE_WIDTH = 100;
   public static final int FACE_HEIGHT = 110;
  
   public static final int X_FACE = 50;
   public static final int Y_FACE = 5;
  
   public static final int FACE_SHIFT_X = 100;
   public static final int FACE_SHIFT_Y = 100;
  
   public static final int X_LEFT_EYE = 70;
   public static final int X_RIGHT_EYE = 115;
   public static final int Y_EYE = 30;
  
   public static final int EYE_WIDTH = 20;
   public static final int EYE_HEIGHT = 15;
  
   public static final int X_MOUTH = 78;
   public static final int Y_MOUTH = 70;
  
   public static final int MOUTH_WIDTH = 50;
   public static final int MOUTH_HEIGHT = 25;
  
   public static final int X_LEFT_EAR = 12;
   public static final int Y_EAR = 10;
   public static final int X_RIGHT_EAR = 148;
  
   public static final int EAR_WIDTH = 40;
   public static final int EAR_HEIGHT = 50;
  
   public static final int PALM_WIDTH = 40;
   public static final int PALM_HEIGHT = 20;
  
   public static final int FINGER_WIDTH = 8;
   public static final int FINGER_HEIGHT = 20;
  
   public int X_LEFT_HAND;
   public int Y_LEFT_HAND;
   public int X_RIGHT_HAND;
   public int Y_RIGHT_HAND;
  
  
   private JButton hearNoEvilButton;
   private JButton seeNoEvilButton;
   private JButton speakNoEvilButton;
  
   private int control = 0;
   public void init() {
      
       hearNoEvilButton = new JButton("Hear No Evil");
       seeNoEvilButton = new JButton("See No Evil");
       speakNoEvilButton = new JButton("Speak No Evil");
      
       hearNoEvilButton.addActionListener(this);
       seeNoEvilButton.addActionListener(this);
       speakNoEvilButton.addActionListener(this);
      
       JPanel bottom = new JPanel(); // a Panel to hold the control buttons
       bottom.setLayout(new GridLayout(1, 3, 3, 3));
       bottom.add(hearNoEvilButton);
       bottom.add(seeNoEvilButton);
       bottom.add(speakNoEvilButton);
      
       getContentPane().setLayout(new BorderLayout(3, 3));
       getContentPane().add("South", bottom);
   }

   @Override
   public void actionPerformed(ActionEvent e) {
       if (e.getSource() == hearNoEvilButton) {
           control = 2;
           hearNoEvilButton.disable();
           seeNoEvilButton.enable();
           speakNoEvilButton.enable();
       }
      
       else if (e.getSource() == seeNoEvilButton) {
           control = 1;
           seeNoEvilButton.disable();
           speakNoEvilButton.enable();
           hearNoEvilButton.enable();
       }
      
       else if (e.getSource() == speakNoEvilButton) {
           control = 3;
           speakNoEvilButton.disable();
           seeNoEvilButton.enable();
           hearNoEvilButton.enable();
       }
       repaint();
   }
  
   @Override
   public void paint(Graphics g) {
       g.clearRect(0,0, 500, 500);
       setBackground(Color.white);
       drawFace(g, Color.red, X_FACE, Y_FACE);
       switch (control) {
       case 1:
           X_LEFT_HAND = 65;
           Y_LEFT_HAND = 30;
           X_RIGHT_HAND = 110;
           Y_RIGHT_HAND = 30;
           break;
          
       case 2:
           X_LEFT_HAND = 12;
           Y_LEFT_HAND = 35;
           X_RIGHT_HAND = 148;
           Y_RIGHT_HAND = 35;
           break;
       case 3:
           X_LEFT_HAND = 68;
           Y_LEFT_HAND = 80;
           X_RIGHT_HAND = 108;
           Y_RIGHT_HAND = 80;
           break;
       }
       drawHand(g, X_LEFT_HAND, Y_LEFT_HAND);
       drawHand(g, X_RIGHT_HAND, Y_RIGHT_HAND);
   }
  
   public void drawFace(Graphics g, Color color, int x, int y) {
       g.setColor(color);
       g.drawOval(x, y, FACE_WIDTH, FACE_HEIGHT);
       g.fillOval(X_LEFT_EYE, Y_EYE, EYE_WIDTH, EYE_HEIGHT);
       g.fillOval(X_RIGHT_EYE, Y_EYE, EYE_WIDTH, EYE_HEIGHT);
       g.drawArc(X_MOUTH, Y_MOUTH, MOUTH_WIDTH, MOUTH_HEIGHT, 180, 180); // draw mouth
       g.drawOval(X_LEFT_EAR, Y_EAR, EAR_WIDTH, EAR_HEIGHT);
       g.drawOval(X_RIGHT_EAR, Y_EAR, EAR_WIDTH, EAR_HEIGHT);
   }
  
   public void drawHand(Graphics g, int x, int y) {
       if (control != 0) {
           g.setColor(Color.black);
           g.fillOval(x, y, PALM_WIDTH, PALM_HEIGHT);
           g.fillOval(x+2, y-10, FINGER_WIDTH, FINGER_HEIGHT);
           g.fillOval(x+18, y-12, FINGER_WIDTH, FINGER_HEIGHT);
           g.fillOval(x+34, y-10, FINGER_WIDTH, FINGER_HEIGHT);
       }
      
   }
  
  

}