简体   繁体   English

使用Color.brighter方法打印红色,绿色和蓝色值

[英]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" “在Java库中,颜色由0、255之间的红色,绿色和蓝色分量指定(请参阅第68页的表4)。编写程序BrighterDemo,该程序用红色,绿色和蓝色构造Color对象。值分别为50、100和150。然后应用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());
    }
}

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) 您正在创建颜色brighterRedColor, brighterGreenColor, brighterBlueColor ,但不对其进行任何处理(例如将它们打印出来)
  2. The colors you're creating are being based off the static Color.red, Color.green, Color.blue instances, not your myColor object. 您创建的颜色基于静态Color.red, Color.green, Color.blue实例,而不是myColor对象。
  3. Your question is asking you to get the RGB values of your myColor and output them after you apply brighter() to your color. 您的问题是要您获取myColor的RGB值并在对颜色应用myColor brighter()之后输出它们。 Not to create 3 new colors and brighten them. 不要创建3种新颜色并使其变亮。
  4. Not sure what the JFrame is for. 不知道JFrame是干什么用的。 You don't need it based on your problem. 根据您的问题,您不需要它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM