Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help with my java progamming and it\'s being done on a mac computer and I

ID: 3818975 • Letter: I

Question

I need help with my java progamming and it's being done on a mac computer and I still need help displaying these images. I also want screen shots on how they were display that would be helpful for me to see what I'm doing wrong. Here is the code and here is what I did.

This is were my image in the work space

This is what displays and it's still not displaying an image

Here is the code

import java.awt.Dimension;

import java.awt.FlowLayout;

import java.awt.GridLayout;

import java.awt.event.*;

import java.text.SimpleDateFormat;

import java.util.*;

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 dice;

private JLabel computerLabel;

private JLabel playerStatusLabel1,playerStatusLabel2; //need two label to display 2 player dice

private JLabel computerStatusLabel1,computerStatusLabel2;////need two label to display 2 computer dice

//add an image icon to display dice image on the labels

private ImageIcon imageIcon;

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,600);

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);

playerStatusLabel1 = new JLabel("",JLabel.CENTER);

playerStatusLabel2 = new JLabel("",JLabel.CENTER);

diceLabel=new JLabel("",JLabel.CENTER);

computerStatusLabel1=new JLabel("",JLabel.CENTER);

computerStatusLabel2=new JLabel("",JLabel.CENTER);

playerStatusLabel1.setSize(119,116);

playerStatusLabel2.setSize(119,116);

computerStatusLabel1.setSize(119,116);

computerStatusLabel2.setSize(119,116);

controlPanel = new JPanel();

controlPanel.setLayout(new FlowLayout());

mainFrame.add(headerLabel);

mainFrame.add(diceLabel);

mainFrame.add(playerLabel);

mainFrame.add(computerLabel);

  

mainFrame.add(controlPanel);

mainFrame.add(playerStatusLabel1);mainFrame.add(playerStatusLabel2);

mainFrame.add(computerStatusLabel1); mainFrame.add(computerStatusLabel2);

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");

if(cPoints

JOptionPane.showMessageDialog(mainFrame, "Player Wins");

if(cPoints==pPoints)

JOptionPane.showMessageDialog(mainFrame, "Its a TIE");

  

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++;

}

//Set the image icon based on playerDice values

//split the values to get single values to determine image icon

String[] PlayerDiceIcons = playerDice.split(",");

String[] ComputerDiceIcons = computerDice.split(",");

String player_dice1 = getIconFileName(PlayerDiceIcons[0]);

String player_dice2 = getIconFileName(PlayerDiceIcons[1]);

String computer_dice1 = getIconFileName(ComputerDiceIcons[0]);

String computer_dice2 = getIconFileName(ComputerDiceIcons[1]);

//display player dice icons

imageIcon = new ImageIcon("D:\Java Projects\TestApp\src\testapp\"+player_dice1);

playerStatusLabel1.setIcon(imageIcon);

imageIcon = new ImageIcon("D:\Java Projects\TestApp\src\testapp\"+player_dice2);

playerStatusLabel2.setIcon(imageIcon);

playerStatusLabel1.setText("Player Dice Rolled:"+playerDice+" points "+pPoints);

//display computer dice icons

imageIcon = new ImageIcon("D:\Java Projects\TestApp\src\testapp\"+computer_dice1);

computerStatusLabel1.setIcon(imageIcon);

imageIcon = new ImageIcon("D:\Java Projects\TestApp\src\testapp\"+computer_dice2);

computerStatusLabel2.setIcon(imageIcon);

computerStatusLabel1.setText("Computer Dice Rolled:"+computerDice+" 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");

if(cPoints

JOptionPane.showMessageDialog(mainFrame, "Player Wins");

if(cPoints==pPoints)

JOptionPane.showMessageDialog(mainFrame, "Its a TIE");

  

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);

}

public String getIconFileName(String DiceNumber)

{

String IconfileName = "";

if(DiceNumber.equals("1")){IconfileName = "DieFace1.gif";}

if(DiceNumber.equals("2")){IconfileName = "DieFace2.gif";}

if(DiceNumber.equals("3")){IconfileName = "DieFace3.gif";}

if(DiceNumber.equals("4")){IconfileName = "DieFace4.gif";}

if(DiceNumber.equals("5")){IconfileName = "DieFace5.gif";}

if(DiceNumber.equals("6")){IconfileName = "DieFace6.gif";}

return IconfileName;

}

}

Explanation / Answer

There might be path you are passing in ImageIcon it is wrong.

As you can see below i put the path like this and able to see iamges, please try using below option

imageIcon = new ImageIcon("home/anijhawa/Desktop/images.png",player_dice1);

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote