I want to generate randomly one of three colours, either red,blue,white. I want to create a program that randomly generated one these colours to a person when after they enter their name in java. The purpose of the program would be for the user to enter a name and the out put would be that" name is assigned to either red, blue, or white"
Create an array:
String[] colors = {"red", "blue", "white"};
Generate a random number between 0 and 2:
Random rand = new Random();
int random = rand.nextInt((2 - 0) + 1);
Get the random color:
String randomColor = colors[random];
Related
Currently I generate random colors to paint, but I would like to know if there is a method to generate random colors within a range of the selected color, I want the colors not to be totally random, but to be from the same range of colors as the selected one
int colorselected = colors.getFirstColor();
int colorrandom = ((int) (Math.random() * 16777215));
paint4.setColorFilter(new PorterDuffColorFilter(colorselected+colorrandom, PorterDuff.Mode.SRC_IN));
I'm developing a chat application, for this, I'm in need of generation random colour for the user profile picture. But I need to generate the same colour for the same person. I have a unique user id. I don't like to save the colour code after generating the colour code once. So, basically I need an method to get colour code.
int getUserColourCode(String userId) {
//code needed.
//It should return random colour code (int) with respect to user id.
//I would like to exclude light shade colours (dark and semi dark colours are preferred).
}
Thanks in advance.
int getUserColourCode(String userId) {
StringBuilder input1 = new StringBuilder();
input1.append(userId);
input1=input1.reverse();
String pair[]={"0","0","0","0","0","0","0","0"};
char[] character = input1.toString().toCharArray();
for(int i=0;i<character.length;i++)
{
pair[i]=String.valueOf(character[i]);
}
int color = Color.argb((Integer.parseInt(pair[0]+pair[1])*2)+50, (Integer.parseInt(pair[2]+pair[3])*2)+50, (Integer.parseInt(pair[4]+pair[5])*2)+50, (Integer.parseInt(pair[6]+pair[7])*2)+50);
return color;
}
First, you need to pass integer as the argument of you function. Instead of String userId, please use int userId. If you still want to pass String type as the argument, you need to parse it to integer.
Second, you need to define all the colors you want in an array.
Basically you can not use Java Random number build in function because it will always generate new random number, so it will not match with your need.
int getUserColourCode(String userId) {
int id = Integer.parseInt(userId);
//create integer color as much as you want,
int[] colors = {Color.BLUE, Color.CYAN, Color.MAGENTA, Color.parseColor("#ff00f8")};
int colorLength = colors.length - 1;
int randomNumber = id % colorLength;
return colors[randomNumber];
}
If you pass integer as the type of your argument you can use this:
int getUserColourCode(int userId) {
//create integer color as much as you want,
int[] colors = {Color.BLUE, Color.CYAN, Color.MAGENTA, Color.parseColor("#ff00f8")};
int colorLength = colors.length - 1;
int randomNumber = userId % colorLength;
return colors[randomNumber];
}
Using this you can generate random colors from color code,
But if you need to get color from the user you need shared preference value for the local database to save the color of the user
int getUserColourCode(String userId) {
Random rnd = new Random();
int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
return color;
}
I need one small advice on how to set more than one colour to a method setBackgroundColor, i managed to make more than one color but only if program randomly pick color but i want to set specific 4 or five colours,here is my part of the code:
(so on selected object it changes color)
if (isSelected)
style.setBackgroundColor (new Color ((float) Math.random(),
(float) Math.random(),
(float) Math.random()));
else
style.unsetBackgroundColor();
Since you want 4 or 5 specific colors what you could do is make a list.
ArrayList<Color> colorList = new ArrayList<Color>();
//Then you add any colors you want, although you would have to define them yourself.
colorList.add(color1);
After adding the colors to a color list, you would need a way to grab a random color. One way we could do this is by making a Random object and using it to find an Integer from 0 to the list's size.
Random rand = new Random();
int colorNum = rand.nextInt(colorList.size());
Now that we have the actual number, we can simply access that index in the list.
Color c = colorList.get(colorNum);
//Now, assuming your code above works for one color, you could do your
style.setBackgroundColor(c);
This way you can add any colors in or even make colors based on user request and it would handle any color as long as you add it to the list.
How would I display a random number into a text field on jFrame? My method for generating a random number is in a different class.
Random rn = new Random();
int i = rn.nextInt(51);
System.out.println(i);
How would I make the number appear in the textfield?
You don't have to use a seperate class.
Just do as following.
Random rn = new Random();
JTextField textField = //initialize your text field here
Then set the random number as,
textField.setText(Integer.toString(rn.nextInt(51)));
I am taking a programming class, and I have to make an instrument "play". I want to use Math.random to create a random number between either 0-9 or 0-10 and have that number correspond to a picture in an array displayed via JOptionPane icon. My only issue is, how can I create a program that will correspond a random int to a picture and then present it using JOption Pane. Here is what I have so far:
public static String Flute(String pickYourInstrument, String instrument){
//try to assign variables to pictures in an array
ImageIcon icon = new ImageIcon("/home/james/programmingpics/A_Flute");
JOptionPane.showMessageDialog(null, "A Note", "A Note with Flute",
JOptionPane.OK_OPTION, icon);
for (int i = 0; i < 1000; i++) {
int random = 1 * (int) (Math.random() * 10);
System.out.println(random);
}
}
I am stuck, I stopped after I realized that I didn't know how to make the ImageIcon icon into an array (I have nine other pictures to make an icon for). Does anyone have an idea how I could create the program?
Simply create an array of ImageIcon, then the random number obtained can be used as an index to that array to get the corresponding icon. Something as simple as
int randomNumber = //.... get random int
ImageIcon myIcon = iconArray[randomNumber];