2014-01-10 1 views
0

HSB 색상 선택 도구를 만들고 있습니다. 색상의 채도, 채도, 밝기를 입력 할 수 있으며 입력 한 색상을 작은 사각형으로 만듭니다. 이제는 사용자가 작동하는 색상을 저장하도록 허용하고 있지만 '열기'버튼을 누르면 h, s 및 b 텍스트 입력 상자에 h, s 및 b 값이 표시됩니다. 저장된 색. 여기 내 코드는 다음과 같습니다.java - hsb 색상의 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; 
     } 
    } 

} 
} 

color1.color 파일에 h, s 및 b 값을 저장해야합니까? 나는 그것이 어떻게 일어날 지 모른다.

답변

0
당신은 Color 클래스가 제공하는 컨버터를 사용할 수 있습니다

: 그래서 당신은 아마 360 곱해 할 것이다 0과 1

Color color = Color.red; 
float [] hsb = Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(), null); 

또는

Color color = Color.red; 
float [] hsb = new float[3]; 
Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(), hsb); 

retured 값

이를 사용자가 프로그램 모델에 맞도록하십시오.

Javadoc for RGBtoHSB

+0

그래서 컬러 색상은 openhsb 같을까요? –

+0

예,'Color' 객체 만 저장할 수 있습니다. hsb를 가져와야 할 때'RGBtoHSB' 메소드를 사용하여 변환 할 수 있습니다. 그러나 hsb 값에 자주 액세스해야하는 경우 (드로잉 스레드에서) 색상을 변경할 때 값을 저장하고 업데이트 할 수 있습니다. –

관련 문제