Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Colors in Java The basic scheme for representing a picture in a computer is to break the picture down into small elements called pixels and

Colors in Java The basic scheme for representing a picture in a computer is to break the picture down into small elements called pixels and then represent the color of each pixel by a numeric code (this idea is discussed in section 1.6 of the text). In most computer languages, including Java, the color is specified by three numbersone representing the amount of red in the color, another the amount of green, and the third the amount of blue. These numbers are referred to as the RGB value of the color. In Java, each of the three primary colors is represented by an 8-bit code. Hence, the possible base 10 values for each have a range of 0-255. Zero means none of that color while 255 means the maximum amount of the color. Pure red is represented by 255 for red, 0 for green, and 0 for blue, while magenta is a mix of red and blue (255 for red, 0 for green, and 255 for blue). In Java you can create your own colors. So far in the graphics programs we have written we have used the pre-defined colors, such as Color.red, from the Color class. However, we may also create our own Color object and use it in a graphics program. One way to create a Color object is to declare a variable of type Color and instantiate it using the constructor that requires three integer parametersthe first representing the amount of red, the second the amount of green, and the third the amount of blue in the color. For example, the following declares the Color object myColor and instantiates it to a color with code 255 for red, 0 for green, and 255 for blue.

Color myColor = new Color(255, 0, 255);

The statementpage.setColor(myColor) then will set the foreground color for the page to be the color defined by the myColor object. The file Colors.java contains an applet that defines myColor to be a Color object with color code (200, 100, 255) - a shade of purple. Save the program and its associated HTML file Colors.html to your directory, compile and run it using the appletviewer. Now make the following modifications:

1. Change the constructor so the color code is (0,0,0) absence of color. What color should this be? Run the program to check.

2. Try a few other combinations of color codes to see what you get. The description of the Color class in Appendix M contains documentation that gives the codes for the pre-defined colors.

3. Notice in the Color class in Appendix M there is a constructor that takes a single integer as an argument. The first 8 bits of this integer are ignored while the last 24 bits define the color8 bits for red, 8 for green, and the last 8 bits for blue. Hence, the bit pattern

00000000000000001111111100000000

should represent pure green. Its base 10 value is 65280. Change the declaration of the myColor object to Color myColor = new Color (65280); Compile and run the program. Do you see green?

4. The Color class has methods that return the individual color codes (for red, green, and blue) for a Color object. For example,

redCode = myColor.getRed(); r

eturns the code for the red component of the myColor object (redCode should be a variable of type int). The methods that return the green and blue components are getGreen and getBlue, respectively. Add statements to the program, similar to the above, to get the three color codes for myColor (you need to declare some variables). Then add statements such as

page.drawString("Red: " + redCode, ____ , ____ );

to label the rectangle with the three color codes (fill in the blanks with appropriate coordinates so each string is drawn inside the rectangleyou also need to set the drawing color to something such as black so the strings will show up). Compile and run the program; for the pure green color above, you should get 0 for red and blue and 255 for green. Now change the integer you use to create the color from 65280 to 115 and see what you get (can you predict?), then try it again with 2486921 (harder to predict!).

// *************************************************************

// Colors.java //

// Draw rectangles to illustrate colors and their codes in Java

//*************************************************************

import javax.swing.JApplet;

import java.awt.*;

public class Colors extends JApplet

{

public void paint (Graphics page)

{

// Declare size constants

final int PAGE_WIDTH = 600;

final int PAGE_HEIGHT = 400;

// Declare variables int x, y;

// x and y coordinates of upper left-corner of each shape int width, height; // width and height of each shape

Color myColor = new Color (200, 100, 255);

// Set the background color and paint the screen with a white rectangle

setBackground (Color.white);

page.setColor(Color.white);

page.fillRect(0, 0, PAGE_WIDTH, PAGE_HEIGHT);

// Set the color for the rectangle

page.setColor (myColor);

// Assign the corner point and width and height then draw

x = 200;

y = 125;

width = 200;

height = 150;

page.fillRect(x, y, width, height);

}

}

Colors.html

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems Design Implementation And Management

Authors: Carlos Coronel, Steven Morris

14th Edition

978-0357673034

More Books

Students also viewed these Databases questions