Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a New Java Project called YourLastNameRadioButtons. Write a program with

ID: 3804889 • Letter: C

Question

Create a New Java Project called YourLastNameRadioButtons. Write a program with three radio buttons, labeled DOG, BEAR, and OTHER. When the user clicks on the radio button labeled DOG, an image of a dog should be displayed in the window. When the user clicks on the radio button labeled BEAR, an image of an bear should be displayed in the window. When the user clicks on the radio button labeled OTHER, an image, of your choosing, should be displayed. Please note that no more than one image should be displayed at any given time

Explanation / Answer

solution: hi,

NOTE: change image address according to you where you place these images.

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class YourLastNameRadioButtons extends JFrame
{
private JPanel panel; // A holding panel
private JPanel imagePanel; // A holding panel
private JLabel imageLabel; // Holds the image
private JLabel messageLabel; // A message to the user
private JRadioButton dogButton; // Holds radio button for DOG
private JRadioButton bearButton; // Holds radio button for BEAR
private JRadioButton otherButton; // Holds radio button for OTHER
private ButtonGroup radioButtonGroup; // To group radio buttons
private final int WINDOW_WIDTH = 800; // Window width
private final int WINDOW_HEIGHT = 200; // wINDOW HEIGHT
  
// Constructor
public YourLastNameRadioButtons()
{
// Set the title.
setTitle("Animals");
  
// Set the size of the window.
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setLayout(new GridLayout(2, 8));
  
// Specify an action for the close button.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
// Build the panels and add it to the frame.
imageLabel = new JLabel("", JLabel.CENTER);
imageLabel.setSize(350, 100);
buildImagePanel();
add(imageLabel);
add(messageLabel);
  
// Add the panel to the frame's content pane.
buildRButtonPanel();
add(panel);
  
// Display the window.
setVisible(true);
}
  
private void buildImagePanel()
{
// Create a panel.
imagePanel = new JPanel();
  
// Create the label, text field, and radio buttons.
messageLabel = new JLabel("Click the button to see the animal picture.", JLabel.CENTER);
  
// Add the label to the panel.
imagePanel.add(imageLabel);
}
  
private void buildRButtonPanel()
{
  
//animalTextField = new JTextField(10);
dogButton = new JRadioButton("DOG"); //FIND PICTURE ON GOOGLE
bearButton = new JRadioButton("BEAR"); //FIND PICTURE ON GOOGLE
otherButton = new JRadioButton("OTHER"); //FIND PICTURE ON GOOGLE
  
// Group the radio buttons.
radioButtonGroup = new ButtonGroup();
radioButtonGroup.add(dogButton);
radioButtonGroup.add(bearButton);
radioButtonGroup.add(otherButton);
  
// Action listeners to the radio buttons.
dogButton.addActionListener(new RadioButtonListener());
bearButton.addActionListener(new RadioButtonListener());
otherButton.addActionListener(new RadioButtonListener());
  
// Create a panel and add the components to it.
panel = new JPanel();
panel.add(messageLabel);
panel.add(dogButton);
panel.add(bearButton);
panel.add(otherButton);
  
setVisible(true);
}
  
private class RadioButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if( e.getSource().equals(dogButton))
{
imageLabel.setIcon(getImageIcon("file:///C:/Users/Rahul.16.k/Desktop/dog.jpg"));
imageLabel.setText(null);
} else if( e.getSource() == (bearButton) )
{
imageLabel.setIcon(getImageIcon("file:///C:/Users/Rahul.16.k/Desktop/bear.jpg"));
imageLabel.setText(null);
} else if (e.getSource() == otherButton)
{
imageLabel.setIcon(getImageIcon("file:///C:/Users/Rahul.16.k/Desktop/other.jpg"));
imageLabel.setText(null);
}
}
}

//This method scales the image to a given size.
private ImageIcon getImageIcon(String fileName)
{
return new ImageIcon(new ImageIcon(fileName).getImage().getScaledInstance(400, 400, Image.SCALE_DEFAULT));
}

public static void main(String[] args)
{
new YourLastNameRadioButtons();
}
}