Pregunta

I've been given a project to do in which I have to roll 3 dice, and count the number of middle dots and dots everywhere else separately, and enter them in order to gain score. I've already made this game playable on the console in NetBeans, but when it comes to me trying to implement it into GUI, I'm stumped. I'm a complete beginner to making the GUI and not sure how to make images appear according to what the number on the die was rolled. Every time I try, either the images won't change if I re-roll the dice, or I'm simply having trouble linking the GUI to my code. So how should I make the images appear on the 3 panels in the JPanel according to what number I rolled on each die?

Here's the main part of the code:


 for(i = 0; i < 3; i++){
    System.out.println("Enter any number to roll the dice: ");
    cont = scan.nextInt();
    //Rolling 3 dice:
    dice1 = rollDice();
    dice2 = rollDice();
    dice3 = rollDice();
                        ..........................

Basically I want it to show image files that, for example, shows the first die with 1 dot in it if it rolls 1 on a panel to the left, the second die with a 3 in it on a panel in the middle, and the third die with a 5 on a panel to the right on a JPanel and it should change appropriately every time I roll the dice.

I've been trying to make this work for ages! Help would be greatly appreciated. Please mind though, I want this to be as simple as possible for the project's sake. I'm just a first semester computer science student who's never done any GUI in class (nor any OOP).

Thanks in advance! :)

¿Fue útil?

Solución

Consider these as my pictures for the dice faces:

face1.png, face2.png, face3.png, face4.png, face5.png, face6.png 

I would advise, using a JLabel (added to each panel) as it is 'simpler' to add a background to this without going into paint components. See below adding face1 to it.

JLabel lblDiceFaceOne = new JLabel();
ImageIcon diceFace = new ImageIcon("face1.png");
lblDiceFaceOne.setIcon(diceFace);

So you could have an array of the file paths and then randomly select an index from these.

String[] paths = {"face1.png", "face2.png".....};

Once you have the random number from 0 to 5 (what you rolled with 0 representing 1 and 5 representing 6) then you can set the label to this particular picture using the string file path. Clear the label first as shown below.

lblDiceFaceOne.setIcon(null); //clear the previous dice picture
lblDiceFaceOne.setIcon(new ImageIcon(paths[i])); //use random file path from array

This can be done for all 3 items inside your GUI whereby you can have a random number within the range and set this to the selected file path to use for the image. To clarify 'i' represents the random integer we got between 0 and 5.

Just an idea that could work for you and is relatively simple to implement.

If you do have to use JPanels then this might help for setting its background.

Simplest way to set image as JPanel background

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top