Results 1 to 3 of 3
  1. #1
    chronicle is offline Member
    Join Date
    Nov 2013
    Posts
    1
    Rep Power
    0

    Default Using a variable like this: setColor(variable), or how to move variables

    In this method I make a string variable, and move it to a different one.

    Java Code:
     private void produceRainbow() {
     String color = "RED";
     produceArc(color);
     }
    There is more code there but it does not matter (essentially, changing the string to other colors).

    Next this method:

    Java Code:
     private void produceArc(String color) {
     GOval arc = new GOval(leftX, upperY, rightX, lowerY); //ignore these four variables
     arc.setColor(Color.color);
    Here I want to set the color to a string.
    So I want it to become "arc.setColor(Color.RED)"

    When I compile, I get this error:

    Program.java:89: cannot find symbol
    symbol : variable color
    location: class java.awt.Color
    arc.setColor(Color.color);

    Is it even possible to do what I want to do? If so, what am I doing wrong?

    (If you're curious, I made a seperate method for each arc (red, blue, green, etc, all have their own method) and this works, but I was wondering if I could just use one method that takes a variable, which makes the code a lot shorter)
    Last edited by JosAH; 11-02-2013 at 08:51 AM. Reason: restored original content

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,424
    Rep Power
    5

    Default Re: Using a variable like this: setColor(variable), or how to move variables

    The compiler is telling you that Color.color is not a valid field. I am not familiar with GOval but most setColor methods expect an object of type Color, not a String. So instead of using "RED", why not just use Color.red?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,386
    Blog Entries
    7
    Rep Power
    20

    Default Re: Using a variable like this: setColor(variable), or how to move variables

    @OP: please don't remove your original question text; it leaves this thread completely useless; I restored the previous content of your post. There are other people besides you who are reading this forum.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 2
    Last Post: 01-09-2013, 08:10 PM
  2. Variable Storage and Global Variables in Java
    By lsrinivasamurthy in forum New To Java
    Replies: 3
    Last Post: 05-08-2012, 12:16 PM
  3. Replies: 1
    Last Post: 06-29-2011, 05:46 PM
  4. Java equivalent of PHP's variable-variables?
    By XmisterIS in forum New To Java
    Replies: 7
    Last Post: 09-01-2010, 11:05 AM
  5. building file and variable names from variables
    By madad2005 in forum New To Java
    Replies: 2
    Last Post: 07-18-2007, 04:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •