/** a mix of old and new java... originally written ages ago in java 1.0, recently spiffed up using some swing. runs OK using netscape under X-window, which is probably as narrow a channel is it would need to pass through. [ miles zarathustra www.aranyaka.org ] */ import java.applet.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; class Util { /** representation of n (the 1st parameter) to width w (2nd parameter) padded on the left with 0 */ public static String zeroPadLeft(int n, int w) { return padLeft(""+n, w, '0'); } /** pad string str to the left, to width w, with charcter c. */ public static String padLeft(String str, int w, char c) { if (str.length() >= w) return str; StringBuffer sb=new StringBuffer(); for (int i = w - str.length(); i > 0; --i) sb.append(c); sb.append(str); return new String(sb); } } class Shade extends Canvas { float h=0.5f, s=0.5f, b=0.5f; Font font; Shade() { font=new Font("TimesRoman",Font.BOLD,20); } void setShade(float H, float S, float B) { setBackground( new Color(Color.HSBtoRGB( h=Math.min(1.0f,H/1000.0f), s=Math.min(1.0f,S/1000.0f), b=Math.min(1.0f,B/1000.0f) )) ); repaint(); } public void paint(Graphics g) { Color bg=getBackground(); setForeground(new Color(~bg.getRGB())); g.setFont(font); } } public class Hue extends Applet { Scrollbar hue, sat, bri; Shade shade; TextField hueText, satText, briText, colorText; static TextField newTextField() { TextField R = new TextField(8); R.setEditable(false); R.setBackground(Color.white); return R; } public Hue() { setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); Panel textPanel = new Panel(new FlowLayout()), huePanel = new Panel(new BorderLayout()); textPanel.add(new Label("color:")); textPanel.add(colorText = newTextField()); textPanel.add(new Label(" H=")); textPanel.add(hueText = newTextField()); textPanel.add(new Label("; S=")); textPanel.add(satText = newTextField()); textPanel.add(new Label("; B=")); textPanel.add(briText = newTextField()); huePanel.add("Center",shade=new Shade()); int maxVal = 1010; // I forget why the extra 10, // but there must have been a good reason. huePanel.add("West",bri=new Scrollbar(Scrollbar.VERTICAL, (int)(maxVal * shade.b),10,0,maxVal)); huePanel.add("North",hue=new Scrollbar(Scrollbar.HORIZONTAL, (int)(maxVal * shade.h),10,0,maxVal)); huePanel.add("East",sat=new Scrollbar(Scrollbar.VERTICAL, (int)(maxVal * shade.s),10,0,maxVal)); add(textPanel); add(huePanel); setColor(); setTextFields(); } public void init() { } void setColor() { float h=hue.getValue(),s=sat.getValue(),b=bri.getValue(); shade.setShade(h,s,b); } void setTextFields() { hueText.setText(shade.h+""); satText.setText(shade.s+""); briText.setText(shade.b+""); colorText.setText( Util.padLeft( Integer.toString(0xffffff & shade.getBackground().getRGB(),16), 6, '0')); } public boolean handleEvent(Event evt) { if (evt.target==hue || evt.target==sat || evt.target==bri) { setColor(); setTextFields(); return true; } return false; } public static void main(String[] args) { final Frame f=new Frame("hue"); f.add(new Hue(),BorderLayout.CENTER); f.pack(); f.setSize(600,600); //f.addWindowListener(new WindowAdapter() { class WindowCloser extends WindowAdapter { public void windowClosing(WindowEvent evt) { f.dispose(); System.exit(0); } } f.addWindowListener(new WindowCloser()); f.setVisible(true); } }