import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class ColorRadioButtonPanel extends JPanel implements ActionListener { //{ private ButtonGroup bg = new ButtonGroup(); private MiniPaint parent; private ScribblePanel scribblePanel; public ColorRadioButtonPanel (ScribblePanel sp) { scribblePanel = sp; setUpPanel(); } private void setUpPanel() { setLayout(new GridLayout(11, 0)); setBorder(BorderFactory.createEmptyBorder(30, 50, 20, 0)); // radio buttons createRadioButton("black"); blackButton.setSelected(true); createRadioButton("blue"); createRadioButton("cyan"); createRadioButton("gray"); createRadioButton("green"); createRadioButton("magenta"); createRadioButton("orange"); createRadioButton("pink"); createRadioButton("red"); createRadioButton("white"); createRadioButton("yellow"); } JRadioButton createRadioButton(String color) { JRadioButton rb = new JRadioButton(color); bg.add(rb); //add the radio button to the button group rb.addActionListener(this); // add the listener to each button add(rb); // add the radio button to the panel return rb; } public void actionPerformed(ActionEvent e) { scribblePanel.changeColor(stringToColor(e.getActionCommand())); } private Color stringToColor(String c) { if (c.equals("orange")) return Color.ORANGE; if (c.equals("blue")) return Color.BLUE; if (c.equals("cyan")) return Color.CYAN; if (c.equals("green")) return Color.GREEN; if (c.equals("magenta")) return Color.MAGENTA; if (c.equals("gray")) return Color.GRAY; if (c.equals("pink")) return Color.PINK; if (c.equals("red")) return Color.RED; if (c.equals("white")) return Color.WHITE; if (c.equals("yellow")) return Color.YELLOW; return Color.BLACK; } }