在java中制作新颜色时如何使用变量
我在一个编程类中,我们正在制作一个类,从用户那里获取用于制作椭圆(长轴、短轴和硬零件颜色)的输入。我想做的是让用户输入rbg值,并制作一个自定义颜色来填充椭圆。在main类中,我通过JOptionPane窗口完成所有输入,并将其解析为双值,然后在JFrame中显示椭圆在java中制作新颜色时如何使用变量,java,Java,我在一个编程类中,我们正在制作一个类,从用户那里获取用于制作椭圆(长轴、短轴和硬零件颜色)的输入。我想做的是让用户输入rbg值,并制作一个自定义颜色来填充椭圆。在main类中,我通过JOptionPane窗口完成所有输入,并将其解析为双值,然后在JFrame中显示椭圆 String input = JOptionPane.showInputDialog("Enter the major Axis of your Ellipse: "); int majAxis = Integer
String input = JOptionPane.showInputDialog("Enter the major Axis of your Ellipse: ");
int majAxis = Integer.parseInt(input);
input = JOptionPane.showInputDialog("Enter the Minor Axis of your Ellipse:");
int minAxis = Integer.parseInt(input);
input = JOptionPane.showInputDialog("Enter the red value in the RBG of your color:");
double red = Double.parseDouble(input);
input = JOptionPane.showInputDialog("Enter the blue value in the RBG of your color:");
double blue = Double.parseDouble(input);
input = JOptionPane.showInputDialog("Enter the green value in the RBG of your color:");
double green = Double.parseDouble(input);
然后我让它通过构造函数传递给另一个类:
Ellipse component = new Ellipse(majAxis, minAxis, red, blue, green);
然后在另一个类中,我将数据从构造函数传输到实例变量,然后再传输到新的颜色构造函数
public Ellipse(int maj, int min, double red1, double blue1, double green1)
{
major = maj;
minor = min;
red = red1;
blue = blue1;
green = green1;
}
public void paintComponent(Graphics g)
{
//sets up access to graphics
Graphics2D g2 = (Graphics2D)g;
Color custom = new Color(red, blue, green); //this is where i get an error saying the variable is undefined.
Ellipse2D.Double e = new Ellipse2D.Double((this.getWidth()-major) / 2,(this.getHeight()-minor) / 2,major,minor);
g2.setColor(Color.BLACK);
g2.draw(e);
}
private int major;
private int minor;
private double red;
private double blue;
private double green;
我需要能够使用变量,但我不知道为什么它不起作用。因此,我可以得到一些关于如何做到这一点的建议的帮助。我不想使用if语句和预设颜色,所以这是我唯一的选择。首先,你应该通过
Color custom = new Color(red, green, blue); // in this order: R G B
其次,您的变量红色
、绿色
和蓝色
实际上没有定义。在调用新颜色(r、g、b)
之前,应该为它们指定一个值
第三,类Color的构造函数接受类型为
int
(0..255)或float
(0..1)的参数。因此,也许你应该用一个浮动的红、绿、蓝来替换双倍的红、绿、蓝,这应该是可行的,我不知道为什么它不是椭圆类的paintComponent方法的一部分?我们能看到整个椭圆类吗