[英]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: 您的代码有些不正确的地方:
brighterRedColor, brighterGreenColor, brighterBlueColor
, but not doing anything with them (such as printing them out) brighterRedColor, brighterGreenColor, brighterBlueColor
,但不对其进行任何处理(例如将它们打印出来) Color.red, Color.green, Color.blue
instances, not your myColor
object. Color.red, Color.green, Color.blue
实例,而不是myColor
对象。 myColor
and output them after you apply brighter()
to your color. myColor
的RGB值并在对颜色应用myColor
brighter()
之后输出它们。 Not to create 3 new colors and brighten them.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.