473,503 Members | 2,313 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help with getting Images to show in a Rock, Paper, Scissors game

1 New Member
Hi

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
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5. public class pss extends JPanel implements ActionListener, ItemListener
  6. {
  7.  
  8. private final Color clrBackground = new Color(163,243,255);
  9. private final Color clrForeground = new Color(0,0,0);
  10.  
  11. private JComboBox cboxWeapon;
  12. private JTextField txtCPUWeapon, txtWins, txtLoses, txtDraws;
  13. private JLabel lblPlayerWeapon, lblCPUWeapon, lblWins, lblLoses, lblDraws, lblStatus, lblPlayerWeaponIcon, lblCPUWeaponIcon;
  14. private JButton cmdPlay, cmdReset;
  15. private ImageIcon[] imgWeapon;
  16. private JPanel panRoot, panPlayerArea, panPlayerWeapon, panCPUArea, panCPUWeapon, panStatusArea, panGo, panCounters, panWins, panLoses, panDraws;
  17.  
  18. private pssEngine engine = new pssEngine();
  19. private objCreateAppletImage createImage = new objCreateAppletImage();
  20.  
  21. private boolean errorWithImages = false;
  22.  
  23. public static void main(String[] args) //With applications, you have to specify a main method (not with applets)
  24. {
  25.  
  26. JFrame.setDefaultLookAndFeelDecorated(true); //Make it look nice
  27. JFrame frame = new JFrame("Paper Stone Scissors"); //Title
  28. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  29. frame.setResizable(false); //Stops the user resizing the window
  30.  
  31. JComponent paneMain = new pss();
  32. paneMain.setOpaque(true);
  33. paneMain.setPreferredSize(new Dimension(420,350));
  34. frame.setContentPane(paneMain);
  35.  
  36. frame.pack();
  37. frame.setVisible(true);
  38.  
  39. }
  40.  
  41. public pss ()
  42. {
  43.  
  44. cboxWeapon = new JComboBox(engine.getWeapon());
  45. cboxWeapon.addItemListener(this);
  46.  
  47. txtCPUWeapon = new JTextField(engine.getStrCPUWeapon(), 5);
  48. txtWins = new JTextField("0", 5);
  49. txtLoses = new JTextField("0", 5);
  50. txtDraws = new JTextField("0", 5);
  51.  
  52. txtCPUWeapon.setEditable(false);
  53. txtWins.setEditable(false);
  54. txtLoses.setEditable(false);
  55. txtDraws.setEditable(false);
  56.  
  57. lblPlayerWeapon = new JLabel("Choose your weapon", JLabel.CENTER);
  58. lblCPUWeapon = new JLabel("The CPU's weapon", JLabel.CENTER);
  59. lblWins = new JLabel("Amount of wins:", JLabel.RIGHT);
  60. lblLoses = new JLabel("Amount of loses:", JLabel.RIGHT);
  61. lblDraws = new JLabel("Amount of Drawss:", JLabel.RIGHT);
  62. lblStatus = new JLabel("", JLabel.CENTER);
  63.  
  64. lblPlayerWeaponIcon = new JLabel("", JLabel.CENTER);
  65. lblCPUWeaponIcon = new JLabel("", JLabel.CENTER);
  66.  
  67. lblPlayerWeaponIcon.setPreferredSize(new Dimension(150,150));
  68. lblCPUWeaponIcon.setPreferredSize(new Dimension(150,150));
  69.  
  70. cmdPlay = new JButton("Go!");
  71. cmdReset = new JButton("Restart");
  72.  
  73. cmdPlay.addActionListener(this);
  74. cmdReset.addActionListener(this);
  75.  
  76. try
  77. {
  78.  
  79.  
  80. imgWeapon = new ImageIcon[3];
  81. for (int i = 0; i < 3; i++)
  82. {
  83.  
  84. imgWeapon[i] = createImage.getImageIcon(this, ".src/images/" + engine.getWeapon(i) + ".gif", "Icon for " + engine.getWeapon(i), 13000);
  85. }
  86.  
  87. lblPlayerWeaponIcon.setIcon(imgWeapon[0]);
  88. lblCPUWeaponIcon.setIcon(imgWeapon[0]);
  89.  
  90. }
  91. catch (Exception ex) //The game works without the images, so carry on
  92. {
  93. errorWithImages = true;
  94. }
  95.  
  96. setLayout(new BorderLayout());
  97. panRoot = new JPanel(new BorderLayout());
  98. panPlayerArea = new JPanel(new BorderLayout());
  99. panPlayerWeapon = new JPanel(new BorderLayout());
  100. panCPUArea = new JPanel(new BorderLayout());
  101. panCPUWeapon = new JPanel(new BorderLayout());
  102. panStatusArea = new JPanel(new BorderLayout());
  103. panGo = new JPanel();
  104. panCounters = new JPanel(new GridLayout(3,1,2,2));
  105. panWins = new JPanel();
  106. panLoses = new JPanel();
  107. panDraws = new JPanel();
  108.  
  109. add(panRoot, BorderLayout.CENTER);
  110. panRoot.add(panPlayerArea, BorderLayout.WEST);
  111. panPlayerArea.add(panPlayerWeapon, BorderLayout.NORTH);
  112. panPlayerWeapon.add(lblPlayerWeapon, BorderLayout.NORTH);
  113. panPlayerWeapon.add(cboxWeapon, BorderLayout.SOUTH);
  114. panPlayerArea.add(lblPlayerWeaponIcon, BorderLayout.SOUTH);
  115. panRoot.add(panCPUArea, BorderLayout.EAST);
  116. panCPUArea.add(panCPUWeapon, BorderLayout.NORTH);
  117. panCPUWeapon.add(lblCPUWeapon, BorderLayout.NORTH);
  118. panCPUWeapon.add(txtCPUWeapon, BorderLayout.SOUTH);
  119. panCPUArea.add(lblCPUWeaponIcon, BorderLayout.SOUTH);
  120. panRoot.add(panStatusArea, BorderLayout.SOUTH);
  121. panStatusArea.add(panGo, BorderLayout.NORTH);
  122. panGo.add(cmdPlay);
  123. panGo.add(cmdReset);
  124. panGo.add(lblStatus);
  125. panStatusArea.add(panCounters, BorderLayout.SOUTH);
  126. panCounters.add(panWins);
  127. panWins.add(lblWins);
  128. panWins.add(txtWins);
  129. panCounters.add(panLoses);
  130. panLoses.add(lblLoses);
  131. panLoses.add(txtLoses);
  132. panCounters.add(panDraws);
  133. panDraws.add(lblDraws);
  134. panDraws.add(txtDraws);
  135.  
  136. panRoot.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
  137.  
  138. setBackground(clrBackground);
  139. panRoot.setBackground(clrBackground);
  140. panPlayerArea.setBackground(clrBackground);
  141. panPlayerWeapon.setBackground(clrBackground);
  142. panCPUArea.setBackground(clrBackground);
  143. panCPUWeapon.setBackground(clrBackground);
  144. panStatusArea.setBackground(clrBackground);
  145. panGo.setBackground(clrBackground);
  146. panCounters.setBackground(clrBackground);
  147. panWins.setBackground(clrBackground);
  148. panLoses.setBackground(clrBackground);
  149. panDraws.setBackground(clrBackground);
  150.  
  151. lblPlayerWeapon.setForeground(clrForeground);
  152. lblCPUWeapon.setForeground(clrForeground);
  153. lblWins.setForeground(clrForeground);
  154. lblLoses.setForeground(clrForeground);
  155. lblDraws.setForeground(clrForeground);
  156. txtWins.setForeground(clrForeground);
  157. txtLoses.setForeground(clrForeground);
  158. txtDraws.setForeground(clrForeground);
  159. txtCPUWeapon.setForeground(clrForeground);
  160.  
  161. }
  162.  
  163. public void reset ()
  164. {
  165.  
  166. cboxWeapon.setSelectedIndex(0);
  167. lblStatus.setText("");
  168.  
  169. engine.reset();
  170.  
  171. }
  172.  
  173. public void actionPerformed (ActionEvent e)
  174. {
  175.  
  176. if (e.getSource() == cmdReset)
  177. {
  178. reset();
  179. }
  180. else
  181. {
  182. lblStatus.setText(engine.play(cboxWeapon.getSelectedIndex()));
  183. }
  184.  
  185. txtCPUWeapon.setText(engine.getStrCPUWeapon());
  186. txtWins.setText(Integer.toString(engine.getWins()));
  187. txtLoses.setText(Integer.toString(engine.getLoses()));
  188. txtDraws.setText(Integer.toString(engine.getDraws()));
  189.  
  190. if (!errorWithImages)
  191. {
  192. lblCPUWeaponIcon.setIcon(imgWeapon[engine.getCPUWeapon()]);
  193. }
  194.  
  195. }
  196.  
  197. public void itemStateChanged (ItemEvent e)
  198. {
  199.  
  200. if (!errorWithImages)
  201. {
  202. lblPlayerWeaponIcon.setIcon(imgWeapon[cboxWeapon.getSelectedIndex()]);
  203. }
  204.  
  205. }
  206.  
  207. }
Here is the other .java file that calls on the Images:
Expand|Select|Wrap|Line Numbers
  1. import java.awt.*;
  2. import java.io.*;
  3. import javax.swing.ImageIcon;
  4.  
  5. public class objCreateAppletImage
  6. {
  7.  
  8. public void objCreateAppletImage ()
  9. {
  10. }
  11. //If an error occurs (or is thrown by me) it will be thrown to the next level up, and either caught or thrown
  12. public ImageIcon getImageIcon (Object parentClass, String path, String description, int fileSize) throws Exception
  13. {
  14.  
  15. int count = 0;
  16. BufferedInputStream imgStream = new BufferedInputStream(parentClass.getClass().getResourceAsStream(path));
  17. byte buff[] = new byte[fileSize];
  18.  
  19. if (imgStream == null) //If doesn't exist
  20. {
  21. throw new Exception("File not Found");
  22. }
  23.  
  24. try
  25. {
  26.  
  27. count = imgStream.read(buff);
  28. imgStream.close(); //Closes the stream
  29.  
  30. }
  31. catch (IOException ex)
  32. {
  33. throw new Exception("Corrupt file");
  34. }
  35.  
  36. return new ImageIcon(Toolkit.getDefaultToolkit().createImage(buff), description); //Creates the image from the byte array
  37.  
  38. }
  39.  
  40. }
Could someone please help me? I really have no idea and I would like this to work.

Thank you

Frank
Aug 27 '08 #1
1 3086
Nepomuk
3,112 Recognized Expert Specialist
Hi flg22! Welcome to bytes.com!

It's great to have you here!

When you post, please always keep to the Posting Guidelines and when you post code, please post it in [code] ... [/code] tags (I've added them for you in this case).

Also, please only post the relevant code - code snipplets should be fine, posting complete classes is normally far to much. Nobody will want to go through your complete code, if it's not necessary. And please describe what exactly your problem is - how far did you get with the image problem and what does your program do so far?

Otherwise, I'll just wish you the best and hope you enjoy being part of bytes.com!

Greetings,
Nepomuk (Moderator)
Aug 27 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
3380
by: abcd | last post by:
kutthaense Secretary Djetvedehald H. Rumsfeld legai predicted eventual vicmadhlary in Iraq mariyu Afghmadhlaistmadhla, kaani jetvedehly after "a ljetvedehg, hard slog," mariyu vede legai pressed...
5
2961
by: Craig Keightley | last post by:
Please help, i have attached my page which worksin IE but i cannnot get the drop down menu to fucntion in firefox. Any one have any ideas why? Many Thanks Craig ...
12
2200
by: gc | last post by:
Hi I'm working on a rock, scissors, paper program. I think I have most of it, but I am having trouble with my main function, here is my code: #include <stdio.h> #include <stdlib.h> /* for...
8
3140
by: CM | last post by:
Hi, Could anyone please help me? I am completing my Master's Degree and need to reproduce a Webpage in Word. Aspects of the page are lost and some of the text goes. I would really appreciate it....
1
1438
by: phuker420 | last post by:
don't know how to get started please help with this program. rock,paper,scissors We will write a program that multiple games of rock, paper, scissors: keeps a cumulitive score, quits when...
0
5518
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
2
1894
praclarush
by: praclarush | last post by:
I need to take an image that was in a Array object image and have it display in a table. this is for a item list the table has four columns one for the image, one for a description, one for a price,...
11
2382
by: blackhacker | last post by:
Please can anyone help me to make a simple simple Java code to force the game scissor,paper,rock to work ? for example i know how to do it in Visual Basic but not in java,for example this is kind...
8
5440
by: jmf777 | last post by:
Hi here is my problem I want to have the outcome of my rock paper scissors game to print outcomes like: You chose paper and I chose rock. You win. The value for player_choice and machine_choice is...
0
7194
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7267
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7316
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6976
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1495
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
729
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
372
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.