/** * @(#)KenColorChooser.java * * KenColorChooser application * * @author * @version 1.00 2011/5/11 */ import java.awt.*; import javax.swing.*; import java.util.*; import java.awt.event.*; import java.math.*; public class KenColorChooser extends JFrame implements ActionListener{ private JTextArea j; public static void main(String[] args) { KenColorChooser s = new KenColorChooser(); s.setVisible(true); s.setLocationRelativeTo(null); } public KenColorChooser(){ super(); setSize(500,500); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setTitle("Color Chooser by Professor Kenneth Moore CCAC"); Font largeFontBOLD = new Font("Calibri", Font.BOLD,22); setLayout(new BorderLayout()); j = new JTextArea(" Now is the time \n for all good men\n to come to the aid \n of their country"); j.setOpaque(true); j.setFont(largeFontBOLD); j.setBackground(Color.blue); j.setForeground(Color.yellow); add(j, BorderLayout.CENTER); JPanel b = new JPanel(); b.setLayout(new GridLayout(1,2)); JButton back = new JButton("Set Background"); b.add(back); JButton fore = new JButton("Set Foreground"); b.add(fore); back.addActionListener(this); fore.addActionListener(this); add(b,BorderLayout.SOUTH); } String getHex(int i){ BigInteger b = new BigInteger(""+i); String s = b.toString(16); if(s.equals("0"))s = "00"; s = s.toUpperCase(); return s; } void addString(Color c,String s){ int red=0, green=0, blue=0; red = c.getRed(); green = c.getGreen(); blue = c.getBlue(); j.append(s +"\nDecimal("+red+","+green+","+blue+")"); j.append("\nHex #"+getHex(red)+getHex(green)+getHex(blue)+"\n"); } public void actionPerformed(ActionEvent e){ if(e.getActionCommand().equals("Set Background")){ Color newColor = JColorChooser.showDialog( KenColorChooser.this, "Choose Background Color", Color.white); j.setBackground(newColor); } else if(e.getActionCommand().equals("Set Foreground")){ Color newColor = JColorChooser.showDialog( KenColorChooser.this, "Choose Foreground Color", Color.white); j.setForeground(newColor); } j.setText(" Now is the time \n for all good men\n to come to the aid \n of their country\n"); Color fg = j.getForeground(); addString(fg,"\nForeground"); Color bg = j.getBackground(); addString(bg,"\nBackground"); } }