Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help with colors for beginner 1

Status
Not open for further replies.

heather473

Technical User
Dec 2, 2000
8
0
0
US
I am trying to make a simple rainbow but I am having trouble geting each arch a different color. I am having a problem of repeating colors. Can someone please help me with a way to have many colors in my rainbow.

Thank you.
 
Heather,

Look at the Color class. It is made of up 3 Int arguments representing RED, BLUE, GREEN. Values from 0 to 255.

eg. Color curColor = new Color(255, 0, 0); // Red
or use static Color.red;

set your current color and then draw an arc. Then change the color and draw another arc but just move it a few pixels over. Keep doing this until you have your rainbow drawn. setColor(curColor); // sets the cur color to Red.

You can also look at the Graphics 2D classes, which may give you a better looking rainbow.

Good luck, Brian
 
This is my code I have gotten so far,,, am I way off in making a rainbow, I guess I should say.
Thanks Heather



import java.applet.Applet;
import java.awt.*;

public class Rainbow extends Applet
{
private final int MAX_WIDTH = 300;
private final int NUM_ARCS = 7;
private final int ARC_WIDTH = 25;

//-----------------------------------------------------------------
// Paints a Rainbow.
//-----------------------------------------------------------------
public void paint (Graphics page)
{
int x = 180, y = 0, diameter;

setBackground (Color.cyan);

diameter = MAX_WIDTH;
page.setColor (Color.white);

for (int count = 0; count < NUM_ARCS; count++)
{
if (page.getColor() == Color.red) // alternate colors
page.setColor (Color.blue);
else
page.setColor (Color.red);
if (page.getColor() == Color.blue)
page.setColor (Color.orange);


page.fillArc (x, y, diameter, diameter);

diameter -= (2 * ARC_WIDTH);
x += ARC_WIDTH;
y += ARC_WIDTH;
}
 
You have a few things wrong with this code.

1. Your if statements may not be nested like you want them. Try using { braces } around each section of code that goes with an if or else. Always indent for readability.

2. look at the fillArc function in your books or the API documentation. It does not take 4 arguments. It takes 6.

3. Make sure you use } braces on all your methods and the class itself.

This program does not compile because of some of these problems. You need to fix them and then you will be closer to creating your rainbow.

I assume you are a student taking this for a class. This is something you must learn to do, debug your programs, and look up the API method calls. Many instructors will not even look at compile error messages, so I would suggest you start Hacking programs and learning what works in the language and what does not. I am sure your instructor is teaching you the theories and the basics of the language, but you need to work with the code a lot to start understanding it. &quot;In other words &quot;No pain, No gain&quot;. Have patients and continue to practice writing little programs.

Hope this helps, I'll leave the rest to you and your instructor.

Brian Zimmer
(Sun Certified Java Programmer & Instructor)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top