Original Post
I''ve created a simple color creator applet where you use three sliders to generate different values for red, green, and blue. Using those colors, a single color is formed, and is used to color a circle on the screen. The applet also displays the red, green, and blue values. I would like to get some opinions on this, about whether it is well coded or not. Also, is there a way to convert the red, green, and blue values tot he HEX equivalent? I''d like to add that if I can.
//Source below.
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class ColorPicker extends Applet implements AdjustmentListener {
private Scrollbar red;
private Scrollbar green;
private Scrollbar blue;
private int redValue = 0;
private int greenValue = 0;
private int blueValue = 0;
private static final int diameter = 200;
private static final int circleX = 120;
private static final int circleY = 60;
private static final int boxX = 20;
private static final int boxY = 20;
private static final int boxWidth = 40;
private static final int boxHeight = 20;
public void init() {
red = new Scrollbar(Scrollbar.VERTICAL, 1, 0, 0, 256);
add(red);
red.addAdjustmentListener(this);
green = new Scrollbar(Scrollbar.VERTICAL, 1, 0, 0, 256);
add(green);
green.addAdjustmentListener(this);
blue = new Scrollbar(Scrollbar.VERTICAL, 1, 0, 0, 256);
add(blue);
blue.addAdjustmentListener(this);
}
public void paint(Graphics g) {
drawBoxesLabels(g);
drawSampleCircle(g);
}
public void drawBoxesLabels(Graphics g) {
int labelOffset = boxHeight * 3 / 4;
g.setColor(new Color(redValue, 0, 0));
g.fillRect(boxX, boxY, boxWidth, boxHeight);
g.setColor(new Color(0, greenValue, 0));
g.fillRect(boxX, boxY + 21, boxWidth, boxHeight);
g.setColor(new Color(0, 0, blueValue));
g.fillRect(boxX, boxY + 42, boxWidth, boxHeight);
g.setColor(Color.black);
g.drawString("Red value: " + redValue, boxX + boxWidth + 2, boxY + labelOffset);
g.drawString("Green value: " + greenValue, boxX + boxWidth + 2, boxY + labelOffset + 21);
g.drawString("Blue value: " + blueValue, boxX + boxWidth + 2, boxY + labelOffset + 42);
}
public void drawSampleCircle(Graphics g) {
Color color = new Color(redValue, greenValue, blueValue);
g.setColor(color);
g.fillOval(circleX, circleY, diameter, diameter);
}
public void adjustmentValueChanged(AdjustmentEvent e) {
redValue = red.getValue();
greenValue = green.getValue();
blueValue = blue.getValue();
repaint();
}
}
//Source ends here.
By the way, how can you insert source code into one of those code boxes? I couldn''t figure out how.
Grant Palin