• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need help understanding math.random method.

 
kvn grll
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I'm trying to make 3 random images with one having text on them on an applet. I understand how to make random numbers but images are completely new and I also have to use the start() method to use as a random flag to determine which image should be shown. I've tried searching for a solution but haven't found anything. Hope someone can help me out with this.
 
Greg Brannon
Bartender
Posts: 563
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try reading this (as far as you want), then this, and then come back with what you don't understand. Your question is very broad as is, and the little bit of knowledge you gain from those two posts may help you narrow it a bit.
 
kvn grll
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I'm using the java.applet.Applet class so I cant work with any other classes. I understand how to load the image gifs but not how to make them random. Here is the full question if it helps with clarifying:

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 hould 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


 
Saurabh Pillai
Ranch Hand
Posts: 524
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kvn grll wrote: I'm trying to make 3 random images with one having text on them on an applet. I understand how to make random numbers but images are completely new and I also have to use the start() method to use as a random flag to determine which image should be shown. I've tried searching for a solution but haven't found anything. Hope someone can help me out with this.


Please clarify one thing, you already have images and you just want to display them randomly? If yes, then you already mentioned that you know how to generate random numbers. It is easy to extend it for the images too. If it generates number N1 show image Img1 and so forth.
 
kvn grll
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


yes. I'm just trying to make them random with one showing a line of text at the bottom. How would I write the statements after the start() method?
 
Saurabh Pillai
Ranch Hand
Posts: 524
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know much about applet but I think it is not that crucial for this problem.

kvn grll wrote:yes. I'm just trying to make them random with one showing a line of text at the bottom.


I would assume that you have two elements on applet, one to display image and other to display text.

- Generate random numbers. You need 3 numbers for 3 images. Let's keep it simple for now.
- Now, YOU decide in your code, which image you want to show, when you get some number, lets say number 1. Another image when you get number 2 and the last image when you get number 3. This is important part. Do you follow this?
- Now, when you set image at the same time, you set the relevant text that you want to show.

Good luck and Welcome to Javaranch.

 
kvn grll
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I need to have the text and image on one applet. The other 2 would just be the image gifs.

So, the idea would be that each time I ran the applet, I would get shown a random image with the possibility of one having text on it.

I didn't quite follow your examples. sorry

thanks for the welcome
 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can make an image an object, and an object can be stored in an array, say imageArray[]. If you have 3 images, you'll have imageArray[0] through imageArray[2]. You can then select an image at random from the array by generating a random number from 0 through 2. I don't quite understand how the text part works in, but it seems to be associated with a specific image, so you'd test for that specific image, and if it's chosen, also display the text.

Make sense?
 
Saurabh Pillai
Ranch Hand
Posts: 524
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kvn grll wrote: I didn't quite follow your examples. sorry


Can you please be more specific about the step that you don't follow? Also, I would like to see what you have tried so far coding wise. I think I broke down the steps pretty well in my last post
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic