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

The fourth programming project focuses on demonstrating your understanding of Ja

ID: 3538527 • Letter: T

Question

The fourth programming project focuses on demonstrating your understanding of Java Collections. Before attempting this project, be sure you have completed all of the reading assignments listed in the syllabus to date, participated in the weekly conferences, and thoroughly understand the examples throughout the chapters. The project requirements include:


Write a Java application that effectively uses Java collections to store pairs of unique colors and their unique hexadecimal values. (For example, Red -> FF0000). Store up to 20 of these pairs. Then write a GUI that displays the hexadecimal values using radio buttons to select a value. When selected the background of the GUI should change to that color.

Your program should compile and run without errors.

Explanation / Answer


import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JRadioButton;


public class ColorGUI {
   
    JFrame frame;
    Map<String, Color> colorMap = new HashMap<String, Color>();
    public ColorGUI() {
       
        colorMap.put("#FF0000", Color.RED);
        colorMap.put("#00FF00", Color.GREEN);
        colorMap.put("#0000FF", Color.BLUE);
        colorMap.put("#FFFF00", Color.YELLOW);
        colorMap.put("#FFA500", Color.ORANGE);
        colorMap.put("#FFFFFF", Color.BLACK);
       
        //you can add remaining colors here...
       
       
        frame = new JFrame("Color Demo");
        JRadioButton[] buttons = new JRadioButton[20];
        ButtonGroup buttonGroup = new ButtonGroup();
        String[] keys = new String[colorMap.size()];
        colorMap.keySet().toArray(keys);
        for(int i=0; i < colorMap.size(); i++) {
           
           
            buttons[i] = new JRadioButton(keys[i]);
            buttonGroup.add(buttons[i]);
            frame.add(buttons[i]);
            buttons[i].addActionListener(new Action());
           
        }
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout());
        frame.setSize(100,700);
        frame.setVisible(true);
       
    }
    public class Action implements ActionListener{
        public void actionPerformed(ActionEvent e){
            frame.getContentPane().setBackground(colorMap.get(e.getActionCommand()));
           
       
        }
    }
    public static void main(String[] args) {
        new ColorGUI();
    }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote