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 21 of 21

Thread: Trying to make 3 images become random in an Applet. Need help

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Trying to make 3 images become random in an Applet. Need help

    I need to have 3 images become random with one having a string of text on it. I get the Applet to run and load the image and text but when I restart, the applet just opens up the same image and string of text.
    import java.applet.Applet;
    import java. awt.*;
     
    public class MyPictures extends Applet {
    Image action;
     
    public void start(){
    action = getImage(getDocumentBase(),"");
    int rint = (int)(Math.random() * 3);
    if (rint == 0)
    {
    getImage(getDocumentBase(),"images/image 3.jpg");
    }
    else{
    getImage(getDocumentBase(),"images/image 1.jpg");
     
    getImage(getDocumentBase(),"images/image 2.jpg");
     
    }
    }
        public void paint(Graphics graph) {
         graph.drawImage(action, 10, 50, this);
         graph.drawString("The Forest of Bent Tress", 475, 30);
         resize(1200,900);
        }
    }


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

    Default Re: Trying to make 3 images become random in an Applet. Need help

    Cross posted at: Need help making 3 images become random. code inside - Dev Shed

    Does this code show an image when it is executed?
    Last edited by Norm; March 9th, 2012 at 02:14 PM.

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trying to make 3 images become random in an Applet. Need help

    not at the moment because I don't have any image placed in it but whenever I give it a specific image, the applet just shows that image and the string of text
    Last edited by slahsdash; March 9th, 2012 at 02:42 PM.

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

    Default Re: Trying to make 3 images become random in an Applet. Need help

    the applet just shows that image
    Can you post the code that displays the image?
    This code won't show any image:
    import java.applet.Applet;
    import java. awt.*;
     
    public class MyPictures extends Applet {
    Image action;
     
    public void start(){
    action = getImage(getDocumentBase(),"");
    int rint = (int)(Math.random() * 3);
    if (rint == 0)
    {
    getImage(getDocumentBase(),"http://www.javaprogrammingforums.com/images/image 3.jpg");
    }
    else{
    getImage(getDocumentBase(),"http://www.javaprogrammingforums.com/images/image 1.jpg");
     
    getImage(getDocumentBase(),"http://www.javaprogrammingforums.com/images/image 2.jpg");
     
    }
    }
        public void paint(Graphics graph) {
         graph.drawImage(action, 10, 50, this);
         graph.drawString("The Forest of Bent Tress", 475, 30);
         resize(1200,900);
        }
    }

  5. #5
    Junior Member
    Join Date
    Mar 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trying to make 3 images become random in an Applet. Need help

    I'm using the first image this time for an example

     
    import java.applet.Applet;
    import java. awt.*;
     
    public class MyPictures extends Applet {
    Image action;
     
    public void start(){
    action = getImage(getDocumentBase(),"images/image 1.jpg");
    int rint = (int)(Math.random() * 3);
    if (rint == 0)
    {
    getImage(getDocumentBase(),"images/image 3.jpg");
    }
    else{
    getImage(getDocumentBase(),"images/image 1.jpg");
     
    getImage(getDocumentBase(),"images/image 2.jpg");
     
    }
    }
        public void paint(Graphics graph) {
         graph.drawImage(action, 10, 50, this);
         graph.drawString("The Forest of Bent Tress", 475, 30);
         resize(1200,900);
        }
    }

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

    Default Re: Trying to make 3 images become random in an Applet. Need help

    You call the getImage() method in 4 places. The first place you call it you save what it returns in the action variable. For the other 3 places you ignore what getImage() returns. Try assigning what it returns to the actoin variable in all the places that you call it.

  7. #7
    Junior Member
    Join Date
    Mar 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trying to make 3 images become random in an Applet. Need help

    but how do I get the string of text to be only displayed on the third image? the text is also appearing each time i run the applet

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Trying to make 3 images become random in an Applet. Need help

    how do I get the string of text to be only displayed on the third image?l
    When you set action to the third image, you need a way to tell the paint method that the text is to be displayed. When action is set to the other images, you should tell paint not to display the text.
    One way is to use a boolean variable that you set true when the text is to be displayed and use it in paint() to control when to display the text.

  9. #9
    Junior Member
    Join Date
    Mar 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trying to make 3 images become random in an Applet. Need help

    i was given this example but even this code does not make the images random.

        import java.applet.Applet;
        import java. awt.*;
     
        public class MyPictures extends Applet {
                Image action = null;
                public void init(){
                        int rand = (int) Math.random() * 3;
                        if(rand == 0){
                                action = getImage(getDocumentBase(), "image/image3.jpg");
                        } else if(rand == 1){
                                action = getImage(getDocumentBase(), "images/image1.jpg");
                        } else {
                                action = getImage(getDocumentBase(), "images/image2.jpg");
                        }
                }
     
                public void paint(Graphics g){
                        if(action != null){
                                g.drawImage(action, 10, 50, this);
                                g.drawString("The Forest of Bent Tress", 475, 30);
                                resize(1200,900);
                        }
                }
        }

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Trying to make 3 images become random in an Applet. Need help

    this code does not make the images random.
    Please explain.
    Add a println statement immediately following the call to random() that prints out the value of rand.

    execute the program several times and see what number is printed out. Is it always the same number?

  11. #11
    Junior Member
    Join Date
    Mar 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trying to make 3 images become random in an Applet. Need help

    sorry but my course has not covered using println statements yet.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Trying to make 3 images become random in an Applet. Need help

    Here is a sample:
    System.out.println("var=" + var);
    Replace var with any variable you want to print out the value of.

  13. #13
    Junior Member
    Join Date
    Mar 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trying to make 3 images become random in an Applet. Need help

    I think it would be a bad idea to use code that I have not been taught on this project.


    Any other way around this problem?

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Trying to make 3 images become random in an Applet. Need help

    Any other way around this problem?
    Use an interactive debugger so you can see what the code is doing. You must see what the code is doing if you are going to be able to fix it. printlns are the most common way to do it.
    If you don't see what the code is doing you will NOT be able to fix it.

    What did your instructor explain to you about how to debug your program?
    What techniques were given?
    It is impossible to write a program and get it to work without being able to debug it.
    Last edited by Norm; March 9th, 2012 at 05:00 PM.

  15. #15
    Junior Member
    Join Date
    Mar 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trying to make 3 images become random in an Applet. Need help

    This was the code I sent to him and his reply was one of the first solutions you told me and nothing else was explained.

    import java.applet.Applet;
    import java. awt.*;
     
    public class MyPictures extends Applet {
     
    public void start(){
    int rint = (int)(Math.random() * 3);
    if (rint == 0)
    {
    getImage(getDocumentBase(),"images/image 3.jpg");
    }
    else{
    getImage(getDocumentBase(),"images/image 1.jpg");
     
    getImage(getDocumentBase(),"images/image 2.jpg");
     
    }
    }
        public void paint(Graphics graph) {
         Image Action = getImage(getDocumentBase(),"images/image 3.jpg");
         graph.drawImage(action, 10, 50, this);
         graph.drawString("The Forest of Bent Tress", 475, 30);
         resize(1200,900);
        }
    }

    My question to him:

    "I can not figure out how to write the image statements after the start() method.

    I think I have the right idea, but I seem to be writing the image statements wrong".

    his reply:
    "When you write the data type in front of a variable, you're declaring it. You only need to declare action once, then you can assign it any value you want.

    But in your code, every time you want to *use* action, you've written 'Image' in front of it. On line 13, Java is telling you that you're declaring action for the second time in one block".

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Trying to make 3 images become random in an Applet. Need help

    If you want to understand what your code is doing when it executes, you must debug it.
    One way is using an interactive debugger that is part of some IDEs.
    Another way is to use the println statement as I showed you earlier.

    If you don't use some technique to see what the code is doing, you will not be able to make it do what you want.
    Your choice.

  17. #17
    Junior Member
    Join Date
    Mar 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trying to make 3 images become random in an Applet. Need help

    I was able to figure out how to make the images random but now I can't get the text to only go with the bent tree image.

    here is the new code:

     
    import java.applet.Applet;
    import java. awt.*;
     
    public class MyPictures extends Applet {
    Image action;
     
    public void start(){
    int rint = (int)(Math.random() * 3);
    if(rint == 0){
        action = getImage(getDocumentBase(), "images/image 3.jpg");
     
    } else if(rint == 1){
        action = getImage(getDocumentBase(), "images/image 1.jpg");
    } else {
        action = getImage(getDocumentBase(), "images/image 2.jpg");
    }
    }
        public void paint(Graphics graph) {
        	if(action != null){
         graph.drawImage(action, 10, 50, this);
         graph.drawString("The Forest of Bent Tress", 475, 30);
         resize(1200,900);
        }
        }
    }

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Trying to make 3 images become random in an Applet. Need help

    Look back at post #8. You need a flag (a boolean variable for example) that you set when the image is the bent tree and that you can use in the paint method to control the drawing of the text.
    Very similar to how you are using the value of the action variable to control the drawing of an image.

  19. #19
    Junior Member
    Join Date
    Mar 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trying to make 3 images become random in an Applet. Need help

    The exclamation point is apparently making the image a boolean. can I use this statement as a boolean? I tried inserting the third image but it did not change anything.

    import java.applet.Applet;
    import java. awt.*;
     
    public class MyPictures extends Applet {
    Image action;
     
    public void start(){
    int rint = (int)(Math.random() * 3);
    if(rint == 0){
        action = getImage(getDocumentBase(), "/images/image 3.jpg");
     
    } else if(rint == 1){
        action = getImage(getDocumentBase(), "/images/image 1.jpg");
    } else {
        action = getImage(getDocumentBase(), "/images/image 2.jpg");
    }
    }
        public void paint(Graphics graph) {
        	if(action != getImage(getDocumentBase(), "http://www.javaprogrammingforums.com/images/image 3.jpg"));{
         graph.drawImage(action, 10, 50, this);
         graph.drawString("The Forest of Bent Tress", 475, 30);
         resize(1200,900);
        }
        }
    }
    Last edited by slahsdash; March 9th, 2012 at 06:02 PM. Reason: for some reason the url keeps getting put into the code no matter how many times i try and delete

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Trying to make 3 images become random in an Applet. Need help

    That is NOT the way to do it!!!

    You code will never show image3 .

  21. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Trying to make 3 images become random in an Applet. Need help

    Is this the same problem:
    Need help understanding random loops. code inside (Beginning Java forum at JavaRanch)

Similar Threads

  1. generate a random number from 100 to 150, inclusive in applet
    By chonch in forum Java Theory & Questions
    Replies: 12
    Last Post: February 14th, 2014, 05:44 AM
  2. Replies: 2
    Last Post: February 24th, 2012, 07:56 PM
  3. Generation of random number using random class
    By JavaPF in forum Java SE API Tutorials
    Replies: 1
    Last Post: December 7th, 2011, 05:46 PM
  4. How to make a new screen on applet.
    By pottsiex5 in forum Java Applets
    Replies: 6
    Last Post: November 23rd, 2011, 02:12 PM
  5. Generation of random number using random class
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: April 16th, 2009, 06:10 AM