473,326 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Trouble with setIcon with JLabel

I am trying to create a simple slot program. I have no problem with getting the initial graphics representing the reels to display. I am having trouble when it comes to changing the reels graphics. I try to change the graphic in the playslot method


Here is the class I am having trouble with:

Expand|Select|Wrap|Line Numbers
  1. import java.awt.*;
  2. import javax.swing.*;
  3. import java.io.*;
  4. import java.util.Random;  // Needed for Random class
  5. import java.awt.event.*;
  6.  
  7. public class AddSlotPanel extends JPanel
  8. {
  9.     // The following variables are needed to add
  10.     // the image to the window
  11.     private JPanel reelOneImagePanel; //Panel to hold the image for reel one
  12.     private JLabel reelOneImageLabel; // label that will be the image for reel one
  13.     private JPanel reelTwoImagePanel; //Panel to hold the image for reel two
  14.     private JLabel reelTwoImageLabel; // label that will be the image for reel two
  15.     private JPanel reelThreeImagePanel; //Panel to hold the image for reel three    
  16.     private JLabel reelThreeImageLabel; // label that will be the image for reel three
  17.  
  18.     private String reelOne; // Used to hold the string name of reel one
  19.     private String reelTwo; // Used to hold the string name of reel two
  20.     private String reelThree; // Used to hold the string name of reel three
  21.  
  22.     // These three arrays hold the possible positions of the reels of the slot
  23.     private String[] reelOneArray = { "black", "white", "purple", "red", "green", "yellow", "white", "orange", "red", "orange", 
  24.                                       "purple", "blue", "orange", "white", "red", "green", "purple", "blue", "yellow", "white",
  25.                                       "green", "purple", "orange", "white", "yellow", "purple", "green", "yellow", "orange", "white",
  26.                                       "green", "white", "green", "white", "orange", "black", "green", "black"};
  27.     private String[] reelTwoArray = { "black", "yellow", "green", "red", "purple", "white", "purple", "orange", "red", "orange",
  28.                                       "white", "green", "black", "red", "white", "orange", "blue", "green", "white", "yellow",
  29.                                       "blue", "purple", "green", "purple", "yellow", "white", "orange", "purple", "green", "white",
  30.                                       "orange", "yellow", "green", "orange", "green", "white", "green", "black"};
  31.     private String[] reelThreeArray = { "black", "green", "black", "orange", "white", "green", "white", "green", "white", "orange",
  32.                                         "yellow", "green", "purple", "yellow", "white", "orange", "purple", "green", "white", "yellow",
  33.                                         "blue", "purple", "green", "red", "white", "orange", "blue", "purple", "orange", "red",
  34.                                         "Orange", "white", "yellow", "green", "red", "purple", "white", "black"};
  35.  
  36.     private int randomNumOne;
  37.     private int randomNumTwo;
  38.     private int randomNumThree;
  39.  
  40.     /**
  41.      *  Constructor
  42.      */
  43.  
  44.     public AddSlotPanel()
  45.     {
  46.         // Create a GridLayout manager with
  47.         // 1 row and 3 column.
  48.         setLayout(new GridLayout(1, 3));
  49.  
  50.         boolean endOfFile = false; // End of file flag
  51.  
  52.         try // Try one
  53.         {
  54.             // Open pledges.dat as a binary file.
  55.             FileInputStream fstream = new FileInputStream("savedgame.dat");
  56.             DataInputStream inputFile = new DataInputStream(fstream);
  57.  
  58.             while (!endOfFile)
  59.             {
  60.                 try // Try two
  61.                 {
  62.                     reelOne = inputFile.readUTF();
  63.                     reelTwo = inputFile.readUTF();
  64.                     reelThree = inputFile.readUTF();
  65.  
  66.                 } // End try two
  67.                 catch (EOFException event2)
  68.                 {
  69.                     endOfFile = true;
  70.                 } // End catch
  71.             } // End While
  72.             // Close the file
  73.             inputFile.close();
  74.         }  // End Try one
  75.         catch (IOException event)
  76.         {
  77.             reelOne = "blue";
  78.             reelTwo = "red";
  79.             reelThree = "blue";
  80.         } // end catch
  81.  
  82.         // Create panel for image
  83.         reelOneImagePanel = new JPanel();
  84.         reelTwoImagePanel = new JPanel();
  85.         reelThreeImagePanel = new JPanel();
  86.  
  87.         if (reelOne.equals("blue"))
  88.         {
  89.             // Read the image file into an ImageIcon object.
  90.             ImageIcon reelOneImage = new ImageIcon("blue.png");
  91.             JLabel reelOneImageLabel = new JLabel(reelOneImage);
  92.  
  93.             //Display the image in the label
  94.             reelOneImageLabel.setIcon(reelOneImage);
  95.  
  96.             // Add image to the pane
  97.             reelOneImagePanel.add(reelOneImageLabel);
  98.         } // end if for "red"
  99.         else if (reelOne.equals("red"))
  100.         {
  101.             // Read the image file into an ImageIcon object.
  102.             ImageIcon reelOneImage = new ImageIcon("red.png");
  103.             JLabel reelOneImageLabel = new JLabel(reelOneImage);
  104.  
  105.             //Display the image in the label
  106.             reelOneImageLabel.setIcon(reelOneImage);
  107.  
  108.             // Add image to the pane
  109.             reelOneImagePanel.add(reelOneImageLabel);
  110.         } // end else if for "blue"
  111.         else if (reelOne.equals("yellow"))
  112.         {
  113.             // Read the image file into an ImageIcon object.
  114.             ImageIcon reelOneImage = new ImageIcon("yellow.png");
  115.             JLabel reelOneImageLabel = new JLabel(reelOneImage);
  116.  
  117.             //Display the image in the label
  118.             reelOneImageLabel.setIcon(reelOneImage);
  119.  
  120.             // Add image to the pane
  121.             reelOneImagePanel.add(reelOneImageLabel);
  122.         } // end else if for "yellow"
  123.         else if (reelOne.equals("purple"))
  124.         {
  125.             // Read the image file into an ImageIcon object.
  126.             ImageIcon reelOneImage = new ImageIcon("purple.png");
  127.             JLabel reelOneImageLabel = new JLabel(reelOneImage);
  128.  
  129.             //Display the image in the label
  130.             reelOneImageLabel.setIcon(reelOneImage);
  131.  
  132.             // Add image to the pane
  133.             reelOneImagePanel.add(reelOneImageLabel);
  134.         } // end else if for "purple"
  135.         else if (reelOne.equals("orange"))
  136.         {
  137.             // Read the image file into an ImageIcon object.
  138.             ImageIcon reelOneImage = new ImageIcon("orange.png");
  139.             JLabel reelOneImageLabel = new JLabel(reelOneImage);
  140.  
  141.             //Display the image in the label
  142.             reelOneImageLabel.setIcon(reelOneImage);
  143.  
  144.             // Add image to the pane
  145.             reelOneImagePanel.add(reelOneImageLabel);
  146.         } // end else if for "orange"
  147.         else if (reelOne.equals("green"))
  148.         {
  149.             // Read the image file into an ImageIcon object.
  150.             ImageIcon reelOneImage = new ImageIcon("green.png");
  151.             JLabel reelOneImageLabel = new JLabel(reelOneImage);
  152.  
  153.             //Display the image in the label
  154.             reelOneImageLabel.setIcon(reelOneImage);
  155.  
  156.             // Add image to the pane
  157.             reelOneImagePanel.add(reelOneImageLabel);
  158.         } // end else if for "green"
  159.         else if (reelOne.equals("white"))
  160.         {
  161.             // Read the image file into an ImageIcon object.
  162.             ImageIcon reelOneImage = new ImageIcon("white.png");
  163.             JLabel reelOneImageLabel = new JLabel(reelOneImage);
  164.  
  165.             //Display the image in the label
  166.             reelOneImageLabel.setIcon(reelOneImage);
  167.  
  168.             // Add image to the pane
  169.             reelOneImagePanel.add(reelOneImageLabel);
  170.         } // end else if for "white"
  171.         else if (reelOne.equals("black"))
  172.         {
  173.             // Read the image file into an ImageIcon object.
  174.             ImageIcon reelOneImage = new ImageIcon("black.png");
  175.             JLabel reelOneImageLabel = new JLabel(reelOneImage);
  176.  
  177.             //Display the image in the label
  178.             reelOneImageLabel.setIcon(reelOneImage);
  179.  
  180.             // Add image to the pane
  181.             reelOneImagePanel.add(reelOneImageLabel);
  182.         } // end else if for "black"
  183.  
  184.         if (reelTwo.equals("blue"))
  185.         {
  186.             // Read the image file into an ImageIcon object.
  187.             ImageIcon reelTwoImage = new ImageIcon("blue.png");
  188.             JLabel reelTwoImageLabel = new JLabel(reelTwoImage);
  189.  
  190.             //Display the image in the label
  191.             reelTwoImageLabel.setIcon(reelTwoImage);
  192.  
  193.             // Add image to the pane
  194.             reelTwoImagePanel.add(reelTwoImageLabel);
  195.         } // end if for "red"
  196.         else if (reelTwo.equals("red"))
  197.         {
  198.             // Read the image file into an ImageIcon object.
  199.             ImageIcon reelTwoImage = new ImageIcon("red.png");
  200.             JLabel reelTwoImageLabel = new JLabel(reelTwoImage);
  201.  
  202.             //Display the image in the label
  203.             reelTwoImageLabel.setIcon(reelTwoImage);
  204.  
  205.             // Add image to the pane
  206.             reelTwoImagePanel.add(reelTwoImageLabel);
  207.         } // end else if for "blue"
  208.         else if (reelTwo.equals("yellow"))
  209.         {
  210.             // Read the image file into an ImageIcon object.
  211.             ImageIcon reelTwoImage = new ImageIcon("yellow.png");
  212.             JLabel reelTwoImageLabel = new JLabel(reelTwoImage);
  213.  
  214.             //Display the image in the label
  215.             reelTwoImageLabel.setIcon(reelTwoImage);
  216.  
  217.             // Add image to the pane
  218.             reelTwoImagePanel.add(reelTwoImageLabel);
  219.         } // end else if for "yellow"
  220.         else if (reelTwo.equals("purple"))
  221.         {
  222.             // Read the image file into an ImageIcon object.
  223.             ImageIcon reelTwoImage = new ImageIcon("purple.png");
  224.             JLabel reelTwoImageLabel = new JLabel(reelTwoImage);
  225.  
  226.             //Display the image in the label
  227.             reelTwoImageLabel.setIcon(reelTwoImage);
  228.  
  229.             // Add image to the pane
  230.             reelTwoImagePanel.add(reelTwoImageLabel);
  231.         } // end else if for "purple"
  232.         else if (reelTwo.equals("orange"))
  233.         {
  234.             // Read the image file into an ImageIcon object.
  235.             ImageIcon reelTwoImage = new ImageIcon("orange.png");
  236.             JLabel reelTwoImageLabel = new JLabel(reelTwoImage);
  237.  
  238.             //Display the image in the label
  239.             reelTwoImageLabel.setIcon(reelTwoImage);
  240.  
  241.             // Add image to the pane
  242.             reelTwoImagePanel.add(reelTwoImageLabel);
  243.         } // end else if for "orange"
  244.         else if (reelTwo.equals("green"))
  245.         {
  246.             // Read the image file into an ImageIcon object.
  247.             ImageIcon reelTwoImage = new ImageIcon("green.png");
  248.             JLabel reelTwoImageLabel = new JLabel(reelTwoImage);
  249.  
  250.             //Display the image in the label
  251.             reelTwoImageLabel.setIcon(reelTwoImage);
  252.  
  253.             // Add image to the pane
  254.             reelTwoImagePanel.add(reelTwoImageLabel);
  255.         } // end else if for "green"
  256.         else if (reelTwo.equals("white"))
  257.         {
  258.             // Read the image file into an ImageIcon object.
  259.             ImageIcon reelTwoImage = new ImageIcon("white.png");
  260.             JLabel reelTwoImageLabel = new JLabel(reelTwoImage);
  261.  
  262.             //Display the image in the label
  263.             reelTwoImageLabel.setIcon(reelTwoImage);
  264.  
  265.             // Add image to the pane
  266.             reelTwoImagePanel.add(reelTwoImageLabel);
  267.         } // end else if for "white"
  268.         else if (reelTwo.equals("black"))
  269.         {
  270.             // Read the image file into an ImageIcon object.
  271.             ImageIcon reelTwoImage = new ImageIcon("black.png");
  272.             JLabel reelTwoImageLabel = new JLabel(reelTwoImage);
  273.  
  274.             //Display the image in the label
  275.             reelTwoImageLabel.setIcon(reelTwoImage);
  276.  
  277.             // Add image to the pane
  278.             reelTwoImagePanel.add(reelTwoImageLabel);
  279.         } // end else if for "black"
  280.  
  281.         if (reelThree.equals("blue"))
  282.         {
  283.             // Read the image file into an ImageIcon object.
  284.             ImageIcon reelThreeImage = new ImageIcon("blue.png");
  285.             JLabel reelThreeImageLabel = new JLabel(reelThreeImage);
  286.  
  287.             //Display the image in the label
  288.             reelThreeImageLabel.setIcon(reelThreeImage);
  289.  
  290.             // Add image to the pane
  291.             reelThreeImagePanel.add(reelThreeImageLabel);
  292.         } // end if for "red"
  293.         else if (reelThree.equals("red"))
  294.         {
  295.             // Read the image file into an ImageIcon object.
  296.             ImageIcon reelThreeImage = new ImageIcon("red.png");
  297.             JLabel reelThreeImageLabel = new JLabel(reelThreeImage);
  298.  
  299.             //Display the image in the label
  300.             reelThreeImageLabel.setIcon(reelThreeImage);
  301.  
  302.             // Add image to the pane
  303.             reelThreeImagePanel.add(reelThreeImageLabel);
  304.         } // end else if for "blue"
  305.         else if (reelThree.equals("yellow"))
  306.         {
  307.             // Read the image file into an ImageIcon object.
  308.             ImageIcon reelThreeImage = new ImageIcon("yellow.png");
  309.             JLabel reelThreeImageLabel = new JLabel(reelThreeImage);
  310.  
  311.             //Display the image in the label
  312.             reelThreeImageLabel.setIcon(reelThreeImage);
  313.  
  314.             // Add image to the pane
  315.             reelThreeImagePanel.add(reelThreeImageLabel);
  316.         } // end else if for "yellow"
  317.         else if (reelThree.equals("purple"))
  318.         {
  319.             // Read the image file into an ImageIcon object.
  320.             ImageIcon reelThreeImage = new ImageIcon("purple.png");
  321.             JLabel reelThreeImageLabel = new JLabel(reelThreeImage);
  322.  
  323.             //Display the image in the label
  324.             reelThreeImageLabel.setIcon(reelThreeImage);
  325.  
  326.             // Add image to the pane
  327.             reelThreeImagePanel.add(reelThreeImageLabel);
  328.         } // end else if for "purple"
  329.         else if (reelThree.equals("orange"))
  330.         {
  331.             // Read the image file into an ImageIcon object.
  332.             ImageIcon reelThreeImage = new ImageIcon("orange.png");
  333.             JLabel reelThreeImageLabel = new JLabel(reelThreeImage);
  334.  
  335.             //Display the image in the label
  336.             reelThreeImageLabel.setIcon(reelThreeImage);
  337.  
  338.             // Add image to the pane
  339.             reelThreeImagePanel.add(reelThreeImageLabel);
  340.         } // end else if for "orange"
  341.         else if (reelThree.equals("green"))
  342.         {
  343.             // Read the image file into an ImageIcon object.
  344.             ImageIcon reelThreeImage = new ImageIcon("green.png");
  345.             JLabel reelThreeImageLabel = new JLabel(reelThreeImage);
  346.  
  347.             //Display the image in the label
  348.             reelThreeImageLabel.setIcon(reelThreeImage);
  349.  
  350.             // Add image to the pane
  351.             reelThreeImagePanel.add(reelThreeImageLabel);
  352.         } // end else if for "green"
  353.         else if (reelThree.equals("white"))
  354.         {
  355.             // Read the image file into an ImageIcon object.
  356.             ImageIcon reelThreeImage = new ImageIcon("white.png");
  357.             JLabel reelThreeImageLabel = new JLabel(reelThreeImage);
  358.  
  359.             //Display the image in the label
  360.             reelThreeImageLabel.setIcon(reelThreeImage);
  361.  
  362.             // Add image to the pane
  363.             reelThreeImagePanel.add(reelThreeImageLabel);
  364.         } // end else if for "white"
  365.         else if (reelThree.equals("black"))
  366.         {
  367.             // Read the image file into an ImageIcon object.
  368.             ImageIcon reelThreeImage = new ImageIcon("black.png");
  369.             JLabel reelThreeImageLabel = new JLabel(reelThreeImage);
  370.  
  371.             //Display the image in the label
  372.             reelThreeImageLabel.setIcon(reelThreeImage);
  373.  
  374.             // Add image to the pane
  375.             reelThreeImagePanel.add(reelThreeImageLabel);
  376.         } // end else if for "black"
  377.  
  378.         // Add image to the pane
  379.         add(reelOneImagePanel);
  380.         add(reelTwoImagePanel);
  381.         add(reelThreeImagePanel);
  382.  
  383.         validate();
  384.  
  385.  
  386.     } // End AddSlotPanel Constructor
  387.  
  388.     public void playSlot(int creditPlayed)
  389.     {
  390.         // Create a Random object
  391.         Random randomNumber = new Random();
  392.  
  393.         // Get three random numbers
  394.         randomNumOne = randomNumber.nextInt(38);
  395.         randomNumTwo = randomNumber.nextInt(38);
  396.         randomNumThree = randomNumber.nextInt(38);
  397.  
  398.         System.out.println(randomNumOne);
  399.  
  400.         // Read the image file into an ImageIcon object.
  401.  
  402.  
  403.         reelThreeImagePanel = new JPanel();
  404.  
  405.             ImageIcon reelThreeImage = new ImageIcon("black.png");
  406.             //JLabel reelThreeImageLabel = new JLabel(reelThreeImage);
  407.  
  408.  
  409.             //Display the image in the label
  410.             reelThreeImageLabel.setIcon(reelThreeImage);
  411.  
  412.             // Add image to the pane
  413.             reelThreeImagePanel.add(reelThreeImageLabel);
  414.  
  415.             add(reelThreeImagePanel);
  416.  
  417.             validate();
  418.     }
  419.  
  420. } // End public class AddSlotPanel
Jan 3 '14 #1
2 1395
Nepomuk
3,112 Expert 2GB
Hi rehfeldtb and welcome to bytes.com!

What exactly is your problem? When you post code without the [CODE] tags it's very difficult to read what you've posted. Also, you told us what you're trying to do but not why it's not working. More details are essential here.
Jan 3 '14 #2
The code as is gives me the following error message:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

if I undo the comments in line 406 instead of replacing a picture it adds a fourth and fifth and so on.

I need the JLabel with the picture to erase and be redrawn with a new picture
Jan 3 '14 #3

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

Similar topics

6
by: James Nugent | last post by:
Hi everyone, My app need to manipulate JLabels that have been added to the pane. All I need to do is simple stuff like make them disappear or change their background color. Unfortunately (and...
14
by: Rajko | last post by:
I found possible bug with set icon. I'm not sure but I don't think this is supposed to work like this. Program uses large instead of small icons. Please tell me is it known bug or maybe I missed...
2
by: Egbert Nierop \(MVP for IIS\) | last post by:
Igor sorry for not being clear in my original question. I have a valid handle to an icon control (type = ICON) for a propertysheet, but when I use SetIcon, either MFC or ATL, the icon is not...
5
by: jerico | last post by:
Hi, I am developing an application which needs to dynamically show whether a host is up or down.I have used buttons on which i am pasting the icon indicating up or down.So, when a node is down, I...
5
Shinzon
by: Shinzon | last post by:
Ok so I have been working on this for a moment now and wondering how to add multiple jlabels to a jframe. the code looks like this: JLabel jl = new JLabel("DVD ID #= " + dvd); //Displays DVD...
3
nanhiPari
by: nanhiPari | last post by:
hello everyone i need to create DYNAMIC JLABElS(say 5)..yes i know it sounds easy but i also have to give them Events n then want to refer each of them when ever Mouse is Clicked on a Specific...
1
by: frozenade | last post by:
Hi.. I am realy new in Java, and I still confuse with some Java features. I have jLabel1 to display picture. I used jButton1 to change picture in jLabel1. I used this code: Image img =...
34
by: soty | last post by:
PLEASE I NEED HELP ON THIS CODE. I'M TRYING TO WRITE A CODE ON VENDING MACHINE. BUT FIRST OF ALL I NEED TO PUT PICTURES ON THE BUTTONS THAT I'M USING. THE ThING IS I'M HAVING ERRORS WHEN...
2
by: Coreyja | last post by:
Im trying to display a png image by setting it as a ImageIcon and then putting that in a JLabel. I cant get it to display the image. I am using a null layout as it is the simplest way for me to get...
0
by: UnknitSplash | last post by:
I need to clean my labelResult each time on textField Action, but on the first time it adds 'null' in front of string and then - prints new string right after. Please help. import...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.