SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Swing Ii
Exercise:
Programming Projects
Question:3 | ISBN:9780132830317 | Edition: 5

Question

Write a GUI program that uses the methods in the Graphics class to draw a smiley face when the window is activated, and a frowny face when the window is deactivated. This will require use of a WindowListener to detect the activation or deactivation events.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

// import the packages
import java.awt.Graphics;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JFrame;
@SuppressWarnings("serial")
 //class definition
public class SmileFace extends JFrame {
 //initialize the face co-ordinates
 private final int FACEDIAMETER = 200;
 private final int XFACE = 100;
 private final int YFACE = 100;

 //initialize the eye co-ordinates
 private final int EYEWIDTH = 20;
 private final int EYEHEIGHT = 10;
 private final int X_RIGHTEYE = XFACE + 35;
 private final int Y_RIGHTEYE = YFACE + 60;
 private final int X_LEFTEYE = XFACE + 130;
 private final int Y_LEFTEYE = XFACE + 60;

 //initialize the mouth co-ordinates
 private final int MOUTHWIDTH = 100;
 private final int MOUTHHEIGHT = 50;
 private final int XMOUTH = XFACE + 50;
 private final int YMOUTH = YFACE + 100;
 private final int MOUTH_STARTANGLE = 180;
 private final int MOUTH_ARCSWEEP = 180;

 private boolean activated;

 // default constructor definition
 public SmileFace() {
  super("Smile or Frown");
  setSize(400, 400);
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  activated = true;
  addWindowListener(new CheckActivated());
 }

 // paint method definition
 public void paint(Graphics g) {
  super.paint(g);

  g.drawOval(XFACE, YFACE, FACEDIAMETER, FACEDIAMETER);

  g.fillOval(X_RIGHTEYE, Y_RIGHTEYE, EYEWIDTH, EYEHEIGHT);
  g.fillOval(X_LEFTEYE, Y_LEFTEYE, EYEWIDTH, EYEHEIGHT);

  if (activated) {
   g.drawArc(XMOUTH, YMOUTH, MOUTHWIDTH, MOUTHHEIGHT, MOUTH_STARTANGLE, MOUTH_ARCSWEEP);
  } else {
   g.drawArc(XMOUTH, YMOUTH, MOUTHWIDTH, MOUTHHEIGHT, MOUTH_STARTANGLE, -MOUTH_ARCSWEEP);
  }
 }

 // CheckActivated class implements with windows listener
 private class CheckActivated implements WindowListener {

  // windowActivated method definition
  public void windowActivated(WindowEvent w) {
   activated = true;
   repaint();
  }

  // windowDeactivated method definition
  public void windowDeactivated(WindowEvent w) {
   activated = false;
   repaint();
  }

  public void windowDeiconified(WindowEvent w) {}

  public void windowIconified(WindowEvent w) {}

  public void windowOpened(WindowEvent w) {}

  public void windowClosed(WindowEvent w) {}

  public void windowClosing(WindowEvent w) {}

 }

 // main method definition
 public static void main(String args[]) {
  //create the object for class
  SmileFace pic = new SmileFace();
  pic.setVisible(true);
 }
}

 

0 0

Discussions

Post the discussion to improve the above solution.