aspose file tools*
The moose likes Beginning Java and the fly likes Need help understanding math.random method. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need help understanding math.random method." Watch "Need help understanding math.random method." New topic
Author

Need help understanding math.random method.

kvn grll
Greenhorn

Joined: Feb 23, 2012
Posts: 4


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

Joined: Oct 24, 2010
Posts: 557
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.


Learning Java using Eclipse on OpenSUSE 11.2
Linux user#: 501795
kvn grll
Greenhorn

Joined: Feb 23, 2012
Posts: 4


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

Joined: Sep 12, 2008
Posts: 507
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

Joined: Feb 23, 2012
Posts: 4


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

Joined: Sep 12, 2008
Posts: 507
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

Joined: Feb 23, 2012
Posts: 4


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

Joined: Oct 24, 2010
Posts: 557
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

Joined: Sep 12, 2008
Posts: 507
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need help understanding math.random method.