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

    Default icon does not change when I want it too

    :confused:

    I am getting so frustrated with this code. My icon does change but not when I want it to. How do I fix that so it will change each time a new roll is made after the initial roll?


    code where I need icon to change at

    Java Code:
    guess = JOptionPane.showOptionDialog(null,
        "Will the next roll be higher?",
        "Dice Game",
        JOptionPane.YES_NO_CANCEL_OPTION,
        JOptionPane.QUESTION_MESSAGE,
        icon,
        options,
        options[0]);
    
          if (guess == JOptionPane.CANCEL_OPTION){
              exit();
                System.exit(1);
          }else if (currentRoll == lastRoll){
    
       //current roll is equal to last roll with either guess
          JOptionPane.showMessageDialog(null,
          "Your guess was neither correct nor incorrect.  Score will remain the same.",
          "Dice Game",
          JOptionPane.INFORMATION_MESSAGE,
          icon);
      }else if (currentRoll > lastRoll){

    full code:

    Java Code:
    package Final;
    
    import javax.swing.*;
    
    public class P_Final_Exam {
        //declaring main variables
        static int currentRoll;
        static int lastRoll;
        static int score;
        static int guess;
    
        public static void main(String[] args){
    
        //ask player if they want to play
            int answer = JOptionPane.showConfirmDialog(null, "Do you want to play?",
                    "Dice Game",JOptionPane.YES_NO_CANCEL_OPTION,
        JOptionPane.QUESTION_MESSAGE);
    
            if (answer == JOptionPane.NO_OPTION){
                System.exit(1);
            } else if (answer == JOptionPane.CANCEL_OPTION){
                System.exit(1);
            }
    
            //JOptionPane.showMessageDialog(null, random());
            //System.out.println(System.getProperty("user.dir"));
    for(;;){
    
    currentRoll = random();
    
        ImageIcon icon = new ImageIcon ("icons/" + currentRoll + ".png");
        Object[] options = {"Higher",
                        "Lower",
                        "Cancel"};
    
    guess = JOptionPane.showOptionDialog(null,
        "Will the next roll be higher?",
        "Dice Game",
        JOptionPane.YES_NO_CANCEL_OPTION,
        JOptionPane.QUESTION_MESSAGE,
        icon,
        options,
        options[0]);
    
          if (guess == JOptionPane.CANCEL_OPTION){
              exit();
                System.exit(1);
          }else if (currentRoll == lastRoll){
    
       //current roll is equal to last roll with either guess
          JOptionPane.showMessageDialog(null,
          "Your guess was neither correct nor incorrect.  Score will remain the same.",
          "Dice Game",
          JOptionPane.INFORMATION_MESSAGE,
          icon);
      }else if (currentRoll > lastRoll){
    
       //current roll is higher than last roll and guess was higher
          if (guess == JOptionPane.YES_OPTION){
          JOptionPane.showMessageDialog(null,
          "Your guess was right.  Your score has increased by 1.",
          "Dice Game",
          JOptionPane.INFORMATION_MESSAGE,
          icon);
              rightAnswer();
          }else {
    
       //current roll is higher than last roll and lower was guessed
          JOptionPane.showMessageDialog(null,
          "Your guess was wrong.  Your score has decreased by 1.",
          "Dice Game",
          JOptionPane.INFORMATION_MESSAGE,
          icon);
              wrongAnswer();
          }
      }else {
          if (guess == JOptionPane.NO_OPTION){
    
        //current roll is lower than last roll and lower was guess
          JOptionPane.showMessageDialog(null,
          "Your guess was right.  Your score has increased by 1.",
          "Dice Game",
          JOptionPane.INFORMATION_MESSAGE,
          icon);
              rightAnswer();
          }else {
    
        //current roll is lower than last roll and higher was guessed
          JOptionPane.showMessageDialog(null,
          "Your guess was wrong.  Your score has decreased by 1.",
          "Dice Game",
          JOptionPane.INFORMATION_MESSAGE,
          icon);
              wrongAnswer();
          }
      }
    
    //asking player if they want to play again
      answer = JOptionPane.showConfirmDialog(null, "Do you want to play again?",
                    "Dice Game",JOptionPane.YES_NO_OPTION,
        JOptionPane.QUESTION_MESSAGE);
    
            if (answer == JOptionPane.NO_OPTION){
                exit();
            }else{
          lastRoll = currentRoll;
            }
        }
    } 
    public static int random(){
        //generate random number for die and icon
       int num;
       num = (int)(Math.random()*6) + 1;
    
       return num;
    }
    public static int rightAnswer(){
        //add to score
        score++;
        return score;
    }
    public static int wrongAnswer(){
        //subtract from score
        score--;
        return score;
    }
    public static int sameAnswer(){
        //do nothing to score
        return score;
    }
    public static int exit(){
        //display final score on exit
        JOptionPane.showMessageDialog(null,
          "Thank you for playing.  Your score is " + score +".",
          "Dice Game",
          JOptionPane.INFORMATION_MESSAGE);
           System.exit(1);
        return exit();
        }
    }

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I'm not entirely sure what you mean, how does it change currently? How do you want it to look?

Similar Threads

  1. change icon of frame?
    By Manish87 in forum AWT / Swing
    Replies: 9
    Last Post: 04-15-2011, 04:11 AM
  2. Netbeans6.8 .jar file icon change
    By avixt in forum AWT / Swing
    Replies: 3
    Last Post: 04-08-2011, 09:34 PM
  3. Swing: Change the icon on a JComponent
    By xytho33 in forum Advanced Java
    Replies: 1
    Last Post: 09-23-2010, 06:53 AM
  4. How to change Window Icon
    By sharafat in forum AWT / Swing
    Replies: 7
    Last Post: 01-15-2010, 12:37 AM
  5. Icon change and lib folder problem
    By LeonLanford in forum New To Java
    Replies: 10
    Last Post: 10-21-2009, 04:13 PM

Posting Permissions

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