Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Randomizing images

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Randomizing images

    Hi,

    I have a super class and an inherited class. What it is supposed to do is display random images in one applet. The images dont match the words and images dont match the wording. What am I doing wrong? Probably a few errors. Thanks.

    Super Class:

    import java.awt.Color;;
    public class Dukes {
     
        private Color noseColor = Color.red; // default Dukes have red noses
        private String action = "../../images/duke/dukeWave.gif"; //default Dukes are friendly
        private String whatDoing = "Give me something to do"; 
        private String message= "";
     
        public Dukes() 
        {
            int rint = (int)(Math.random() * 3);  // randomly generates a 0, 1, or 2
            if (rint == 0) 
            {
                noseColor = Color.blue;   // more often red by default
                action = "../../images/duke/dukeWave2.gif";
                message = "What's up with the blue nose!";
            }		
         }
     
         public String getAction()
         {
             return whatDoing;
         }
     
         public String getActionImage()
         {
             return action;
         }
     
         public Color getNoseColor()
         {
             return noseColor;
         }
     
         public String getMessage()
         {
             return message;
         }
     
         public String write()
         {
             whatDoing = "I am a writing Duke";   
             if (noseColor == Color.red)
             {
             action = "../../images/duke/penduke.gif";
             message = "";
             }
             else
             {
             action = "../../images/duke/writing.gif";
             message = "My nose feels funny";
             }
             return action;
          }
     
          public String think()
          {
             whatDoing = "I am a thinking Duke";
             if (noseColor == Color.red)
             {
             action = "../../images/duke/thinking.gif";
             message = "";
             }
             else 
             {
             action = "../../images/duke/thinking2.gif";
             message = "My nose feels funny";
             }
             return action;
          }
     
          public String wave()
          {
             whatDoing = "I am a waving Duke";
             if (noseColor == Color.red)
             {
             action = "../../images/duke/dukeWave.gif";
             message = "";
             }  
             else 
             {
             action = "../../images/duke/dukeWave2.gif";
             message = "My nose feels funny";
             }
             return action;
           }
     
     
     
     }


    Inherited class:

    import java.applet.Applet;  
    import java.awt.*;  
     
    public class ThreeDukesApplet extends Applet {  
     
        Dukes myDuke, yourDuke, theirDuke;     
        String Action, yourAction, theirAction;  
     
        public void init() {  
     
            myDuke = new Dukes();              
            Action = myDuke.getActionImage();  
     
            yourDuke =new Dukes();            
            yourAction =yourDuke.write();  
            theirDuke =new Dukes();  
            theirAction =theirDuke.getActionImage();  
     
            resize(600,400);   
     
        }  
     
       public void paint(Graphics g) {  
     
     
            switch ((int)(Math.random() * 3))  
            {  
            case 0: Action= yourDuke.write(); break;  
            case 1: Action= theirDuke.think(); break;  
            case 2: Action= myDuke.wave(); break;  
     
            }  
     
            Image myChoice = getImage(getDocumentBase(), Action);   
            g.drawString(myDuke.getAction(), 10,165);   
            g.drawString(myDuke.getMessage(), 10,180);  
            g.drawImage(myChoice, 20, 50, Color.white, this);  
     
     
            g.drawString(yourDuke.getAction(), 200,165);   
            g.drawString(yourDuke.getMessage(), 200,180);  
            Image yourChoice = null;
    		g.drawImage(yourChoice, 200, 50, Color.white, this);  
     
            Image theirChoice = getImage(getDocumentBase(), theirAction);  
            g.drawString(theirDuke.getAction(), 400, 165);  
            g.drawString(theirDuke.getMessage(),400, 180);  
            g.drawImage(theirChoice, 400, 50, Color.white, this);  
        }  
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Randomizing images

    The images dont match the words and images dont match the wording.
    Can you give some examples of the mismatches? Do the mismatches change as the code executes?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Aug 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Randomizing images

    Ok, I will get wording like waving duke but the picture above it will be a writing duke. Sometimes the writing is there but no picture. This is a class exercise from O'Rielly. Sometimes all i get is the writing.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Randomizing images

    How are you debugging it? I use println() statement to print out the values of variables as they are changed and used. If you know how the code is supposed to work, printing out what it is doing should show you what the problems are. Add lots of printlns and make sure they print a unique id String so you can tell which println printed it.
    For example:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Aug 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Randomizing images

    Can u give an example using my code?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Randomizing images

    First you need to make a list of all the tests that are displayed
    and a list of all the images.

    Then define the rules controlling when each are shown:
    How are the texts supposed to be associated with the images? Are they supposed to always be paired the same?
    Can one image have more than one text?
    Can one text be shown with more than one image?

    Now look at the code and make sure it follows the rules you've just defined.

    an example using my code?
    System.out.println("blue action="+action + ", message="+message);
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Putting images in GUI. Images in source file dont seem to be recognised.
    By nikolaiL in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 8th, 2014, 02:44 PM
  2. No images being displayed
    By Leonardo1143 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 5th, 2013, 02:57 PM
  3. Images
    By Hoshi in forum Java Theory & Questions
    Replies: 1
    Last Post: September 6th, 2012, 10:54 PM
  4. Doubling The Array Size And Randomizing Array Return
    By Pingu00 in forum What's Wrong With My Code?
    Replies: 18
    Last Post: June 27th, 2011, 10:50 AM
  5. Randomizing cells in a grid
    By Flowbs in forum AWT / Java Swing
    Replies: 3
    Last Post: December 18th, 2010, 09:53 PM