Page 1 of 3 123 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    20
    Rep Power
    0

    Need help making 3 images become random. code inside


    I'm trying to run 3 images in an applet with one of them having text on the image.

    here is the code that I have:


    import java.applet.Applet;
    import java. awt.*;

    public class MyPictures extends Applet {

    public void start(){
    int rint = (int)(Math.random() * 3);
    if (rint == 0){
    Image action = 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, 50, 50, this);
    graph.drawString("The Forest of Bent Tress", 475, 30);
    resize(1200,900);
    }
    }
  2. #2
  3. No Profile Picture
    Super Moderator
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,007
    Rep Power
    2791
    Mmm. Remember: JavaScript is not Java but they do taste similar.

    Moving to the Java forum!

    P.S. Welcome to DevShed!
    Last edited by Winters; March 8th, 2012 at 10:46 AM.
    [PHP] | [Perl] | [Python] | [Java] != [JavaScript] | [XML] | [ANSI C] | [C++] | [LUA] | [MySQL] | [FirebirdSQL] | [PostgreSQL] | [HTML] | [XHTML] | [CSS]

    W3Fools - A W3Schools Intervention.
  4. #3
  5. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Can you explain what your problem is?
    If there are errors, copy and paste the full text of the error message here.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    20
    Rep Power
    0
    I don't receive any errors but I can't make the 3 images become random

    Each time I run the applet, all I see the third picture that the paint() method displays
  8. #5
  9. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    I don't receive any errors
    The code you posted does NOT compile without errors. If it does not compile, you can not execute it.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    20
    Rep Power
    0
    i just tried executing it and and the applet started like normal.

    I need to re-write the other 2 image statements but I'm not sure how.
  12. #7
  13. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Please post your version of the program that executes. Your first post does NOT compile.
    When posting code, please Wrap your code in code tags:
    [code]
    > YOUR CODE <
    [/code]
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    20
    Rep Power
    0
    sorry about that.

    It seems I posted a code with a missing statement and that was why it would not compile

    [CODE]

    import java.applet.Applet;
    import java. awt.*;

    public class MyPictures extends Applet {

    public void start(){
    int rint = (int)(Math.random() * 3);
    if (rint == 0){
    Image action = 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, 50, 50, this);
    graph.drawString("The Forest of Bent Tress", 475, 30);
    resize(1200,900);
    }
    }
  16. #9
  17. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    You left off the end tag: [/code]

    What is all the code in the start method for if you are not going to use it?

    That is where you need to:
    1) generate a random number
    2) use that number to set the value of the image variable.
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    20
    Rep Power
    0
    sorry again

    My project said that the start() method would be a good place to use as a 'random flag' for the images but I don't know how to write the image statements to make them random so I just toyed around with it.

    I've yet to get any other images to show except the one i put in my paint() method.
  20. #11
  21. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Take the image load code out of the paint method. The code in the start() should be where you load the image.

    If you get compiler errors, post them if you want help fixing them.
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    20
    Rep Power
    0
    Do you mean like this?

    [/CODE]

    import java.applet.Applet;
    import java. awt.*;

    public class MyPictures extends Applet {

    public void start(){
    int rint = (int)(Math.random() * 3);
    if (rint == 0){
    Image action = 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.drawString("The Forest of Bent Tress", 475, 30);
    resize(1200,900);
    }
    }
    [CODE]

    When I run the code, the applet pops up but only with my string of text.

    I tried to insert the .drawImage() method after the first image action statement but I received an error saying that graph can't be resolved.
  24. #13
  25. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    The code tags are at the wrong ends. Swap them.

    Please post the full text of the error message.
  26. #14
  27. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    20
    Rep Power
    0
    Code:
    import java.applet.Applet;
    import java. awt.*;
    
    	public class MyPictures extends Applet {
    		
    		public void start(){
    			int rint = (int)(Math.random() * 3); 
    			if (rint == 0){
    			Image action = getImage(getDocumentBase(),"images/image 3.jpg");
    			graph.drawImage(action, 50, 50, this);
    			
    			}
    			else{
    				getImage(getDocumentBase(),"images/image 1.jpg");
    				
    				getImage(getDocumentBase(),"images/image 2.jpg");
    				
    				}
    			}
    	
        
        public void paint(Graphics graph) {
        	
        	graph.drawString("The Forest of Bent Tress", 475, 30);
        	resize(1200,900);
        }
    }
    I can't copy the error message but all it says is that "graph cannot be resolved"

    the error has to do with me placing the .drawImage() method right underneath the first image statement
  28. #15
  29. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    The graph variable is defined in the paint() method. That is the only place that it is known. You must call the Graphics class's drawImage method in the paint() method.
Page 1 of 3 123 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo