DiggityDug
DiggityDug

Reputation: 3

How do I use variables while making a new color in java

I am in a programming class and we are making a class to get input from the user for making an ellipse (the major axis the minor axis and the hard part color). What I want to do is have the user enter in the rbg values and make a custom color to fill the ellipse. In the main class I have all the input done through a JOptionPane window and parsed into double values and I am displaying the ellipse in a JFrame.

    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);

Then i have it passed through a constructor to the other class:

    Ellipse component = new Ellipse(majAxis, minAxis, red, blue, green);

then in the other class I have the data transferred from a constructor to instance variable then into the new color constructor.

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;

I need to be able to use the variables and i don't know why it isn't working. So can please get some help of suggestions on how to do this. i don't want to use if statements and preset colors so this is my only option.

Upvotes: 0

Views: 1027

Answers (1)

naXa stands with Ukraine
naXa stands with Ukraine

Reputation: 37916

First, you should pass

Color custom = new Color(red, green, blue);  // in this order: R G B

Second, your variables red, green and blue actually are not defined. You should assign them a value BEFORE you call the new Color(r, g, b).

Third, the constructor of class Color accepts parameters of types int (0..255) or float (0..1). So may be you should replace double red, green, blue with a float red, green, blue.

Upvotes: 2

Related Questions