APCS Assignment #7: Introduction to Java's Color Class Last week

advertisement

APCS

Assignment #7: Introduction to Java’s Color Class

Last week we explored TurtleGraphics and JFrames. As part of that exploration you were introduce to the Color Class in

Java. In this assignment we are going to learn how to create our own unique colors using the Color Class and experimenting with making Color objects. So, here is what I want you to do:

GOOD: P2.7

In the Java library, a color is specified by its red, green, and blue components between 0 and 255. You may wish to see

Table 4 on page 61 to study how the various amounts of R, G, and B combine to make different Color objects.

Write a program BrighterDemo that constructs a Color object with red, green, and blue values of 50, 100, and 150

(you can also experiment with your own values). Then, apply the brighter method and print the red, green, and blue values of the resulting color.

You can instantiate a Color object in a line of code in Java by writing a line of code similar to this:

Color myColor = new Color(50, 100, 150);

To apply the brighter method, you would do this: myColor.brighter();

To print out the actual values, you would want to use the following for r, g, and b: int red = myColor.getRed();

System.out.println(“Red: “ + red);

//repeat now for blue and for green…

(Note that you won’t actually see the color doing this; just the numbers. If you want to see the color, try the BETTER version.)

BETTER: P2.8

Do exactly what you did in GOOD, but place your code in the following class. Then the color will be displayed.

Experiment with this by changing your Color at least three different times so that you can make different-colored windows.

3.)

2.)

BEST:

Do exactly what was done in GOOD and BETTER, but come up with a way to get user input so that the user can choose the red, green, and blue values to make a color of their own desire. This color would then be displayed as described in

BETTER.

A few reminders:

1.) Remember that the Java API is available to you to explore. Google “Java 7 API Color” and go to the first selection that comes up. This will give you some more in-depth look at the other things you can do with this class.

Remember that if you do the Good version, you need to include text output into the source code. If you do the Better or Best version, you’ll need to include screen shots of the resulting windows with their respective

Colors. Obviously that would be better done on a color printer at home, if at all possible.

Remember as always to include a header with your name, assignment number (number 7 in this case), and a brief description of what your program does.

Download