Results 1 to 13 of 13
  1. #1
    drewtrcy is offline Member
    Join Date
    May 2011
    Posts
    27
    Rep Power
    0

    Default Custom icon not being displayed

    I am working on a final project for my Java class and I have searched and searched to see what I am doing wrong because I am not able to get my icon to show up in my dialog box. In order for me to finish, I need that to show. If anyone can let me know what I am doing wrong, I would be much obliged. We haven't really spent any time on this icon thing which is why I am not able to figure it out. The icon is suppose to show after the initial question of if they want to play or not so that they can guess if the next roll of the die is going to be higher or lower. after trying over a half a dozen different code, this is what I gave up on.



    import javax.swing.*;

    public class P_Final_Exam {
    public static void main(String[] args){

    //ask player if they want to play
    int answer = JOptionPane.showConfirmDialog(null, "Do you want to play?");

    if (answer == JOptionPane.NO_OPTION){
    System.exit(1);
    }
    int dice = random();
    diceRoll();
    //JOptionPane.showMessageDialog(null, random());
    }
    public static void diceRoll(){
    //dice roll

    ImageIcon icon = new ImageIcon ("icons/" + dice + ".png");

    Object[] options = {"Higher",
    "Lower",
    "Cancel"};

    int n = JOptionPane.showOptionDialog(null,
    "Will the next roll be higher?",
    "Dice Game",
    JOptionPane.YES_NO_OPTION,
    JOptionPane.QUESTION_MESSAGE,
    icon,
    options,
    options[0]);
    }

    //ask player if their guess is higher or lower

    //generate random number for die and icon
    public static int random(){
    int num;
    num = (int)(Math.random()*6) + 1;

    return num;
    }

    //calculate score

    //display final score on exit

    }

  2. #2
    drewtrcy is offline Member
    Join Date
    May 2011
    Posts
    27
    Rep Power
    0

    Default

    I have redone the code and now have this which still isn't showing the icon.


    import javax.swing.*;

    public class P_Final_Exam {
    public static void main(String[] args){

    //ask player if they want to play
    int answer = JOptionPane.showConfirmDialog(null, "Do you want to play?");

    if (answer == JOptionPane.NO_OPTION){
    System.exit(1);
    }
    int dice = random();

    //diceRoll();
    //JOptionPane.showMessageDialog(null, random());

    ImageIcon icon = new ImageIcon ("icons/" + dice + ".png");
    Object[] options = {"Higher",
    "Lower",
    "Cancel"};

    int n = JOptionPane.showOptionDialog(null,
    "Will the next roll be higher?",
    "Dice Game",
    JOptionPane.YES_NO_OPTION,
    JOptionPane.QUESTION_MESSAGE,
    icon,
    options,
    options[0]);
    }
    //public static void diceRoll(){
    //dice roll and guess



    //}

    //generate random number for die and icon
    public static int random(){
    int num;
    num = (int)(Math.random()*6) + 1;

    return num;
    }

    //calculate score

    //display final score on exit

    }

  3. #3
    wookyoo is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    Don't you get error when you run?
    cuz i coppied your code and runed but
    you actually haven't declared 'dice' in the diceRoll method.

    so you better declare again because 'dice' variable is local variable in the main method.

    or declare the 'dice' variable in class level not in the main method level

  4. #4
    drewtrcy is offline Member
    Join Date
    May 2011
    Posts
    27
    Rep Power
    0

    Default

    that has nothing to do with what I am asking, I just would like to know what I am doing wrong for my icon not to show up and if you read my code again you will see that dice = random() in the main part and that I have // in front of things that I have yet to begin working on. I am taking this one step at a time and in order for me to know what is going on I need that icon to show up. I know that the dice variable is working just fine because I have tested that out.

  5. #5
    wookyoo is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    ok i see.
    well then your program should work

    cuz i didn't change any your code and copied and runned it.
    while it runs after i check 'Yes' for the first dialog, icon is showed up in the second dialog.

  6. #6
    drewtrcy is offline Member
    Join Date
    May 2011
    Posts
    27
    Rep Power
    0

    Default

    which compiler are you using, I am using NetBeans and it isn't showing for me

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    are you sure you're looking in the right place for your images? Are you looking relative to the user directory?

  8. #8
    drewtrcy is offline Member
    Join Date
    May 2011
    Posts
    27
    Rep Power
    0

    Default

    I have the icons folder in the same folder as the .java file, is that not were it goes for it to run

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by drewtrcy View Post
    I have the icons folder in the same folder as the .java file, is that not were it goes for it to run
    No, you're probably not looking where you think you're looking. To find out where Java is looking for the image, place this in your code:

    Java Code:
    System.out.println(System.getProperty("user.dir"));
    If you also print out your icon's getWidth() method result, I'll bet it's -1.

  10. #10
    drewtrcy is offline Member
    Join Date
    May 2011
    Posts
    27
    Rep Power
    0

    Default

    I have no clue as to what you are talking about, we never got that far in the book. Our instructor only took us up to and including Chapter 6 which is single dimensional arrays. She only gave us that one line of code for the icon to use that I used.



    that little code pretty much told me that I had the folder too far in so I just brought it out one and it works, ty for that help
    Last edited by drewtrcy; 05-03-2011 at 05:13 AM. Reason: icon now shows

  11. #11
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

  12. #12
    wookyoo is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    I am using Netbeans as well.
    And My directory for the icons is
    in the project folder

    for exampe... my project folder is 'IconProject' then the icons folder is 'icons' in the project folder

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by drewtrcy View Post
    I have no clue as to what you are talking about, we never got that far in the book. Our instructor only took us up to and including Chapter 6 which is single dimensional arrays. She only gave us that one line of code for the icon to use that I used.



    that little code pretty much told me that I had the folder too far in so I just brought it out one and it works, ty for that help
    So you're not smart enough to simply try to place that line of code I supplied in your program and read what it puts out?

Similar Threads

  1. Add custom icon to JOptionPane
    By Return 0 in forum New To Java
    Replies: 12
    Last Post: 01-27-2012, 02:44 PM
  2. TextField not displayed
    By weezy2894 in forum Java Applets
    Replies: 6
    Last Post: 04-18-2011, 03:37 AM
  3. Adding another icon to an icon
    By whateverme in forum New To Java
    Replies: 11
    Last Post: 03-29-2011, 09:00 PM
  4. Custom Icon Demo
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-24-2008, 12:19 AM
  5. Custom icon for Frame
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-21-2007, 09:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •