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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Color Class 1

Status
Not open for further replies.

jgroh9

Technical User
Jun 13, 2003
17
US
All,
I need some help regarding the color class and reading files. I have a text file that lists various colors such as magenta, blue, red, yellow. I can read the file no problem to get the color names. The problem is how do I convert these names to a meaning color to display objects in whatever color I read in from the file? Each color is on a different line in the text file.

So I need something like:
graphics.setColor(Color.fileReadInput);

When I read the color name in from the file it is a String data type.
Any help is greatly appreciated.
Thanks
 
Do you have the standard 16 color names?
Like here:

$JAVA_HOME/docs/api/java/awt/Color.html

You could use a small Map, which maps names to colors, or use introspection, to find the color-constant for a given name.

seeking a job as java-programmer in Berlin:
 
Here is some code, using reflection:
Code:
	public ColorName2Value (String name)
	{
		Color c = getColorForName (name);
		if (c != null)
			System.out.println ("RGB = " + c.getRGB ());
		else System.out.println ("color not found");
	}

	private Field findColorName (String name)
	{
		try
		{
			Class clazz = Class.forName ("java.awt.Color");
			// System.out.println (clazz.toString ());
			Field [] fa = clazz.getFields ();
			for (Field field : fa)
			{
				String found = field.toString ();
				if (found.equals ("public static final java.awt.Color java.awt.Color." + name))
				{
					return field;
				}
			}
			System.out.println ("nix found");
		}
		catch (ClassNotFoundException e)
		{
			System.err.println (e.getMessage ());
		}
		return null;
	}

	private Color getColorForName (String name)
	{
		Color c = null;
		Field f = findColorName (name);
		if (f != null)
		{
			try
			{
				Object o = f.get (f.toString ());
				c = (Color) o;
			}
			catch (IllegalAccessException iae)
			{
				System.err.println (iae.getMessage ());
			}
			catch (ClassCastException cce)
			{
				System.err.println ("class cast exception! " + cce.getMessage ());
			}
		}
		return c;
	}

seeking a job as java-programmer in Berlin:
 
stefanwagner,
Thanks for the replies. That was exactly what I was looking for. It seems so simple now that you have shown how it is done.
Thanks again
 
But I don't like the solution, because of the reflection.

A Hashmap with hardcoded values (which we can produce with the help of the above method) would be much better to understand.

Using Java1.5 we could use enums much more elegant:
Code:
	public enum NColor
	{
		BLACK	(-16777216),
		BLUE 	(-16776961),
		YELLOW 	(-256),
		RED 	(-65536);
		// and so on...

		private final int rgb;

		NColor (int rgb)
		{
			this.rgb = rgb;
		}

		public int getRGB () { return rgb; }
	}

	/** */
	public ColorEnum ()
	{
		for (NColor nc : NColor.values ())
		{
			System.out.println (nc.toString () + "\t" + nc.getRGB ());
		}
	}

seeking a job as java-programmer in Berlin:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top