Java Problem Color not being seen

New to java Simple code example for kids

The Color c is not being passed into the new class DrawingLine D1 it sees x1,x2,y1,y2 as it auto populates

any help welcome


public static void main(String[] args)
      {
            Scanner keyboard = new Scanner(System.in);
            DrawingWindow dw = new DrawingWindow();
            
            do
            {
                  System.out.println("---Menu----");
                  System.out.println("1. Add Line");
                  System.out.println("2. Add Rectangle");
                  System.out.println("3. Add Oval");
                  System.out.println("4. Clear");
                  System.out.println("0. Exit");
                  System.out.println("Enter selection: ");
                  int selection= keyboard.nextInt();
                  if(selection ==1)
                  {
                        // TODO: get x1 from the user
                        System.out.println("Enter the X1 for your Line");
                        int x1 = keyboard.nextInt();
                        // TODO: get y1 from the user
                        System.out.println("Enter the Y1 for your Line");
                        int y1 = keyboard.nextInt();
                        // TODO: get x2 from the user
                        System.out.println("Enter the X2 for your Line");
                        int x2 = keyboard.nextInt();
                        // TODO: get y2 from the user
                        System.out.println("Enter the y2 for your Line");
                        int y2 = keyboard.nextInt();
                        
                        // TODO: use a menu to have the user enter 1 for red,
                        //2 for blue, 3 for green or 4 for a random color
                        System.out.println("---Color Menu----");
                        System.out.println("1. Red");
                        System.out.println("2. Blue");
                        System.out.println("3. Green");
                        System.out.println("4. Random Color");
                        System.out.println("0. Exit");
                        System.out.println("Enter selection: ");
                        int colorselection = keyboard.nextInt();
                        
                              // TODO: write ifs to set the value for the color and store it into a variable
                              if (colorselection == 1)
                                    {
                                          Color c = Color.RED;
                                                      
                                    }
                              else if (colorselection == 2)
                                    {
                                          Color c = Color.BLUE;      
                                    }
                              else if (colorselection == 3)
                                    {
                                          Color c = Color.GREEN;            
                                    }
                              else if (colorselection == 3)
                                    {
                                          Color c = new Color ( (int)(Math.random()*256),(int)(Math.random()*256),(int)(Math.random()*256));      
                                    }
                              else
                              {
                                          System.exit(0);      
                              }
                              
                              // TODO: create a new DrawingLine with the information gathered in this if
                  
                              DrawingLine D1 = new DrawingLine (x1,x2,y1,y2,c);
USA Java NewbieAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
Please post the actual code. That wouldn't compile
krakatoaCommented:
'c' in the constructor call, doesnt look to be in scope to me at first glance.
USA Java NewbieAuthor Commented:
import java.util.Scanner;
import java.awt.Color;

public class MainFile
{
      public static void main(String[] args)
      {
            Scanner keyboard = new Scanner(System.in);
            DrawingWindow dw = new DrawingWindow();
            
            do
            {
                  System.out.println("---Menu----");
                  System.out.println("1. Add Line");
                  System.out.println("2. Add Rectangle");
                  System.out.println("3. Add Oval");
                  System.out.println("4. Clear");
                  System.out.println("0. Exit");
                  System.out.println("Enter selection: ");
                  int selection= keyboard.nextInt();
                  if(selection ==1)
                  {
                  
                        System.out.println("Enter the X1 for your Line");
                        int x1 = keyboard.nextInt();
                        System.out.println("Enter the Y1 for your Line");
                        int y1 = keyboard.nextInt();
                        System.out.println("Enter the X2 for your Line");
                        int x2 = keyboard.nextInt();
                        System.out.println("Enter the y2 for your Line");
                        int y2 = keyboard.nextInt();
                        
                         
                        
                        System.out.println("---Color Menu----");
                        System.out.println("1. Red");
                        System.out.println("2. Blue");
                        System.out.println("3. Green");
                        System.out.println("4. Random Color");
                        System.out.println("0. Exit");
                        System.out.println("Enter selection: ");
                        int colorselection = keyboard.nextInt();
                        
                              
                              if (colorselection == 1)
                                    {
                                          Color c = Color.RED;
                                                      
                                    }
                              else if (colorselection == 2)
                                    {
                                          Color c = Color.BLUE;      
                                    }
                              else if (colorselection == 3)
                                    {
                                          Color c = Color.GREEN;            
                                    }
                              else if (colorselection == 3)
                                    {
                                          Color c = new Color ( (int)(Math.random()*256),(int)(Math.random()*256),(int)(Math.random()*256));      
                                    }
                              else
                              {
                                          System.exit(0);      
                              }
                              
      
                  
                              DrawingLine D1 = new DrawingLine (x1,x2,y1,y2,c);
                              
                  
                              
                              dw.addShape(D1);
                  }
                  else if(selection ==2)
                  {

                        

                        

                        System.exit(0);
                  }
                  else if(selection ==3)
                  {

                        
                        System.exit(0);
                  }
                  else if(selection ==4)
                  {

                        System.exit(0);
                  }
                  else if(selection == 0)
                  {
                        System.exit(0);
                  }
            }while(true);
      }
}
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

krakatoaCommented:
Try this :
 
import java.awt.*;
import java.util.*;

class DrawingWindow{


public static void main(String[] args)
      {
            Scanner keyboard = new Scanner(System.in);
            DrawingWindow dw = new DrawingWindow();
			Color c=null;
            
            do
            {
                  System.out.println("---Menu----");
                  System.out.println("1. Add Line");
                  System.out.println("2. Add Rectangle");
                  System.out.println("3. Add Oval");
                  System.out.println("4. Clear");
                  System.out.println("0. Exit");
                  System.out.println("Enter selection: ");
                  int selection= keyboard.nextInt();
                  if(selection ==1)
                  {
                        // TODO: get x1 from the user
                        System.out.println("Enter the X1 for your Line");
                        int x1 = keyboard.nextInt();
                        // TODO: get y1 from the user
                        System.out.println("Enter the Y1 for your Line");
                        int y1 = keyboard.nextInt();
                        // TODO: get x2 from the user
                        System.out.println("Enter the X2 for your Line");
                        int x2 = keyboard.nextInt();
                        // TODO: get y2 from the user
                        System.out.println("Enter the y2 for your Line");
                        int y2 = keyboard.nextInt();
                        
                        // TODO: use a menu to have the user enter 1 for red, 
                        //2 for blue, 3 for green or 4 for a random color
                        System.out.println("---Color Menu----");
                        System.out.println("1. Red");
                        System.out.println("2. Blue");
                        System.out.println("3. Green");
                        System.out.println("4. Random Color");
                        System.out.println("0. Exit");
                        System.out.println("Enter selection: ");
                        int colorselection = keyboard.nextInt();
                        
                              // TODO: write ifs to set the value for the color and store it into a variable
                              if (colorselection == 1)
                                    {
                                          /*Color*/ c = Color.RED;
                                                      
                                    }
                              else if (colorselection == 2)
                                    {
                                          /*Color*/ c = Color.BLUE;      
                                    }
                              else if (colorselection == 3)
                                    {
                                          /*Color*/ c = Color.GREEN;            
                                    }
                              else if (colorselection == 3)
                                    {
                                          /*Color*/ c = new Color ( (int)(Math.random()*256),(int)(Math.random()*256),(int)(Math.random()*256));      
                                    }
                              else
                              {
                                          System.exit(0);      
                              }
                              System.out.println(c.toString());
                              // TODO: create a new DrawingLine with the information gathered in this if
                  
                              //DrawingLine D1 = new DrawingLine (x1,x2,y1,y2,c);
							  }
							  
							  }while(true);
							  }
							  }

Open in new window

USA Java NewbieAuthor Commented:
krakatoa that is my thought process and my trouble shooting has led me to the same conclusion i dont know how to get in scope

Thanks
krakatoaCommented:
I said at first glance, but in any case you only need to declare the Color once, but if you run the code I posted you will see that 'c''s toString() shows you that it is being captured. In which case if it is then you should be able to pass it with all the params to the line's constructor.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CEHJCommented:
Do what Krakatoa posted (without changing the name of the enclosing class)
USA Java NewbieAuthor Commented:
java.awt.Color[r=255,g=0,b=0]
USA Java NewbieAuthor Commented:
Thanks figured it out
CEHJCommented:
Much easier btw is

c = new Color ((int)(Math.random() * 0xFFFFFF));      

Open in new window

krakatoaCommented:
Glad to hear.
(Apologies CEHJ  - you were waiting for the code repost, and I barged in). You should have gotten some points too though). :)
CEHJCommented:
No problem
USA Java NewbieAuthor Commented:
Eek sorry
krakatoaCommented:
Not to worry. ;)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.