import java.awt.*; import java.awt.event.*; /*CIS144java-Fall2000*/ import java.applet.*; /*Abbas Moghtanai */ import java.util.*; /*Dan Carte */ import java.io.*; /**************************************************************************$*/ /* The purpose of this program is for the user to be able to modulate */ /*the color and display the value of that color in either hex ,decimal or */ /*octal. The size of the RGB rectangle's as well as the brightness should */ /*be proportional to the value of the individual colors. The size if the */ /*incremental steps can be modulated either via buttons of the user could */ /*opt to use the text field. In a addition there is a rectangle at the botom*/ /*of the screen that show how the colors look when they are mixed together. */ /****************************************************************************/ /* */ public class ShowColor extends Applet implements ActionListener { String str = ""; int Red=1,Green=1,Blue=1,dRed=1,dGreen=1,dBlue=1; /*Putting the buttons in*/ Button bList[]=new Button[16]; /*array means we can use*/ TextField redBox,greenBox,blueBox,color; /*a loop to add the buttons*/ Checkbox hex,octal,decimal; CheckboxGroup cbg; /*Have to use a group to make*/ /*the CheckBoxes exclusive. */ public void init() { /*Set the default Colors*/ setBackground(Color.lightGray); setForeground(Color.red); setLayout(null); /*Kill LayoutManager! */ /**************************************************************************$*/ /* Initialize all the buttons and textfields. */ /**************************************************************************$*/ Button rInc =new Button("Inc Red"); Button rDec =new Button("Dec Red"); Button rReset =new Button("Reset"); Button gInc =new Button("Inc Green"); Button gDec =new Button("Dec Green"); Button gReset =new Button("Reset"); Button bInc =new Button("Inc Blue"); Button bDec =new Button("Dec Blue"); Button bReset =new Button("Reset"); Button dRedInc =new Button("Red++"); Button dRedDec =new Button("Red--"); Button dGreenInc=new Button("Green++"); Button dGreenDec=new Button("Green--"); Button dBlueInc =new Button("Blue++"); Button dBlueDec =new Button("Blue--"); Button reset =new Button("RESET ALL"); redBox =new TextField(2); greenBox =new TextField(2); blueBox =new TextField(2); color =new TextField(6); cbg = new CheckboxGroup(); decimal =new Checkbox("Decimal",cbg,true); octal =new Checkbox("Octal",cbg,false); hex =new Checkbox("Hex",cbg,false); /**************************************************************************$*/ /* Store references to buttons. */ /**************************************************************************$*/ setBackground(Color.gray); /*Red group*/ setForeground(Color.red); bList[0] = (Button) add(rInc); bList[1] = (Button) add(rDec); bList[2] = (Button) add(rReset); bList[9] = (Button) add(dRedInc); bList[10] = (Button) add(dRedDec); bList[15] = (Button) add(reset); Color gr=new Color(0,150,0); /*Green group*/ setForeground(gr); bList[3] = (Button) add(gInc); bList[4] = (Button) add(gDec); bList[5] = (Button) add(gReset); bList[11] = (Button) add(dGreenInc); bList[12] = (Button) add(dGreenDec); setForeground(Color.blue); /*Blue group*/ bList[6] = (Button) add(bInc); bList[7] = (Button) add(bDec); bList[8] = (Button) add(bReset); bList[13] = (Button) add(dBlueInc); bList[14] = (Button) add(dBlueDec); setForeground(Color.black); add(redBox); /* Textbox for red.*/ add(greenBox); /* Textbox for green.*/ add(blueBox); /* Textbox for blue.*/ add(color); /*Textbox for color value.*/ add(decimal); /*CheckBox for decimal*/ add(octal); /*CheckBox for octal */ add(hex); /*CheckBox for hex */ /**************************************************************************$*/ /* Place the Buttons, and Textfields. */ /**************************************************************************$*/ bList[0].setBounds(10,300,60,20); /*Red Group*/ bList[1].setBounds(90,300,60,20); bList[2].setBounds(170,300,40,20); bList[9].setBounds(10,330,60,20); bList[10].setBounds(90,330,60,20); redBox.setBounds(179,330,22,20); bList[3].setBounds(230,300,60,20); /*Green Group*/ bList[4].setBounds(310,300,60,20); bList[5].setBounds(390,300,40,20); bList[11].setBounds(230,330,60,20); bList[12].setBounds(310,330,60,20); greenBox.setBounds(399,330,22,20); bList[6].setBounds(450,300,60,20); /*Blue Group*/ bList[7].setBounds(530,300,60,20); bList[8].setBounds(610,300,40,20); bList[13].setBounds(450,330,60,20); bList[14].setBounds(530,330,60,20); blueBox.setBounds(619,330,22,20); bList[15].setBounds(300,380,80,30); /*Master Reset*/ color.setBounds(300,10,80,20); /*Display color Group*/ hex.setBounds(260,10,15,15); octal.setBounds(230,10,15,15); decimal.setBounds(200,10,15,15); for (int i=0; i<16; i++) /*Add buttons. */ bList[i].addActionListener(this); redBox.addActionListener(this); /*Add the Action Listener*/ greenBox.addActionListener(this); /*for the textFields. */ blueBox.addActionListener(this); } public void actionPerformed(ActionEvent ae) /*Main body of the program*/ { if (ae.getSource()==bList[2]) Red=dRed=1; if (ae.getSource()==bList[5]) Green=dGreen=1; if (ae.getSource()==bList[8]) Blue=dBlue=1; if (ae.getSource()==bList[15]) Red=dRed=Green=dGreen=Blue=dBlue=1; /***Have to convert the text in the texfields to integer to use them.********/ if (ae.getSource()==redBox) dRed=Integer.parseInt(redBox.getText()); if (ae.getSource()==greenBox) dGreen=Integer.parseInt(greenBox.getText()); if (ae.getSource()==blueBox) dBlue=Integer.parseInt(blueBox.getText()); str=ae.getActionCommand(); /*Increase or decrease the colors*/ if(str=="Inc Red") /*for the various colors depending*/ Red=Red+dRed; /*on the button that was pressed.*/ if(str=="Dec Red") Red=Red-dRed; if(str=="Inc Green") Green=Green+dGreen; if(str=="Dec Green") Green=Green-dGreen; if(str=="Inc Blue") Blue=Blue+dBlue; if(str=="Dec Blue") Blue=Blue-dBlue; if(Red>255) Red=255; /*Set limits on the colors*/ if(Red<1) Red=1; /*to keep them between 1-255*/ if(Green<1) Green=1; if(Green>255) Green=255; if(Blue<1) Blue=1; if(Blue>255) Blue=255; if (str=="Red++" && dRed<20) dRed++; /*Set limits on the deltas*/ if (str=="Red--"&& dRed>1) dRed--; /*to keep them between 1-20*/ if (str=="Green++" && dRed<20) dGreen++; if (str=="Green--"&& dRed>1) dGreen--; if (str=="Blue++" && dRed<20) dBlue++; if (str=="Blue--"&& dRed>1) dBlue--; redBox.setText(""+dRed); /*Have to set the text*/ greenBox.setText(""+dGreen); /*to use the buttons */ blueBox.setText(""+dBlue); /*and display the delta*/ if (cbg.getSelectedCheckbox()==decimal) /*Display the color value*/ color.setText(Red+","+Green+","+Blue); /*in decimal format. */ if (cbg.getSelectedCheckbox()==hex) /*Display the color value*/ color.setText(Integer.toHexString(Red)+","+ /*in hexa-decimal format.*/ Integer.toHexString(Green)+ ","+ Integer.toHexString(Blue)); if (cbg.getSelectedCheckbox()==octal) /*Display the color value*/ color.setText(Integer.toOctalString(Red)+","+ /*in octal format. */ Integer.toOctalString(Green)+","+ Integer.toOctalString(Blue)); repaint(); } public void paint(Graphics g) { Color cr=new Color(Red,0,0); /*Draw the red rectangle.*/ g.setColor(cr); g.fillRect(50,(270-Red),120,Red); Color cg=new Color(0,Green,0); /*Draw the green rectangle.*/ g.setColor(cg); g.fillRect(270,(270-Green),120,Green); Color cb=new Color(0,0,Blue); /*Draw the blue rectangle.*/ g.setColor(cb); g.fillRect(480,(270-Blue),120,Blue); Color crgb=new Color(Red,Green,Blue); /*Draw the color rectangle.*/ g.setColor(crgb); g.fillRect(10,360,660,440); g.drawString("Hex",265,35); /*Draw the labels for the*/ g.drawString("Octal",230,35); /*CheckBoxes. */ g.drawString("Decimal",180,35); } }