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:5 | ISBN:9780132830317 | Edition: 5

Question

Enhance the face drawing in Display 18.17 in the following ways: Add color so the eyes are blue and the mouth is red. When the face winks, the line that represents a closed eye is black not blue. Add a nose and a brown handlebar mustache. Add buttons labeled " Smile " and " Frown ". When the " Frown " button is clicked, the face shows a frown (upside down smile); when the "Smile" button is clicked, the face shows a smile. When the user clicks the close-window button, a window pops up to ask if the user is sure he or she wants to exit, as in Display 18.2.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DrawingExtended extends JFrame implements ActionListener
{
	private final int FACEDIAMETER = 200;
	private final int XFACE = 100;
	private final int YFACE = 100;
	
	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;
	
	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 wink;
	private boolean smile;
	
	private JButton Buttonsmile;
	private JButton Buttonfrown;
	private JButton Buttonwink;
	private JPanel buttonPanel;
	
// default constructor
	public DrawingExtended()
	{
     	super("Smile or Frown");
		setSize(400, 400);
		setLayout(new BorderLayout());	
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		smile = true;
		wink = false;
		buttonPanel = new JPanel();
		buttonPanel.setLayout(new FlowLayout());

		Buttonsmile = new JButton("Smile!");
		Buttonfrown = new JButton("Frown!");
		Buttonwink = new JButton("wink!");
		Buttonsmile.setActionCommand("smile");
		Buttonfrown.setActionCommand("frown");
		Buttonwink.setActionCommand("wink");
		Buttonsmile.addActionListener(this);
		Buttonfrown.addActionListener(this);
		Buttonwink.addActionListener(this);

		buttonPanel.add(Buttonsmile);
		buttonPanel.add(Buttonfrown);
		buttonPanel.add(Buttonwink);
		this.add(buttonPanel);
		
		addWindowListener(new Listener());
	} 
	
     // paint method
	public void paint(Graphics g)
	{
		super.paint(g);
		g.drawOval(XFACE, YFACE, FACEDIAMETER, FACEDIAMETER);
		g.setColor(Color.BLUE);
		g.fillOval(X_RIGHTEYE, Y_RIGHTEYE, EYEWIDTH, EYEHEIGHT);
		if(!wink)
			g.fillOval(X_LEFTEYE, Y_LEFTEYE, EYEWIDTH, EYEHEIGHT);
		else
		{
			g.setColor(Color.BLACK);
			g.drawLine(X_LEFTEYE, Y_LEFTEYE, X_LEFTEYE + EYEWIDTH, Y_LEFTEYE);
		}
		g.setColor(Color.RED);
		if(smile)
		{
			g.drawArc(XMOUTH, YMOUTH, MOUTHWIDTH, MOUTHHEIGHT, MOUTH_STARTANGLE, MOUTH_ARCSWEEP);
		}
		else
		{
			g.drawArc(XMOUTH, YMOUTH, MOUTHWIDTH, MOUTHHEIGHT, MOUTH_STARTANGLE, -MOUTH_ARCSWEEP);
		}
	} 
	
	public void actionPerformed(ActionEvent e)
	{
		String s = e.getActionCommand();

		if(s.equals("smile"))
		{
			smile = true;
			repaint();
		}

		if(s.equals("frown"))
		{
			smile = false;
			repaint();
		}

		if(s.equals("wink"))
		{
			wink = !wink;
			repaint();
		}
	}
	private class Listener implements WindowListener
	{

		public void windowActivated(WindowEvent arg0) {}
		public void windowDeactivated(WindowEvent arg0) {}
		public void windowDeiconified(WindowEvent arg0){}
		public void windowIconified(WindowEvent arg0) {}
		public void windowOpened(WindowEvent arg0) {}
		public void windowClosed(WindowEvent arg0){}	

		public void windowClosing(WindowEvent arg0)
		{
			ConfirmWindow window = new ConfirmWindow();
			window.setVisible(true);
		}
} 
// ConfirmWindow class
private class ConfirmWindow extends JFrame implements ActionListener
	{
        // default constructor of ConfirmWindow class
		public ConfirmWindow()
		{
			setSize(200, 100);
			getContentPane().setBackground(Color.YELLOW);
			setLayout(new BorderLayout());

			JLabel confirmLabel = new JLabel("Are you sure you want to exit?");
			add(confirmLabel, BorderLayout.CENTER);
			
			JPanel buttonPanel = new JPanel();
			buttonPanel.setBackground(Color.ORANGE);
			buttonPanel.setLayout(new FlowLayout());
			
			JButton exitButton = new JButton("Yes");
			exitButton.addActionListener(this);
			buttonPanel.add(exitButton);
			
			JButton cancelButton = new JButton("NO");
			cancelButton.addActionListener(this);
			buttonPanel.add(cancelButton);
			
			add(buttonPanel, BorderLayout.SOUTH);
		} 
		
		public void actionPerformed(ActionEvent e)
		{

			String actionCommand = e.getActionCommand();
			if(actionCommand.equals("Yes"))
				System.exit(0);

			else if (actionCommand.equals("No"))
				dispose();
			else
				System.out.println("Unexpected error in Confirm window.");
		} 
	} 

     // main method
	public static void main(String args[])
	{
		DrawingExtended pic = new DrawingExtended();
		pic.setVisible(true);
	} 	
} 

 

Output:

0 0

Discussions

Post the discussion to improve the above solution.