Please check this code. it isnt compiling. it is a gui that is a face that chang
ID: 3678680 • Letter: P
Question
Please check this code. it isnt compiling. it is a gui that is a face that change appearance with the click of a button
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class Faces extends JFrame
{
private JPanel panel1, panel2;
private JCheckBox eyes, nose, mouth;
private JButton submit;
private ButtonGroup group;
public Faces() throws IOException
{
//Setting up the Title of the Window
super("Expression Game");
//Set Size of the Window (WIDTH, LENGTH)
setSize(775,300);
//Exit Property of the Window
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
eyes = new JCheckBox("Eyes",false);
nose = new JCheckBox("Nose",false);
mouth = new JCheckBox("Mouth",false);
submit=new JButton("Submit");
panel1 = new JPanel();
panel1.setLayout(new GridLayout(1,4)); //setting layout on JPanel 1
//Adding JRadioButton in JPanel
panel1.add(eyes);
panel1.add(nose);
panel1.add(mouth);
group = new ButtonGroup();
//Group the four radio button using the class ButtonGroup
group.add(eyes);
group.add(nose);
group.add(mouth);
panel2 = new JPanel();
panel2.setLayout(new GridLayout(1,4));
BufferedImage wPic = ImageIO.read(this.getClass().getResource("img1.png"));
JLabel wIcon = new JLabel(new ImageIcon(wPic));
panel2.add(wIcon);
//Setting up the container ready for the components to be added.
Container pane = getContentPane();
setContentPane(pane);
pane.add(panel1, BorderLayout.WEST);
pane.add(panel2,BorderLayout.EAST);
GridLayout grid = new GridLayout(2,1);
pane.setLayout(grid);
submit.addActionListener
(
new ActionListener()
{
//Handle JButton event if it is clicked
public void actionPerformed(ActionEvent event)
{
int total=0;
int opt=0;
if(eyes.isSelected()==true)
{
BufferedImage wPic1 = ImageIO.read(this.getClass().getResource("eyes.png"));
JLabel wIcon1 = new JLabel(new ImageIcon(wPic1));
panel2.add(wIcon1);
}
if(nose.isSelected()==true)
{
BufferedImage wPic2 = ImageIO.read(this.getClass().getResource("nose.png"));
JLabel wIcon2 = new JLabel(new ImageIcon(wPic2));
panel2.add(wIcon2);
}
if(mouth.isSelected()==true)
{
BufferedImage wPic3 = ImageIO.read(this.getClass().getResource("img1.png"));
JLabel wIcon3 = new JLabel(new ImageIcon(wPic3));
panel2.add(wIcon3);
}
else
{
BufferedImage wPic3 = ImageIO.read(this.getClass().getResource("mouth.png"));
JLabel wIcon3 = new JLabel(new ImageIcon(wPic3));
panel2.add(wIcon3);
}
}
}
); //Problems with this semi colon//
public static void main(String args[]) throws IOException
{
Faces f = new Faces();
}
}
Explanation / Answer
I have fixed all compilation error.
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Faces extends JFrame {
private JPanel panel1, panel2;
private JCheckBox eyes, nose, mouth;
private JButton submit;
private ButtonGroup group;
public Faces() throws IOException {
// Setting up the Title of the Window
super("Expression Game");
// Set Size of the Window (WIDTH, LENGTH)
setSize(775, 300);
// Exit Property of the Window
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
eyes = new JCheckBox("Eyes", false);
nose = new JCheckBox("Nose", false);
mouth = new JCheckBox("Mouth", false);
submit = new JButton("Submit");
panel1 = new JPanel();
panel1.setLayout(new GridLayout(1, 4)); // setting layout on JPanel 1
// Adding JRadioButton in JPanel
panel1.add(eyes);
panel1.add(nose);
panel1.add(mouth);
group = new ButtonGroup();
// Group the four radio button using the class ButtonGroup
group.add(eyes);
group.add(nose);
group.add(mouth);
panel2 = new JPanel();
panel2.setLayout(new GridLayout(1, 4));
BufferedImage wPic = ImageIO.read(this.getClass().getResource(
"img1.png"));
JLabel wIcon = new JLabel(new ImageIcon(wPic));
panel2.add(wIcon);
// Setting up the container ready for the components to be added.
Container pane = getContentPane();
setContentPane(pane);
pane.add(panel1, BorderLayout.WEST);
pane.add(panel2, BorderLayout.EAST);
GridLayout grid = new GridLayout(2, 1);
pane.setLayout(grid);
submit.addActionListener(new ActionListener() {
// Handle JButton event if it is clicked
@Override
public void actionPerformed(ActionEvent event) {
int total = 0;
int opt = 0;
if (eyes.isSelected() == true) {
BufferedImage wPic1 = null;
try {
wPic1 = ImageIO.read(this.getClass().getResource(
"eyes.png"));
} catch (IOException e) {
}
JLabel wIcon1 = new JLabel(new ImageIcon(wPic1));
panel2.add(wIcon1);
}
if (nose.isSelected() == true) {
BufferedImage wPic2 = null;
try {
wPic2 = ImageIO.read(this.getClass().getResource(
"nose.png"));
} catch (IOException e) {
}
JLabel wIcon2 = new JLabel(new ImageIcon(wPic2));
panel2.add(wIcon2);
}
if (mouth.isSelected() == true) {
BufferedImage wPic3 = null;
try {
wPic3 = ImageIO.read(this.getClass().getResource(
"img1.png"));
} catch (IOException e) {
}
JLabel wIcon3 = new JLabel(new ImageIcon(wPic3));
panel2.add(wIcon3);
} else {
BufferedImage wPic3 = null;
try {
wPic3 = ImageIO.read(this.getClass().getResource(
"mouth.png"));
} catch (IOException e) {
}
JLabel wIcon3 = new JLabel(new ImageIcon(wPic3));
panel2.add(wIcon3);
}
}
}
); // Problems with this semi colon//
}
public static void main(String args[]) throws IOException {
Faces f = new Faces();
}
}