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("---Men u----");
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("---Col or 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()*2 56),(int)( Math.rando m()*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);
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("---Men
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("---Col
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),(
}
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);
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.
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("---Men u----");
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("---Col or 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()*2 56),(int)( Math.rando m()*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);
}
}
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("---Men
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("---Col
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),(
}
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);
}
}
ASKER
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
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Do what Krakatoa posted (without changing the name of the enclosing class)
ASKER
java.awt.Color[r=255,g=0,b =0]
ASKER
Thanks figured it out
Much easier btw is
c = new Color ((int)(Math.random() * 0xFFFFFF));
Glad to hear.
(Apologies CEHJ - you were waiting for the code repost, and I barged in). You should have gotten some points too though). :)
(Apologies CEHJ - you were waiting for the code repost, and I barged in). You should have gotten some points too though). :)
No problem
ASKER
Eek sorry
Not to worry. ;)