aspose file tools*
The moose likes Beginning Java and the fly likes Need help understanding random loops. code inside Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need help understanding random loops. code inside " Watch "Need help understanding random loops. code inside " New topic
Author

Need help understanding random loops. code inside

Mack Grill
Greenhorn

Joined: Feb 27, 2012
Posts: 29
i'm trying to make 3 images become random with one of them having a string of text on it. I was curious as to if my code will make these images random and also, i'm having trouble having the image appear. I've imported them in the right way but i guess i must have formatted them wrong because i get an error messge saying that the image action variable has no value

here is the project to clarify anything:

In your applet, randomly show an image when the applet is started or restarted. Also, print a message on the applet, depending on which image is shown. Hints: the start() method would be a good place to put some sort of random flag to determine which image should be shown. It is also a good place to call a separate method to set the image and message. The paint() method should only do the actual drawing of the image and the message. Separate methods can be created to set the image and the message into variables for the paint() method to use.

sorry for not using the proper way to show code i dont know how
Rod Singh
Greenhorn

Joined: Feb 10, 2012
Posts: 16
Since you are overriding start(), add a public access modifier. In addition, use graph.drawImage() in paint() method -- to display a random image.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
Mack Grill wrote: . . . sorry for not using the proper way to show code i dont know how . . .
You can click the code button, which gets you [code=java][/code] and you put your cursor inside the pair of ][. Then you can write your code there. Or you can highlight the code and push to code button. Which is what I did. Unfortunately that improves things little because you have done the only thing worse than indenting with tabs: mixing spaces and tabs.
You are using Math.random and multiplying by 2: you need to read the API documentation to see what you get, and then work out whether multiplying by 2 is any good. You are also using a very peculiar means of choosing from three possibilities. I would have thought an array would be a better way to choose; I think I have already told you that.
Also your choice is a local variable in the start() method, which you never use in the paint() method. In fact, your choice vanishes into cyber-limbo never to be seen again, long before you get anywhere near paint().
Mack Grill
Greenhorn

Joined: Feb 27, 2012
Posts: 29


thanks
Andrew Thompson
Ranch Hand

Joined: Aug 05, 2010
Posts: 77
    
    5
Tips:

  • Load all the images in the init() and store them as class attributes in an array.
  • Also declare as an attribute of the class:
    1) A Random object .
    2) In int index.
  • In start(), call Random.nextInt() and set it as the value of the index, then call repaint().
  • In paint(), draw the image of the array that is at index.


  • Andrew Thompson
    http://pscode.org/
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 36599
        
      16
    You mean random.nextInt(int)? It should be easy enough to work out which number to put there!
    Andrew Thompson
    Ranch Hand

    Joined: Aug 05, 2010
    Posts: 77
        
        5
    Campbell Ritchie wrote:..It should be easy enough to work out which number to put there!


    Yep, that was pretty much what I was thinking. Paint with a broad stroke, presume the OP knows how to find & use the documentation.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Need help understanding random loops. code inside
     
    Similar Threads
    the display of my applet in html got problem~
    Improving Graphics.
    Improving Graphics
    Using arrays to randomly generate images? (applet)
    graphics object