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)));
Related
I am creating a higher or lower card game, and currently, it generates a random number from 1 to 13 as just a simple string label (1,2,3,4 etc), depending on what number gets pulled.
Instead of this, I would like to show a graphic of the card corresponding to the number.
public int getRandomNumber(){
Random r = new Random();
int nr = r.nextInt(13);
return nr;
}
...
JLabel label = new JLabel(getRandomNumber(), JLabel.CENTER);
To Update Label text you can use this method
public void updateLabel(int nr){
label.setText(nr+"");
//place this method inside your Jframe class extend from javax.swing.Jframe
}
If you want to put an image according to the number. Supose you have an array of 13 images(numbers)//Image images[13];
then you can use this
public void setIcon(int nr){
ImageIcon icon = new ImageIcon(images[nr]);
label.setIcon(icon);
}
I think that what you are looking for is adding an Icon to a JLabel.
You can do that using the setIcon method which accept any implementation of Icon, as for instance ImageIcon.
Something like the following should work:
ImageIcon imgIcon = new ImageIcon(URL_OF_THE_PIC));
jLabel.setIcon(imgIcon);
and whenever a new card gets picked you can change text and Icon accordingly.
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];
I have created a matrix of JButtons in a JFrame using the GUI of NetBeans. I want to select them randomly, but I don't have any idea how to do it. Any idea will help. Thanks.
Having the list of JButtons.
List<JButton> buttons;
Simply pick one random from it with the Random instance.
Random rnd = new Random();
int i = rnd.nextInt(buttons.size());
JButton btn = buttons.get(i);
Works on the same logic like arrays.
So I have this 2d array of buttons and I have an array of images. I want to get the images on the buttons but I want the images to be on random buttons every time the program starts. Like this:What I want it to look like. Right now I can only get one color to be on all of the buttons by changing the value of icons when I make the new JButton. What I think I need to do is have Math.Random() set to a variable and to get a random value from the array of images and then put the variable in icons[] when i declare the new JButton but I don't know if this is right and don't know how to do it. I did some searching and tried using this:
var randomValue = icons[Math.floor(Math.random() * icons.length)];
but I get an error saying
possible loss of precision, required int, found double.
Help would be greatly appreciated. If you want me to post the entire code let me know.
// 2D Array of buttons
buttons = new JButton[8][8];
for(int row=0; row<8; row++)
{
for (int col=0; col<8; col++)
{
buttons[row][col] = new JButton(icons[0]);
buttons[row][col].setLocation(6+col*70, 6+row*70);
buttons[row][col].setSize(69,69);
getContentPane().add(buttons[row][col]);
}
}
// Array of images
public static ImageIcon[] icons = {new ImageIcon("RedButton.png"),
new ImageIcon("OrangeButton.png"),
new ImageIcon("YellowButton.png"),
new ImageIcon("GreenButton.png"),
new ImageIcon("BlueButton.png"),
new ImageIcon("LightGrayButton.png"),
new ImageIcon("DarkGrayButton.png")};
I'd simplify this greatly by putting all my ImageIcons in an ArrayList, calling java.util.Collections.shuffle(...) on the ArrayList, and then passing out the ImageIcons from the shuffled ArrayList in order. Or if your buttons allow for repeated icons, then use a java.util.Random variable, say called random and simply call random.nextInt(icons.length) to get a random index for my array.
As an aside, please for your own sake, don't use null layout and absolute positioning. Your grid of JButtons is begging to be held in a GridLayout-using JPanel. Begging.
As an aside, why are you posting questions on the same project but using different names? You've similar posts but different user names in both of your other posts here:
JButtons won't update on button click
My New Game JButton is not working?
Before you set the icons on the JButton use this shuffle function...
public ImageIcon[] shuffle(ImageIcon[] icons)
{
int index = 0;
ImageIcon temp = 0;
for(int i = icons.length -1; i > 0; i--)
{
index = r.nextInt(i + 1);
temp = icons[index];
icons[index] = icons[i];
icons[i] = temp;
}
return icons;
}
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];