I want to know how to paint the background based on the color of the hex value inputed by the user. I have this:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class SimpleColorCalc extends JFrame implements Runnable
{
ColorPanel cp;
JButton show;
JTextField hexCode;
public SimpleColorCalc()
{
super("Simple Color Calculator");
cp = new ColorPanel();
show = new JButton("Show the Color");
hexCode = new JTextField("ffffff");
show.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
String text = hexCode.getText();
try
{
int colorCode = Integer.parseInt(text, 16);
Color enteredColor = new Color(colorCode);
cp.setColor(enteredColor);
}
catch(NumberFormatException ex)
{
hexCode.setText("ffffff");
cp.setColor(Color.white);
}
finally
{
repaint();
}
}
});
}
public static void main(String[] args)
{
SimpleColorCalc scc = new SimpleColorCalc();
javax.swing.SwingUtilities.invokeLater(scc);
}
public void run()
{
setSize(400,300);
Container c = getContentPane();
JPanel top = new JPanel();
c.add(BorderLayout.NORTH, top);
top.setLayout(new GridLayout(1,2));
top.add(show);
top.add(hexCode);
setVisible(true);
}
}
But I want to know how to fix it such that incase the user decides to put a 0x in front of the hexadecimal code or not it will work. I also want to know how to convert the hex code into a color in java. I am having trouble with this.