I still need help putting these dice images in to the box so that everytime you
ID: 3817583 • Letter: I
Question
I still need help putting these dice images in to the box so that everytime you roll the player and computer will display the image of the dice and the number they roll but I'm getting error gif not found since I put the gifs in the workspace chicago folder. Here is what it looks like and these are the images that I am using for the dice to display they are DieFace1.gif through DieFace6.gif
Error message I don't know why? It can't find gif.
The images are in the folder
Here is the java code
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class SwingControlDemo {
private JFrame mainFrame;
private JLabel headerLabel;
private JLabel diceLabel;
String diceString = "";
private JLabel playerLabel;
List<String> dice;
private JLabel computerLabel;
private JLabel playerStatusLabel;
private JLabel computerStatusLabel;
private JLabel playerDie1Label;
private JLabel playerDie2Label;
private JLabel computerDie1Label;
private JLabel computerDie2Label;
private JLabel pPointsLabel;
private JLabel cPointsLabel;
JPanel p;
JPanel c;
private int pPoints = 0;
private int cPoints = 0;
int counter = 2;
private JPanel controlPanel;
public boolean timeOver = false;
Random rand;
public SwingControlDemo() {
prepareGUI();
rand = new Random();
dice = new ArrayList<>();
for (int i = 1; i <= 6; i++)
for (int j = 1; j <= 6; j++) {
dice.add(i + "," + j);
}
diceString = dice.get(rand.nextInt(36));
diceLabel.setText("DICE :" + diceString);
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
new Thread(new Runnable() {
@Override
public void run() {
Date date = new Date();
date.setHours(0);
date.setMinutes(10);
date.setSeconds(0);
while (!timeOver) {
// TODO Auto-generated method stub
headerLabel.setText(sdf.format(date));
date.setSeconds(date.getSeconds() - 1);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (date.getMinutes() == 0 && date.getSeconds() == 0)
timeOver = true;
}
}
}).start();
;
}
public static void main(String[] args)
{
SwingControlDemo swingControlDemo = new SwingControlDemo();
swingControlDemo.showButtonDemo();
}
private void prepareGUI()
{
mainFrame = new JFrame("Chicago Dice Game");
mainFrame.setSize(800, 800);
mainFrame.setLayout(new GridLayout(10, 1));
mainFrame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent windowEvent)
{
System.exit(0);
}
});
playerLabel = new JLabel(" PLAYER ", JLabel.LEFT);
computerLabel = new JLabel(" COMPUTER ", JLabel.LEFT);
headerLabel = new JLabel("", JLabel.CENTER);
playerStatusLabel = new JLabel("", JLabel.LEFT);
playerDie1Label = new JLabel("", JLabel.CENTER);
playerDie2Label = new JLabel("", JLabel.RIGHT);
diceLabel = new JLabel("", JLabel.CENTER);
computerStatusLabel = new JLabel("", JLabel.CENTER);
computerDie1Label = new JLabel("", JLabel.LEFT);
computerDie2Label = new JLabel("", JLabel.RIGHT);
pPointsLabel = new JLabel("", JLabel.CENTER);
cPointsLabel = new JLabel("", JLabel.CENTER);
playerStatusLabel.setSize(600, 100);
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
p = new JPanel();
p.setLayout(new FlowLayout());
c = new JPanel();
c.setLayout(new FlowLayout());
mainFrame.add(headerLabel);
mainFrame.add(diceLabel);
mainFrame.add(playerLabel);
mainFrame.add(computerLabel);
mainFrame.add(controlPanel);
p.add(playerStatusLabel);
p.add(playerDie1Label);
p.add(playerDie2Label);
p.add(pPointsLabel);
mainFrame.add(p);
c.add(computerStatusLabel);
c.add(computerDie1Label);
c.add(computerDie2Label);
c.add(cPointsLabel);
mainFrame.add(c);
mainFrame.setVisible(true);
}
private void showButtonDemo()
{
// resources folder should be inside SWING folder.
JButton rollDice = new JButton("Roll Dice");
rollDice.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if (!timeOver)
{
if (counter <= 12)
counter++;
else {
if (cPoints > pPoints)
{
JOptionPane.showMessageDialog(mainFrame, "Computer Wins");
} else if (cPoints == pPoints)
{
JOptionPane.showMessageDialog(mainFrame, "Its a TIE");
} else
{
JOptionPane.showMessageDialog(mainFrame, "Player Wins");
}
JOptionPane.showMessageDialog(mainFrame, "Restrating the game");
counter = 2;
diceLabel.setText("DICE: " + dice.get(rand.nextInt(36)));
cPoints = 0;
pPoints = 0;
}
String playerDice = dice.get(rand.nextInt(36));
String computerDice = dice.get(rand.nextInt(36));
if (playerDice.equalsIgnoreCase(diceString)) {
pPoints++;
}
if (computerDice.equalsIgnoreCase(diceString)) {
cPoints++;
}
playerStatusLabel.setText("Player Dice Rolled:");
String[] playerDices = playerDice.split(",");
playerDie1Label.setOpaque(true);
ImageIcon playerDie1Icon = createImageIcon(playerDices[0] + ".gif", "");
playerDie1Label.setIcon(playerDie1Icon);
playerDie2Label.setOpaque(true);
ImageIcon playerDie2Icon = createImageIcon(playerDices[1] + ".gif", "");
playerDie2Label.setIcon(playerDie2Icon);
pPointsLabel.setText("Player points " + pPoints);
computerStatusLabel.setText("Computer Dice Rolled:");
String[] computerDices = computerDice.split(",");
computerDie1Label.setOpaque(true);
ImageIcon computerDie1Icon = createImageIcon(computerDices[0] + ".gif", "");
computerDie1Label.setIcon(computerDie1Icon);
computerDie2Label.setOpaque(true);
ImageIcon computerDie2Icon = createImageIcon(computerDices[1] + ".gif", "");
computerDie2Label.setIcon(computerDie2Icon);
cPointsLabel.setText("Computer points " + cPoints);
playerLabel.setText(" PLAYER : " + playerDice + " " + pPoints);
computerLabel.setText(" COMPUTER: " + computerDice + " " + cPoints);
}
else
{
JOptionPane.showMessageDialog(mainFrame, "TimeUp");
if (cPoints > pPoints)
JOptionPane.showMessageDialog(mainFrame, "Computer Wins");
else if (cPoints == pPoints)
JOptionPane.showMessageDialog(mainFrame, "Its a TIE");
else
JOptionPane.showMessageDialog(mainFrame, "Player Wins");
JOptionPane.showMessageDialog(mainFrame, "Restrating the game");
counter = 2;
diceString = dice.get(rand.nextInt(36));
cPoints = 0;
pPoints = 0;
System.exit(0);
}
}
});
controlPanel.add(rollDice);
mainFrame.setVisible(true);
}
protected static ImageIcon createImageIcon(String path, String description)
{
java.net.URL imgURL = Label.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL, description);
}
else
{
System.err.println("Couldn't find file: " + path);
return null;
}
}
}
This what should pop up and look like this, but I can't seem to get this way
Explanation / Answer
In the code, it says, the resources that is the .gif files need to be resource folder inside SWING folder.
Most of the times the resource folder is named as res. Make sure you put them in res folder.
Now if doesnt solve the issue, just put the obsolute path of the gif file.
firstly look at the createImaageicon function
protected static ImageIcon createImageIcon(String path, String description)
it takes path, and take a look at what being passed t this function
ImageIcon playerDie1Icon = createImageIcon(playerDices[0] + ".gif", "");
so it is passing, something.gif not the obsolute path.
I think if we add the path i,e /SWING/res/ like this, it shoukld resolve your problem.
ImageIcon playerDie1Icon = createImageIcon("SWING/res/"+playerDices[0] + ".gif", "");
If this doesnt give right solution, make sure the playerDices[i] and computerDices[i] has DiceFacei.gif as value.