I need help writing a application that extends JFrame which will display a yello
ID: 3766971 • Letter: I
Question
I need help writing a application that extends JFrame which will display a yellow smiling face on the screen. save as JSmileFace.java. Then I need to add a JButton to the JSmileFace program so the smile changes to a frown when the user clicks the JButton. Save the file as JSmileFace2.java. I have pasted the code below but cannot get it to work .Please add a main.
thanks!
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class smileyFace2 extends JApplet implements ActionListener
{
private JButton pressButton = new JButton("Change");
private boolean smile = true;
public void init()
{
Container con = getContentPane();
con.setLayout(new FlowLayout());
con.add(pressButton);
pressButton.addActionListener(this);
}
public void paint(Graphics g)
{
if (smile)
drawSmile(g);
else
drawFrown(g);
pressButton.repaint();
}
private void smileFace(Graphics g)
{
g.setColor(Color.yellow); // to display a yellow smileface
g.drawArc(150, 100, 100, 200, 360, 360);
g.fillArc(100, 100, 200, 200, 360, 360);
g.setColor(Color.black);
g.drawOval(145, 150, 40, 60);
g.fillOval(145, 150, 40, 60);
g.drawOval(225, 150, 35, 60);
g.fillOval(225, 150, 35, 60);
}
private void drawSmile(Graphics g)
{
smileFace(g);
g.drawArc(155, 240, 90, 30, 180, 180);
}
//to change smile to frown on calling this function
private void drawFrown(Graphics g)
{
smileFace(g);
g.drawArc(155, 240, 90, 30, 0, 180);
}
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if (source == pressButton) // if user presses button then smile
{
smile = true;
repaint();
} // if user doesnt press buttone no smile
else if (source == pressButton)
{
smile = false;
repaint();
}
}
}
Explanation / Answer
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class smileyFace2 extends JApplet implements ActionListener
{
private JButton pressButton = new JButton("Change");
private boolean smile = true;
public void init()
{
Container con = getContentPane();
con.setLayout(new FlowLayout());
con.add(pressButton);
pressButton.addActionListener(this);
}
public void paint(Graphics g)
{
if (smile)
drawSmile(g);
else
drawFrown(g);
pressButton.repaint();
}
private void smileFace(Graphics g)
{
g.setColor(Color.yellow);
g.drawArc(150, 100, 100, 200, 360, 360);
g.fillArc(100, 100, 200, 200, 360, 360);
g.setColor(Color.black);
g.drawOval(145, 150, 40, 60);
g.fillOval(145, 150, 40, 60);
g.drawOval(225, 150, 35, 60);
g.fillOval(225, 150, 35, 60);
}
private void drawSmile(Graphics g)
{
smileFace(g);
g.drawArc(155, 240, 90, 30, 180, 180);
}
//to change smile to frown on calling this function
private void drawFrown(Graphics g)
{
smileFace(g);
g.drawArc(155, 240, 90, 30, 0, 180);
}
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if (source == pressButton)
{
smile = true;
repaint();
}
else if (source == pressButton)
{
smile = false;
repaint();
}
public static void main(String []args)
{
closeframe d = new closeframe();
d.setsize(300,300);
d.settitle("smile face");
d.setvisible(true);
d.addwindowlistener(new windowadapter ()
{
public void windowclosing (windowevent e)
{
system.exit(0);
}
});
}
}