Modify the attached version of the face with the winking eye. This version has t
ID: 3888937 • Letter: M
Question
Modify the attached version of the face with the winking eye. This version has the eye wink when the user clicks the button. Save as the program as ex145.java and change the program so the eye will continue to wink on its own. Follow the comments to modify the code. It will lesson flicker by only redrawing the eye that winks and not the background, face, or other eye. Add a static sleep so the face does not wink too fast */ import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ex1011c extends JApplet implements ActionListener { // get rid of all winkbutton code JButton winkbutton = new JButton("Wink At You"); boolean wink = false, first = true; Container c; public void init() { c = getContentPane(); c.setLayout(new FlowLayout()); c.setBackground(Color.blue); winkbutton.setForeground(Color.cyan); c.add(winkbutton); winkbutton.addActionListener(this); } // get rid of actionPerformed public void actionPerformed(ActionEvent e) { wink = !wink; repaint(); } public void paint(Graphics g) { /* if first time, draw the face and non winking eye, set first to false */ super.paint(g); g.setColor(Color.yellow); g.fillOval(50, 50, 100, 100); g.setColor(Color.black); g.fillOval(85, 80, 10, 20); /* cover just the eye that winks (if winking or not, but do not cover anything else), switch the wink boolean */ // draw the full eye or winking eye if (wink) g.fillOval(105, 88, 10, 5); else g.fillOval(105, 80, 10, 20); // go to sleep for a second // call repaint } // override update to lesson flicker }
Explanation / Answer
Updated Code for winking eye:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ex1011c extends JApplet implements ActionListener
{
Container c;
public void init()
{
c = getContentPane();
c.setLayout(new FlowLayout());
c.setBackground(Color.blue);
}
public void paint(Graphics g)
{
/* if first time, draw the face and non winking eye,
set first to false */
if(first == true) // draw complete face if called first time, otherwise draw just wiking eye
{
super.paint(g);
g.setColor(Color.yellow);
g.fillOval(50, 50, 100, 100);
g.setColor(Color.black);
g.fillOval(85, 80, 10, 20);
first = false;
}
else {
/* else block to lesson flicker by only redrawing the eye that winks if this is not the first time */
/* cover just the eye that winks (if winking or not, but do not
cover anything else), switch the wink boolean */
// draw the full eye or winking eye
if (wink)
{
g.fillOval(105, 88, 10, 5); // eye shut
wink = !wink; // flip wink for blinking
}
else
{
g.fillOval(105, 80, 10, 20); //eye open
wink = !wink; // flip wink for blinking
}
}
// go to sleep for a second
try {
thread.sleep(900);
} catch (Exception e) {
System.out.println("Killed while sleeping……….!!!!!!");
}
this.repaint(); // call paint again for flickering
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.