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

    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";
    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
    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: cannot find symbol
    symbol : variable color
    location: class java.awt.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
    Northern Virginia, United States
    Rep Power

    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

    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Voorschoten, the Netherlands
    Blog Entries
    Rep Power

    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,

    Build a wall around Donald Trump; I'll pay for it.

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