https://slcc.instructure.com/courses/260614/assignments/1586307#submit I will gi
ID: 3559463 • Letter: H
Question
https://slcc.instructure.com/courses/260614/assignments/1586307#submit
I will give you 3000 points instead of 1500 if you can complete this assignment.
Paint images on a JPanel Dynamically update the images Continue your practice of event handling Use check boxes Practice composition Create a gui application that provides the following functionality: It has a control panel on the left side The control panel consists of one submit button and 3 check boxes that correspond to three features (eyes, nose, and mouth) All the features, whose check boxes are selected, change when the submit button is clicked If none of the check boxes is selected the face itself changes It has a face on the right side, that changes depending on the check box selection Make sure that all the image files are part of the project and that they are accessed by using the resources of the class. (If you use absolute paths from your computer I won't be able to run it) Look at Assignment Face.mp4 to see a running version of the finished assignment. Your solution might look different because no images are provided. This is your opportunity to be creative and to have fun.Explanation / Answer
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);
}
}
}
);
public static void main(String args[]) throws IOException
{
Faces f = new Faces();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.