【发布时间】:2014-01-10 22:50:08
【问题描述】:
我正在制作一个 HSB 颜色选择器。您可以输入颜色的色相、饱和度、亮度,它会将您输入的颜色变成一个小方块。现在我允许用户保存颜色,这是可行的,但我希望这样当您按下“打开颜色”按钮时,h、s 和 b 文本输入框会显示 h、s 和 b 值保存的颜色。这是我的代码:
@SuppressWarnings("serial")
public class Main extends Applet implements ActionListener, java.io.Serializable {
private Rectangle color;
Random randomColor = new Random();
TextField one;
TextField two;
TextField three;
Button enter;
Button random;
Button save;
Button open;
String hvalue="255";
String svalue="0";
String bvalue="255";
Color hsb = Color.getHSBColor(Integer.parseInt(hvalue), Integer.parseInt(bvalue), Integer.parseInt(svalue));
int width = 320;
int height = 260;
String version = "0.0.1";
String name = "Color Picker";
public void init() {
setSize(width, height);
Frame c = (Frame)this.getParent().getParent();
c.setTitle(name + " - Version " + version);
setLayout(null);
color = new Rectangle(width/2-32, 20, 64, 64);
one = new TextField("", 15);
one.setBounds(width/2-60, height-120, 40, 20);
add(one);
two = new TextField("", 15);
two.setBounds(width/2-20, height-120, 40, 20);
add(two);
three = new TextField("", 15);
three.setBounds(width/2+20, height-120, 40, 20);
add(three);
enter = new Button("Enter");
enter.setBounds(width/2-50, height-80, 100, 20);
add(enter);
enter.addActionListener(this);
random = new Button("Random Color");
random.setBounds(width/2-50, height-60, 100, 20);
add(random);
random.addActionListener(this);
save = new Button("Save Color");
save.setBounds(width/2-50, height-40, 100, 20);
add(save);
save.addActionListener(this);
open = new Button("Open Color");
open.setBounds(width/2-50, height-20, 100, 20);
add(open);
open.addActionListener(this);
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
g2.setColor(hsb);
g2.fill(color);
}
@Override
public void actionPerformed(ActionEvent evt) {
if (evt.getSource() == enter) {
hvalue = one.getText();
svalue = two.getText();
bvalue = three.getText();
try {
hsb = Color.getHSBColor(Integer.parseInt(hvalue), Integer.parseInt(bvalue), Integer.parseInt(svalue));
repaint();
}
catch (Exception e) {}
}
else if (evt.getSource() == random) {
hvalue = String.valueOf(randomColor.nextInt(200));
svalue = String.valueOf(randomColor.nextInt(200));
bvalue = String.valueOf(randomColor.nextInt(200));
one.setText(hvalue);
two.setText(svalue);
three.setText(bvalue);
try {
hsb = Color.getHSBColor(Integer.parseInt(hvalue), Integer.parseInt(bvalue), Integer.parseInt(svalue));
repaint();
}
catch (Exception e) {}
}
else if (evt.getSource() == save) {
try {
FileOutputStream fileOut = new FileOutputStream("/C:/Users/Barbara/Downloads/color.color");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(hsb);
out.close();
fileOut.close();
} catch(IOException i) {
i.printStackTrace();
}
}
else if (evt.getSource() == open) {
Color openhsb = null;
try {
FileInputStream fileIn = new FileInputStream("/C:/Users/Barbara/Downloads/color.color");
ObjectInputStream in = new ObjectInputStream(fileIn);
openhsb = (Color) in.readObject();
in.close();
fileIn.close();
try {
hsb = openhsb;
repaint();
}
catch (Exception e) {}
} catch(IOException i) {
i.printStackTrace();
return;
} catch(ClassNotFoundException c) {
c.printStackTrace();
return;
}
}
}
}
我应该将 h、s 和 b 值保存到 color1.color 文件吗?如果是这样的话,我不知道该怎么做。
【问题讨论】: