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

//Please note that no more than one image should be displayed at any given time.

ID: 3863995 • Letter: #

Question

//Please note that no more than one image should be displayed at any given time.

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

public class LeavinesRadioButtons 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 JTextField animalTextField; // To hold user input
   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 = 400; // Window width
   private final int WINDOW_HEIGHT = 100; // wINDOW HEIGHT
  
   // Constructor
   public LeavinesRadioButtons()
   {
       // Set the title.
       setTitle("Animals");
      
       // Set the size of the window.
       setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
      
       // Specify an action for the close button.
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      
       // Creating a border layout manager.
       setLayout(new BorderLayout());
      
       // Build the panels and add it to the frame.
       buildRButtonPanel();
       buildImagePanel();
      
       // Add the panel to the frame's content pane.
       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.");
      
       // Add the label to the panel.
       imagePanel.add(imageLabel);
   }
  
   private void buildRButtonPanel()
   {
       ImageIcon Images;
      
       // Create a panel.
       panel = new JPanel();
      
       // Get the dog image.
       Images = new ImageIcon("dog.jpg");
       Images = new ImageIcon("bear.jpg");
       Images = new ImageIcon("other.jpg");
      
      
       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(animalTextField);
       panel.add(dogButton);
       panel.add(bearButton);
       panel.add(otherButton);
   }
  
   private class RadioButtonListener implements ActionListener
   {
       public void actionPerformed(ActionEvent e)
       {
           String input; // Holds user's input.
          
          
           // Get the input.
           input = animalTextField.getText();
          
           // Determine which radio button was clicked.
           if (e.getSource() == dogButton)
           {
               // Reads the image file.
               ImageIcon dogImage = new ImageIcon("dog.jpg");
              
               // Displays the image in the label.
               imageLabel.setIcon(dogImage);
              
               // Remove the text from the label
               imageLabel.setText(null);
           }
          
           else if (e.getSource() == bearButton)
           {
               // Reads the image file.
               ImageIcon bearImage = new ImageIcon("bear.jpg");
              
               // Displays the image in the label.
               imageLabel.setIcon(bearImage);
              
               // Remove the text from the label
               imageLabel.setText(null);
           }
          
           else if (e.getSource() == otherButton)
           {
               // Reads the image file.
               ImageIcon otherImage = new ImageIcon("other.jpg");
              
               // Displays the image in the label.
               imageLabel.setIcon(otherImage);
              
               // Remove the text from the label
               imageLabel.setText(null);
           }
          
          
       }
   }
  
   public static void main(String[] args)
   {
       new LeavinesRadioButtons();
   }
}

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.

Explanation / Answer

The program that performs the above mentioned functionality is:

  package com.demo;    import java.awt.*;  import java.awt.event.*;  import javax.swing.*;    public class LeavinesRadioButtons 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 JTextField animalTextField; // To hold user input      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 = 400; // Window width      private final int WINDOW_HEIGHT = 1000; // wINDOW HEIGHT          public LeavinesRadioButtons()      {          // Set the title.          setTitle("Animals");            // Set the size of the window.          setSize(WINDOW_WIDTH, WINDOW_HEIGHT);          setLayout(new GridLayout(3, 1));            // 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(animalTextField);          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("dog.jpg"));                  imageLabel.setText(null);              } else if( e.getSource() == (bearButton) )              {                  imageLabel.setIcon(getImageIcon("bear.jpg"));                  imageLabel.setText(null);              } else if (e.getSource() == otherButton)              {                  imageLabel.setIcon(getImageIcon("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 LeavinesRadioButtons();      }  }