Meico Hsiao
Meico Hsiao

Reputation: 13

Using the Color.brighter method to print red, green, and blue values

I cannot seem to figure out what I am doing wrong. Here is the exercise in the textbook:

"In the Java library, a color is specified by its red, green, and blue components between 0 and 255 (see Table 4 on page 68). Write a program BrighterDemo that con- structs a Color object with red, green, and blue values of 50, 100, and 150. Then apply the brighter method of the Color class and print the red, green, and blue values of the resulting color"

Here's the code I have so far:

import java.awt.Color;
import javax.swing.JFrame;

public class BrighterDemo
{
    public static void main(String[] args)
    {
    JFrame frame = new JFrame();
    frame.setSize(200, 200);
    Color myColor = new Color(50, 100, 150);
    Color brighterRedColor = myColor.red.brighter();
    Color brighterGreenColor = myColor.green.brighter();
    Color brighterBlueColor = myColor.blue.brighter();
    frame.getContentPane().setBackground(myColor);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    }
}

EDIT: I figured it out, here's the correct code:

import java.awt.Color;

public class BrighterDemo
{
    public static void main(String[] args)
    {
    Color myColor = new Color(50, 100, 150);
    Color brighterColor = myColor.brighter();
    System.out.println("Red - ");
    System.out.println(brighterColor.getRed());
    System.out.println("Green - ");
    System.out.println(brighterColor.getGreen());
    System.out.println("Blue - ");
    System.out.println(brighterColor.getBlue());
    }
}

Upvotes: 0

Views: 2361

Answers (1)

Krease
Krease

Reputation: 16225

There's a few things not quite right with your code:

  1. You're creating the colors brighterRedColor, brighterGreenColor, brighterBlueColor, but not doing anything with them (such as printing them out)
  2. The colors you're creating are being based off the static Color.red, Color.green, Color.blue instances, not your myColor object.
  3. Your question is asking you to get the RGB values of your myColor and output them after you apply brighter() to your color. Not to create 3 new colors and brighten them.
  4. Not sure what the JFrame is for. You don't need it based on your problem.

Upvotes: 1

Related Questions