Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

gpalin

[java] Opinions on program

This topic is 5648 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
Looks good to me.
As far as I know there is a way to convert an integer value to hex(In the Integer calss I believe, I also could of sworn I saw a way to do this in the Color class. But you might need mask the alpha value out, depending on which method you use. I will unpack my stuff tonight and find my book to let you konw for sure.

"Technological progress is like an ax in the hands of a pathological criminal."

Share this post


Link to post
Share on other sites
Any other opinions on my program?

Wrathnut, have you found that Hex conversion stuff yet?

Grant Palin

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!