I need help getting the following code to work correctly. When I click the JButt
ID: 3767694 • Letter: I
Question
I need help getting the following code to work correctly. When I click the JButton the smile face is supposed to go from a smile to a frown and visa versa. For some reason I have to click the button multiple times to make it work. Also please look over the rest of the code and make sure everthing makes sense. I'm not sure if I put repaint() in the proper places and if I have used the paint() method correctly. I would like to use my code so please don't just change it to something else completely but a few changes to make it correct would be greatly appreciated
Thanks!
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JSmileFace2 extends JFrame implements ActionListener
{
JButton pressButton = new JButton("Change");
boolean smile = true;
String title = "SmileyFace";
//Font font = new Font("Arial", Font.BOLD, 36);
private void smileFace(Graphics g)
{
setTitle(title);
setLayout(new FlowLayout());
add(pressButton);
pressButton.addActionListener(this);
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);
pressButton.repaint();
}
@Override
public void paint(Graphics g)
{
super.paint(g);
if (smile)
drawSmile(g);
else
drawFrown(g);
pressButton.repaint();
}
private void drawSmile(Graphics g)
{
smileFace(g);
g.drawArc(155, 240, 90, 30, 180, 180);
}
private void drawFrown(Graphics g)
{
smileFace(g);
g.drawArc(155, 240, 90, 30, 0, 180);
}
@Override
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if (source == pressButton && smile == false)
{
smile = true;
}
else if (source == pressButton && smile == true)
{
smile = false;
}
repaint();
}
public static void main(String []args)
{
JSmileFace2 smile = new JSmileFace2();
smile.setSize(400,400);
smile.setVisible(true);
smile.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Explanation / Answer
Fixed: I have commented out few line and added few.Find them in comments code below.
package com.chegg.nancy.solutions;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JSmileFace2 extends JFrame implements ActionListener {
JButton pressButton = new JButton("Change");
boolean smile = true;
String title = "SmileyFace";
// Font font = new Font("Arial", Font.BOLD, 36);
private void smileFace(Graphics g) {
setTitle(title);
setLayout(new FlowLayout());
add(pressButton);
pressButton.addActionListener(this);
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);
pressButton.repaint();
}
@Override
public void paint(Graphics g) {
super.paint(g);
// Added code here
if (smile) {
drawSmile(g);
smile = false;
} else {
drawFrown(g);
smile = true;
}
pressButton.repaint();
}
private void drawSmile(Graphics g) {
smileFace(g);
g.drawArc(155, 240, 90, 30, 180, 180);
}
private void drawFrown(Graphics g)
{
smileFace(g);
g.drawArc(155, 240, 90, 30, 0, 180);
}
@Override
public void actionPerformed(ActionEvent e) {
/*
* Object source = e.getSource();
*
* if (source == pressButton && smile == false)
*
* { smile = true; } else if (source == pressButton && smile == true) {
* smile = false; }
*/
repaint();
}
public static void main(String[] args) {
JSmileFace2 smile = new JSmileFace2();
smile.setSize(400, 400);
smile.setVisible(true);
smile.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.