Nikolay Rubashka
Nikolay Rubashka

Reputation: 1

Random Color Print in java on Eclipse

I have created a Random Color game and part of the code goes is:

public static void getARandomColor() 
{
    System.out.println();
    System.out.println("Your random color is...");
    // red
    MY_RANDOM = RANDOM_NUMBER.nextInt(COLOR_MAX);
    RED = MY_RANDOM;
    // green
    MY_RANDOM = RANDOM_NUMBER.nextInt(COLOR_MAX);
    GREEN = MY_RANDOM;
    // blue
    MY_RANDOM = RANDOM_NUMBER.nextInt(COLOR_MAX);
    BLUE = MY_RANDOM;
    // combinations...
    System.out.println("Your color is r: " + RED + ", g: " + GREEN
            + ", b: " + BLUE + "!");
    System.out.println();
}

So everything works that is shown, but I want to go further and add text right after the
"System.out.println("Your color is r: " + RED + ", g: " + GREEN + ", b: " + BLUE + "!");" line which becomes the random color given.

full code

     import java.awt.Color;
    import java.util.Random;
    import java.util.Scanner;

    import javax.swing.JTextArea;

    /**
     * My RNG game has multiple choices; Array, Coins, and colors.
     */
    public class RandomNumberGenerator 
    {
        // all colors available
        private static final int COLOR_MAX = 256;
        private static int RED, GREEN, BLUE;
        private static int MY_RANDOM;
        static Scanner MY_CONSOLE = new Scanner(System.in);
        static Random RANDOM_NUMBER = new Random();
        private static int[] RANDOM_NUM_ARRAY;

        /**
         * Make the game work! Main System of the game.
         */
        public static void main(String[] args) 
        {

            while (true) 
            {
                // menu intro
                System.out.println("Welcome to the Random Generation Game!");
                System.out.println("Chose an option below: ");
                System.out.println();
                // menu
                System.out.println("1. Generate a random array of numbers!");
                System.out.println("2. Flip a coin!");
                System.out.println("3. Create a random color!");
                System.out.println("4. Exit!");
                int option = MY_CONSOLE.nextInt();
                // which option is chosen?
                // random array game
                if (option == 1) 
                {
                    createYourRandomArray();
                }
                // coin game
                else if (option == 2) 
                {
                    flipTheCoinGame();
                }
                // color game
                else if (option == 3) 
                {
                    getARandomColor();
                }
                // quit?
                // I tried to put 1, 2, 3 and 4 into constants but then the code
                // would not read the value behind the constant and would go into
                // my else...
                else if (option == 4) 
                {
                    break;
                }
                // Error!
                else 
                {
                    System.out.println("Invalid! Choose 1-4!");
                }
            }

            // close console
            MY_CONSOLE.close();

            System.out.println();
            // end of game text
            System.out.println("See ya later!");
        }

        // random array game works
        /**
         * The array of random numbers created here.
         */
        public static void createYourRandomArray() 
        {
            // elements?
            System.out.println("How many elements do you want in your array?");
            int element = MY_CONSOLE.nextInt();
            MY_CONSOLE.nextLine();
            // min?
            System.out.println("Min value?");
            int myMin = MY_CONSOLE.nextInt();
            MY_CONSOLE.nextLine();
            // max?
            System.out.println("Max?");
            int myMax = MY_CONSOLE.nextInt();
            MY_CONSOLE.nextLine();
            // random works
            RANDOM_NUM_ARRAY = new int[element];
            // open
            System.out.println();
            System.out.print("[");
            //
            for (int i = 0; i < RANDOM_NUM_ARRAY.length; i++) 
            {
                MY_RANDOM = RANDOM_NUMBER.nextInt(myMax - myMin + 1);
                MY_RANDOM += myMin;

                RANDOM_NUM_ARRAY[i]++;
                RANDOM_NUM_ARRAY[i] = MY_RANDOM;

                if (i < RANDOM_NUM_ARRAY.length - 1) 
                {
                    System.out.print(RANDOM_NUM_ARRAY[i] + ", ");
                } 
                else 
                {
                    System.out.print(RANDOM_NUM_ARRAY[i]);
                }

            }

            System.out.println("]");
            System.out.println();
        }

        /**
         * The Flip coin part of game created here.
         */
        public static void flipTheCoinGame() 
        {
            int coinTail = 0;
            int coinHead = 0;

            System.out.println("How many times would you like to flip the coin?");
            int youFlip = MY_CONSOLE.nextInt();
            System.out.println();

            for (int i = 0; i < youFlip; i++) 
            {
                MY_RANDOM = RANDOM_NUMBER.nextInt(2);
                if (MY_RANDOM == 1) 
                {
                    System.out.print("heads, ");
                    coinHead++;
                }
                // if not heads then tails
                else 
                {
                    System.out.print("tails, ");
                    coinTail++;
                }
            }

            System.out.println();
            System.out.println("You flipped " + coinHead + " heads and " + coinTail
                    + " tails!");
            System.out.println();
        }

        // Generates color in RGB form.
        /**
         * Random color generator. Creates a random RGB.
         */
        public static void getARandomColor() 
        {
            System.out.println();
            System.out.println("Your random color is...");
            // red
            MY_RANDOM = RANDOM_NUMBER.nextInt(COLOR_MAX);
            RED = MY_RANDOM;
            // green
            MY_RANDOM = RANDOM_NUMBER.nextInt(COLOR_MAX);
            GREEN = MY_RANDOM;
            // blue
            MY_RANDOM = RANDOM_NUMBER.nextInt(COLOR_MAX);
            BLUE = MY_RANDOM;
            // combinations...
            System.out.println("Your color is r: " + RED + ", g: " + GREEN
                    + ", b: " + BLUE + "!");
            System.out.println();
        }
    }

Upvotes: 0

Views: 1427

Answers (1)

Shashidhar Patil
Shashidhar Patil

Reputation: 55

I think there in no method to get name of color in java. so you create the following method.

String getColorName(int r, int g, int b){switch(r){case 0: switch(g){
    case 0: switch(b)
            {
            case 0: return "black";
            break;
            case 128: return "Navy";
            break;
            case 255: return "Blue";
            break;
            //continue for all color

            }
    }
    }

you can refer this for color names and RGB values. enter link description here

Upvotes: 1

Related Questions