Link to home
Start Free TrialLog in
Avatar of USA Java Newbie
USA Java Newbie

asked on

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);
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Please post the actual code. That wouldn't compile
'c' in the constructor call, doesnt look to be in scope to me at first glance.
Avatar of USA Java Newbie
USA Java Newbie

ASKER

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

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
ASKER CERTIFIED SOLUTION
Avatar of krakatoa
krakatoa
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Do what Krakatoa posted (without changing the name of the enclosing class)
java.awt.Color[r=255,g=0,b=0]
Thanks figured it out
Much easier btw is

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

Open in new window

Glad to hear.
(Apologies CEHJ  - you were waiting for the code repost, and I barged in). You should have gotten some points too though). :)
No problem
Eek sorry
Not to worry. ;)