import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class program2 extends Applet implements WindowListener, AdjustmentListener, ActionListener { //declare ui variables Frame outerbox; Panel colorpad; TextField inputred, inputgreen, inputblue, hexredbox, hexgreenbox, hexbluebox; Scrollbar scrollred, scrollgreen, scrollblue; int redvalue, greenvalue, bluevalue; //values from Scrollbars String redtext, greentext, bluetext; //values from TextFields String hexredvalue, hexgreenvalue, hexbluevalue; //hex string values converted from integers public void init() { buildcomponents(); } //end init public void buildcomponents() { GridBagLayout gridbag = new GridBagLayout(); //initialize outerbox outerbox = new Frame(); outerbox.setTitle("COLOR PALETTE: Rajiv Zutshi"); outerbox.setLayout(gridbag); outerbox.setBackground(Color.white); outerbox.setSize(500, 200); outerbox.addWindowListener(this); //initialize colorpad colorpad = new Panel(); colorpad.setBackground(new Color(160,224,255)); //initialize textfields inputred = new TextField("160", 5); inputred.addActionListener(this); inputgreen = new TextField ("224", 5); inputgreen.addActionListener(this); inputblue = new TextField ("255", 5); inputblue.addActionListener(this); hexredbox = new TextField ("A0", 5); hexredbox.addActionListener(this); hexgreenbox = new TextField ("E0", 5); hexgreenbox.addActionListener(this); hexbluebox = new TextField ("FF", 5); hexbluebox.addActionListener(this); //initialize the red, green, blue scrollbars respectively scrollred = new Scrollbar(Scrollbar.HORIZONTAL, 160, 1, 0, 256); scrollred.setUnitIncrement(1); scrollred.setBlockIncrement(10); scrollred.addAdjustmentListener(this); scrollgreen = new Scrollbar(Scrollbar.HORIZONTAL, 224, 1, 0, 256); scrollgreen.setUnitIncrement(1); scrollgreen.setBlockIncrement(10); scrollgreen.addAdjustmentListener(this); scrollblue = new Scrollbar(Scrollbar.HORIZONTAL, 255, 1, 0, 256); scrollblue.setUnitIncrement(1); scrollblue.setBlockIncrement(10); scrollblue.addAdjustmentListener(this); GridBagConstraints gbc = new GridBagConstraints(); gbc.insets= new Insets (5, 5, 5, 5); //set panel constraints and add the textfields to the frame constraints(outerbox, colorpad, gridbag, gbc, 0, 0, 1, 3, gbc.BOTH, gbc.WEST, 3, 3); //set textfield constraints for HEX value boxes constraints(outerbox, hexredbox, gridbag, gbc, 1, 0, 1, 1, gbc.NONE, gbc.CENTER, gbc.NONE, gbc.NONE); constraints(outerbox, hexgreenbox, gridbag, gbc, 1, 1, 1, 1, gbc.NONE, gbc.CENTER, gbc.NONE, gbc.NONE); constraints(outerbox, hexbluebox, gridbag, gbc, 1, 2, 1, 1, gbc.NONE, gbc.CENTER, gbc.NONE, gbc.NONE); //set textfield constraints for RGB value boxes constraints(outerbox, inputred, gridbag, gbc, 2, 0, 1, 1, gbc.NONE, gbc.CENTER, gbc.NONE, gbc.NONE); constraints(outerbox, inputgreen, gridbag, gbc, 2, 1, 1, 1, gbc.NONE, gbc.CENTER, gbc.NONE, gbc.NONE); constraints(outerbox, inputblue, gridbag, gbc, 2, 2, 1, 1, gbc.NONE, gbc.CENTER, gbc.NONE, gbc.NONE); //set scrollbar constraints and add the scrollbars to the frame constraints(outerbox, scrollred, gridbag, gbc, 3, 0, 1, 1, gbc.BOTH, gbc.CENTER, 3, 3); constraints(outerbox, scrollgreen, gridbag, gbc, 3, 1, 1, 1, gbc.BOTH, gbc.CENTER, 3, 3); constraints(outerbox, scrollblue, gridbag, gbc, 3, 2, 1, 1, gbc.BOTH, gbc.CENTER, 3, 3); //display frame outerbox.setVisible(true); } //end buildframe public void constraints(Container cont, Component comp, GridBagLayout gbl, GridBagConstraints gbc, int gx, int gy, int gw, int gh, int fill, int anchor, int weightx, int weighty) { gbc.gridx = gx; gbc.gridy = gy; gbc.gridwidth = gw; gbc.gridheight = gh; gbc.fill = fill; gbc.anchor = anchor; gbc.weightx = weightx; gbc.weighty = weighty; gbl.setConstraints(comp, gbc); cont.add(comp); } //end addComponent //implement listener methods for window public void windowOpened(WindowEvent e) {} public void windowClosing(WindowEvent e) { Window outerbox = e.getWindow(); outerbox.dispose(); } public void windowClosed(WindowEvent e) {} public void windowIconified(WindowEvent e) {} public void windowDeiconified(WindowEvent e) {} public void windowActivated(WindowEvent e) {} public void windowDeactivated(WindowEvent e) {} //implement listener methods for adjustment - scrollbar public void adjustmentValueChanged(AdjustmentEvent e) { Object s = e.getSource(); //check to see which scroll bar was moved, then get the //integer value, convert it to a string and set the new //text in the textfield input box. if (s == scrollred) { redvalue = scrollred.getValue(); redtext = Integer.toString(redvalue); inputred.setText(redtext); hexredvalue = Integer.toHexString(redvalue); //convert the int to a hex string hexredbox.setText(hexredvalue); //set the hex value in the hex textfield } else if (s == scrollgreen) { greenvalue = scrollgreen.getValue(); greentext = Integer.toString(greenvalue); inputgreen.setText(greentext); hexgreenvalue = Integer.toHexString(greenvalue); hexgreenbox.setText(hexgreenvalue); } else if (s == scrollblue) { bluevalue = scrollblue.getValue(); bluetext = Integer.toString(bluevalue); inputblue.setText(bluetext); hexbluevalue = Integer.toHexString(bluevalue); hexbluebox.setText(hexbluevalue); } //mix the colors by calling a constructor colorpad.setBackground(new Color(redvalue, greenvalue, bluevalue)); } //end adjustmentValueChanged //implement listener methods for textfield changes public void actionPerformed (ActionEvent e) { Object s = e.getSource(); //check to see which input value was changed if (s == inputred) { redtext = inputred.getText(); //get the string value of the textfield redvalue = Integer.parseInt(redtext); //convert the string to an integer scrollred.setValue(redvalue); //set the scrollbar's thumb into position hexredvalue = Integer.toHexString(redvalue); //convert the int value to a hex string hexredbox.setText(hexredvalue); //set the hex value in the hex textfield } else if (s == inputgreen) { greentext = inputgreen.getText(); greenvalue = Integer.parseInt(greentext); scrollgreen.setValue(greenvalue); hexgreenvalue = Integer.toHexString(greenvalue); hexgreenbox.setText(hexgreenvalue); } else if (s == inputblue) { bluetext = inputblue.getText(); bluevalue = Integer.parseInt(bluetext); scrollblue.setValue(bluevalue); hexbluevalue = Integer.toHexString(bluevalue); hexbluebox.setText(hexbluevalue); } //mix the colors by calling a constructor colorpad.setBackground(new Color(redvalue, greenvalue, bluevalue)); } //end actionPerformed } //end program2