I am working on this Rock, paper, scissors java game and the program works, but I can not figure out how to get the images to load onto the program. So my question is how do I get the images to load up with the program? I am using JCreator for this project. I have created the Basic Java Application project, and then added in the 3 .java files that I need to run the program, but I just can not figure out how or where I need to upload the files. The game works without the images, but I would really like them to show up.
This is the .java file that calls up the images:
Expand|Select|Wrap|Line Numbers
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class pss extends JPanel implements ActionListener, ItemListener
- {
- private final Color clrBackground = new Color(163,243,255);
- private final Color clrForeground = new Color(0,0,0);
- private JComboBox cboxWeapon;
- private JTextField txtCPUWeapon, txtWins, txtLoses, txtDraws;
- private JLabel lblPlayerWeapon, lblCPUWeapon, lblWins, lblLoses, lblDraws, lblStatus, lblPlayerWeaponIcon, lblCPUWeaponIcon;
- private JButton cmdPlay, cmdReset;
- private ImageIcon[] imgWeapon;
- private JPanel panRoot, panPlayerArea, panPlayerWeapon, panCPUArea, panCPUWeapon, panStatusArea, panGo, panCounters, panWins, panLoses, panDraws;
- private pssEngine engine = new pssEngine();
- private objCreateAppletImage createImage = new objCreateAppletImage();
- private boolean errorWithImages = false;
- public static void main(String[] args) //With applications, you have to specify a main method (not with applets)
- {
- JFrame.setDefaultLookAndFeelDecorated(true); //Make it look nice
- JFrame frame = new JFrame("Paper Stone Scissors"); //Title
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setResizable(false); //Stops the user resizing the window
- JComponent paneMain = new pss();
- paneMain.setOpaque(true);
- paneMain.setPreferredSize(new Dimension(420,350));
- frame.setContentPane(paneMain);
- frame.pack();
- frame.setVisible(true);
- }
- public pss ()
- {
- cboxWeapon = new JComboBox(engine.getWeapon());
- cboxWeapon.addItemListener(this);
- txtCPUWeapon = new JTextField(engine.getStrCPUWeapon(), 5);
- txtWins = new JTextField("0", 5);
- txtLoses = new JTextField("0", 5);
- txtDraws = new JTextField("0", 5);
- txtCPUWeapon.setEditable(false);
- txtWins.setEditable(false);
- txtLoses.setEditable(false);
- txtDraws.setEditable(false);
- lblPlayerWeapon = new JLabel("Choose your weapon", JLabel.CENTER);
- lblCPUWeapon = new JLabel("The CPU's weapon", JLabel.CENTER);
- lblWins = new JLabel("Amount of wins:", JLabel.RIGHT);
- lblLoses = new JLabel("Amount of loses:", JLabel.RIGHT);
- lblDraws = new JLabel("Amount of Drawss:", JLabel.RIGHT);
- lblStatus = new JLabel("", JLabel.CENTER);
- lblPlayerWeaponIcon = new JLabel("", JLabel.CENTER);
- lblCPUWeaponIcon = new JLabel("", JLabel.CENTER);
- lblPlayerWeaponIcon.setPreferredSize(new Dimension(150,150));
- lblCPUWeaponIcon.setPreferredSize(new Dimension(150,150));
- cmdPlay = new JButton("Go!");
- cmdReset = new JButton("Restart");
- cmdPlay.addActionListener(this);
- cmdReset.addActionListener(this);
- try
- {
- imgWeapon = new ImageIcon[3];
- for (int i = 0; i < 3; i++)
- {
- imgWeapon[i] = createImage.getImageIcon(this, ".src/images/" + engine.getWeapon(i) + ".gif", "Icon for " + engine.getWeapon(i), 13000);
- }
- lblPlayerWeaponIcon.setIcon(imgWeapon[0]);
- lblCPUWeaponIcon.setIcon(imgWeapon[0]);
- }
- catch (Exception ex) //The game works without the images, so carry on
- {
- errorWithImages = true;
- }
- setLayout(new BorderLayout());
- panRoot = new JPanel(new BorderLayout());
- panPlayerArea = new JPanel(new BorderLayout());
- panPlayerWeapon = new JPanel(new BorderLayout());
- panCPUArea = new JPanel(new BorderLayout());
- panCPUWeapon = new JPanel(new BorderLayout());
- panStatusArea = new JPanel(new BorderLayout());
- panGo = new JPanel();
- panCounters = new JPanel(new GridLayout(3,1,2,2));
- panWins = new JPanel();
- panLoses = new JPanel();
- panDraws = new JPanel();
- add(panRoot, BorderLayout.CENTER);
- panRoot.add(panPlayerArea, BorderLayout.WEST);
- panPlayerArea.add(panPlayerWeapon, BorderLayout.NORTH);
- panPlayerWeapon.add(lblPlayerWeapon, BorderLayout.NORTH);
- panPlayerWeapon.add(cboxWeapon, BorderLayout.SOUTH);
- panPlayerArea.add(lblPlayerWeaponIcon, BorderLayout.SOUTH);
- panRoot.add(panCPUArea, BorderLayout.EAST);
- panCPUArea.add(panCPUWeapon, BorderLayout.NORTH);
- panCPUWeapon.add(lblCPUWeapon, BorderLayout.NORTH);
- panCPUWeapon.add(txtCPUWeapon, BorderLayout.SOUTH);
- panCPUArea.add(lblCPUWeaponIcon, BorderLayout.SOUTH);
- panRoot.add(panStatusArea, BorderLayout.SOUTH);
- panStatusArea.add(panGo, BorderLayout.NORTH);
- panGo.add(cmdPlay);
- panGo.add(cmdReset);
- panGo.add(lblStatus);
- panStatusArea.add(panCounters, BorderLayout.SOUTH);
- panCounters.add(panWins);
- panWins.add(lblWins);
- panWins.add(txtWins);
- panCounters.add(panLoses);
- panLoses.add(lblLoses);
- panLoses.add(txtLoses);
- panCounters.add(panDraws);
- panDraws.add(lblDraws);
- panDraws.add(txtDraws);
- panRoot.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
- setBackground(clrBackground);
- panRoot.setBackground(clrBackground);
- panPlayerArea.setBackground(clrBackground);
- panPlayerWeapon.setBackground(clrBackground);
- panCPUArea.setBackground(clrBackground);
- panCPUWeapon.setBackground(clrBackground);
- panStatusArea.setBackground(clrBackground);
- panGo.setBackground(clrBackground);
- panCounters.setBackground(clrBackground);
- panWins.setBackground(clrBackground);
- panLoses.setBackground(clrBackground);
- panDraws.setBackground(clrBackground);
- lblPlayerWeapon.setForeground(clrForeground);
- lblCPUWeapon.setForeground(clrForeground);
- lblWins.setForeground(clrForeground);
- lblLoses.setForeground(clrForeground);
- lblDraws.setForeground(clrForeground);
- txtWins.setForeground(clrForeground);
- txtLoses.setForeground(clrForeground);
- txtDraws.setForeground(clrForeground);
- txtCPUWeapon.setForeground(clrForeground);
- }
- public void reset ()
- {
- cboxWeapon.setSelectedIndex(0);
- lblStatus.setText("");
- engine.reset();
- }
- public void actionPerformed (ActionEvent e)
- {
- if (e.getSource() == cmdReset)
- {
- reset();
- }
- else
- {
- lblStatus.setText(engine.play(cboxWeapon.getSelectedIndex()));
- }
- txtCPUWeapon.setText(engine.getStrCPUWeapon());
- txtWins.setText(Integer.toString(engine.getWins()));
- txtLoses.setText(Integer.toString(engine.getLoses()));
- txtDraws.setText(Integer.toString(engine.getDraws()));
- if (!errorWithImages)
- {
- lblCPUWeaponIcon.setIcon(imgWeapon[engine.getCPUWeapon()]);
- }
- }
- public void itemStateChanged (ItemEvent e)
- {
- if (!errorWithImages)
- {
- lblPlayerWeaponIcon.setIcon(imgWeapon[cboxWeapon.getSelectedIndex()]);
- }
- }
- }
Expand|Select|Wrap|Line Numbers
- import java.awt.*;
- import java.io.*;
- import javax.swing.ImageIcon;
- public class objCreateAppletImage
- {
- public void objCreateAppletImage ()
- {
- }
- //If an error occurs (or is thrown by me) it will be thrown to the next level up, and either caught or thrown
- public ImageIcon getImageIcon (Object parentClass, String path, String description, int fileSize) throws Exception
- {
- int count = 0;
- BufferedInputStream imgStream = new BufferedInputStream(parentClass.getClass().getResourceAsStream(path));
- byte buff[] = new byte[fileSize];
- if (imgStream == null) //If doesn't exist
- {
- throw new Exception("File not Found");
- }
- try
- {
- count = imgStream.read(buff);
- imgStream.close(); //Closes the stream
- }
- catch (IOException ex)
- {
- throw new Exception("Corrupt file");
- }
- return new ImageIcon(Toolkit.getDefaultToolkit().createImage(buff), description); //Creates the image from the byte array
- }
- }
Thank you
Frank