ShowColors
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.lang.*;
/**
**********************************************
*
* Show Colors Applet
*
* T-Bone Connection
* P.O. Box 370962
* Milwaukee, WI, 53237-9914
* http://www.tboneconnection.com
*
* -------------------------------------------
*
* This applet is an R&D tool to quickly build
* and view a color from RGB. It allows you to
* type in red, green or blue values. You can
* hit buttons to add to or subtract from any
* color singly or all of them at the same time.
* Several Preset colors can be chosen as well.
* Preset colors and explicitly typed colors
* respond to the go button. Overall applet
* size on the screen is determined through
* predefined HTML tags.
*
*********************************************/
public class ShowColors extends Applet {
/** Global Declarations Live Here **/
Dimension size; // The size of the applet
Panel display; // This is where the color graphic is
Color back = Color.lightGray; // background color
Color fore = Color.black; // foreground color
Color newcolor;
Choice colorchoice; // Choices or preset colors
TextField r; // Red
TextField g; // Green
TextField b; // Blue
TextField hr; // Red
TextField hg; // Green
TextField hb; // Blue
Graphics solid; // The graphic
Button go; // The go switch ... changes to font
Button darker; // Make color darker
Button lighter; // Make color lighter
Button mred; // Make color more red
Button lred; // Make color less red
Button mgreen; // Make color more green
Button lgreen; // Make color less green
Button mblue; // Make color more blue
Button lblue; // Make color less blue
String hexr; // HTML takes colors in hexidecimal
String hexg; // Green
String hexb; // Blue
/******************************/
/** initialize applet **/
public void init(){
// Get size of applet as defined in html tags
size=this.size();
// Set up Colors
if (back != null) this.setBackground(back);
if (fore != null) this.setForeground(fore);
// Font for buttons and numbers
Font font = new Font("Helvetica",1,14);
this.setFont(font);
// Applet layout manager
this.setLayout(new BorderLayout(10,10));
// Create Display Panel
display = new Panel();
display.setLayout(new BorderLayout(10,10));
this.add("Center",display);
solid = display.getGraphics();
newcolor = new Color(192,192,192);
paint(solid);
// Create Button Panel
Panel buttonbox = new Panel();
this.add("South",buttonbox);
darker = new Button("DARKER");
buttonbox.add(darker);
go = new Button("GO");
buttonbox.add(go);
lighter = new Button("LIGHTER");
buttonbox.add(lighter);
colorchoice = new Choice();
colorchoice.addItem("none");
colorchoice.addItem("black");
colorchoice.addItem("blue");
colorchoice.addItem("cyan");
colorchoice.addItem("darkGray");
colorchoice.addItem("gray");
colorchoice.addItem("green");
colorchoice.addItem("lightGray");
colorchoice.addItem("magenta");
colorchoice.addItem("orange");
colorchoice.addItem("pink");
colorchoice.addItem("red");
colorchoice.addItem("white");
colorchoice.addItem("yellow");
colorchoice.select("none");
buttonbox.add(colorchoice);
// Create RGB Panel
Panel rgb = new Panel();
rgb.setLayout(new GridLayout(4,3,10,10));
this.add("North",rgb);
// Increase Color values
mred = new Button("+ RED");
rgb.add(mred);
mgreen = new Button("+ GREEN");
rgb.add(mgreen);
mblue = new Button("+ BLUE");
rgb.add(mblue);
// Decimal Values
Color startcolor = solid.getColor();
r = new TextField(20);
r.setEditable(true);
r.setText(Integer.toString(startcolor.getRed()));
rgb.add(r);
g = new TextField(20);
g.setEditable(true);
g.setText(Integer.toString(startcolor.getGreen()));
rgb.add(g);
b = new TextField(20);
b.setEditable(true);
b.setText(Integer.toString(startcolor.getBlue()));
rgb.add(b);
// Hexidecimal Values
hr = new TextField(20);
hr.setEditable(false);
hr.setText(Integer.toHexString(startcolor.getRed()));
rgb.add(hr);
hg = new TextField(20);
hg.setEditable(false);
hg.setText(Integer.toHexString(startcolor.getGreen()));
rgb.add(hg);
hb = new TextField(20);
hb.setEditable(false);
hb.setText(Integer.toHexString(startcolor.getBlue()));
rgb.add(hb);
// Decrease Color values
lred = new Button("- RED");
rgb.add(lred);
lgreen = new Button("- GREEN");
rgb.add(lgreen);
lblue = new Button("- BLUE");
rgb.add(lblue);
}
// Respond to actions
public boolean action(Event e, Object arg) {
if (e.target == go) {
/** Display new color **/
try{
String colorstring = colorchoice.getSelectedItem();
if (colorstring.equals("none")) {
int ir = Integer.parseInt(r.getText());
int ig = Integer.parseInt(g.getText());
int ib = Integer.parseInt(b.getText());
hr.setText(Integer.toHexString(ir));
hg.setText(Integer.toHexString(ig));
hb.setText(Integer.toHexString(ib));
newcolor = new Color (ir,ig,ib);
paint(solid);
return true;
} else {
if (colorstring.equals("black"))
{newcolor = Color.black;}
else if (colorstring.equals("blue"))
{newcolor = Color.blue;}
else if (colorstring.equals("cyan"))
{newcolor = Color.cyan;}
else if (colorstring.equals("darkGray"))
{newcolor = Color.darkGray;}
else if (colorstring.equals("gray"))
{newcolor = Color.gray;}
else if (colorstring.equals("green"))
{newcolor = Color.green;}
else if (colorstring.equals("lightGray"))
{newcolor = Color.lightGray;}
else if (colorstring.equals("magenta"))
{newcolor = Color.magenta;}
else if (colorstring.equals("orange"))
{newcolor = Color.orange;}
else if (colorstring.equals("pink"))
{newcolor = Color.pink;}
else if (colorstring.equals("red"))
{newcolor = Color.red;}
else if (colorstring.equals("white"))
{newcolor = Color.white;}
else if (colorstring.equals("yellow"))
{newcolor = Color.yellow;}
r.setText(Integer.toString(newcolor.getRed()));
g.setText(Integer.toString(newcolor.getGreen()));
b.setText(Integer.toString(newcolor.getBlue()));
hr.setText(Integer.toHexString(newcolor.getRed()));
hg.setText(Integer.toHexString(newcolor.getGreen()));
hb.setText(Integer.toHexString(newcolor.getBlue()));
paint (solid);
return true;
}
} catch (Exception ee) {return false;}
} else if (e.target == darker) {
/** darken color and display **/
try {
colorchoice.select("none");
int ir = Integer.parseInt(r.getText());
int ig = Integer.parseInt(g.getText());
int ib = Integer.parseInt(b.getText());
hr.setText(Integer.toHexString(ir));
hg.setText(Integer.toHexString(ig));
hb.setText(Integer.toHexString(ib));
if (ir-5 >= 0) {ir-=5;}
else {ir = 0;}
if (ig-5 >= 0) {ig-=5;}
else {ig = 0;}
if (ib-5 >= 0) {ib-=5;}
else {ib = 0;}
newcolor = new Color (ir,ig,ib);
paint (solid);
r.setText(Integer.toString(newcolor.getRed()));
g.setText(Integer.toString(newcolor.getGreen()));
b.setText(Integer.toString(newcolor.getBlue()));
hr.setText(Integer.toHexString(newcolor.getRed()));
hg.setText(Integer.toHexString(newcolor.getGreen()));
hb.setText(Integer.toHexString(newcolor.getBlue()));
return true;
} catch (Exception ee) {return false;}
} else if (e.target == lred) {
/** less red display **/
try {
colorchoice.select("none");
int ir = Integer.parseInt(r.getText());
int ig = Integer.parseInt(g.getText());
int ib = Integer.parseInt(b.getText());
hr.setText(Integer.toHexString(ir));
if (ir-5 >= 0) {ir-=5;}
else {ir = 0;}
newcolor = new Color (ir,ig,ib);
paint (solid);
r.setText(Integer.toString(newcolor.getRed()));
hr.setText(Integer.toHexString(newcolor.getRed()));
return true;
} catch (Exception ee) {return false;}
} else if (e.target == mred) {
/** more red and display **/
try {
colorchoice.select("none");
int ir = Integer.parseInt(r.getText());
int ig = Integer.parseInt(g.getText());
int ib = Integer.parseInt(b.getText());
hr.setText(Integer.toHexString(ir));
if (ir+5 <= 255) {ir+=5;}
else {ir = 255;}
newcolor = new Color (ir,ig,ib);
paint (solid);
r.setText(Integer.toString(newcolor.getRed()));
hr.setText(Integer.toHexString(newcolor.getRed()));
return true;
} catch (Exception ee) {return false;}
} else if (e.target == lgreen) {
/** less green display **/
try {
colorchoice.select("none");
int ir = Integer.parseInt(r.getText());
int ig = Integer.parseInt(g.getText());
int ib = Integer.parseInt(b.getText());
hg.setText(Integer.toHexString(ig));
if (ig-5 >= 0) {ig-=5;}
else {ig = 0;}
newcolor = new Color (ir,ig,ib);
paint (solid);
g.setText(Integer.toString(newcolor.getGreen()));
hg.setText(Integer.toHexString(newcolor.getGreen()));
return true;
} catch (Exception ee) {return false;}
} else if (e.target == mgreen) {
/** more green and display **/
try {
colorchoice.select("none");
int ir = Integer.parseInt(r.getText());
int ig = Integer.parseInt(g.getText());
int ib = Integer.parseInt(b.getText());
hg.setText(Integer.toHexString(ig));
if (ig+5 <= 255) {ig+=5;}
else {ig = 255;}
newcolor = new Color (ir,ig,ib);
paint (solid);
g.setText(Integer.toString(newcolor.getGreen()));
hg.setText(Integer.toHexString(newcolor.getGreen()));
return true;
} catch (Exception ee) {return false;}
} else if (e.target == lblue) {
/** less blue display **/
try {
colorchoice.select("none");
int ir = Integer.parseInt(r.getText());
int ig = Integer.parseInt(g.getText());
int ib = Integer.parseInt(b.getText());
hb.setText(Integer.toHexString(ib));
if (ib-5 >= 0) {ib-=5;}
else {ib = 0;}
newcolor = new Color (ir,ig,ib);
paint (solid);
b.setText(Integer.toString(newcolor.getBlue()));
hb.setText(Integer.toHexString(newcolor.getBlue()));
return true;
} catch (Exception ee) {return false;}
} else if (e.target == mblue) {
/** more blue and display **/
try {
colorchoice.select("none");
int ir = Integer.parseInt(r.getText());
int ig = Integer.parseInt(g.getText());
int ib = Integer.parseInt(b.getText());
hb.setText(Integer.toHexString(ib));
if (ib+5 <= 255) {ib+=5;}
else {ib = 255;}
newcolor = new Color (ir,ig,ib);
paint (solid);
b.setText(Integer.toString(newcolor.getBlue()));
hb.setText(Integer.toHexString(newcolor.getBlue()));
return true;
} catch (Exception ee) {return false;}
} else if (e.target == lighter) {
/** lighten color and display **/
try {
colorchoice.select("none");
int ir = Integer.parseInt(r.getText());
int ig = Integer.parseInt(g.getText());
int ib = Integer.parseInt(b.getText());
hr.setText(Integer.toHexString(ir));
hg.setText(Integer.toHexString(ig));
hb.setText(Integer.toHexString(ib));
if (ir+5 <= 255) {ir+=5;}
else {ir = 255;}
if (ig+5 <= 255) {ig+=5;}
else {ig = 255;}
if (ib+5 <= 255) {ib+=5;}
else {ib = 255;}
newcolor = new Color (ir,ig,ib);
paint (solid);
r.setText(Integer.toString(newcolor.getRed()));
g.setText(Integer.toString(newcolor.getGreen()));
b.setText(Integer.toString(newcolor.getBlue()));
hr.setText(Integer.toHexString(newcolor.getRed()));
hg.setText(Integer.toHexString(newcolor.getGreen()));
hb.setText(Integer.toHexString(newcolor.getBlue()));
return true;
} catch (Exception ee) {return false;}
} else return false;
}
public void paint (Graphics g) {
g.setColor(newcolor);
g.fillRect(0,0,1000,1000);
}
}
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.