|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Generate data entry and reporting .NET Web apps in minutes, straight from your database. Read our FREE whitepaper “Build Web 2.0 Applications Without Hand-Coding” Download now! |
|
#1
|
|||
|
|||
|
I am a newbie of Java programming and I need help.
I am trying to write an applet which will display random shapes every 5 seconds. I know how to draw individual shapes. But i cant get the random part worin I assume my shapes needs to be in an array and i randomly choose a shape every 5 seconds. Any help would be appreciated thank you |
|
#2
|
|||
|
|||
|
i'm certainly not a java guru
But check out the javadocs (URL for the jdk 1.4 version) for java.util.Random. The nextInt(int n) method is probably what you need to generate the array indexes.
Would it be more efficient to store an array of strings representing the shapes than Shape objects? e.g. "square", "circle", etc. Pull a random element from the array (something like "String foo = shapes[rand.nextInt(shapes.length - 1)"?), then if ... else your way to the appropriate constructor and random numbers for that constructor. Then once you have the object (of whatever type), just draw it as you are now... I'm not really familar with the execution environment for applets (i code server stuff), so I couldn't tell you off the top of my head how you'd handle the 5 second update thing. Probably some sort of Thread voodoo? |
|
#3
|
|||
|
|||
|
also
See this thread about java.util.Random:
URL The gist of it being that you need to instantiate the Random object just once (say as a class variable of you applet or something), not each time the update display method is called, due to the nature of how the Random object spits out numbers. i.e.: Code:
class foo {
Random rand = new Random(optional seed here);
(array of strings for shape tokens)
(blah blah code)
(update method) {
int array_index = rand.nextInt(tie-in to shape array's length);
blah blah
}
(yet more code)
}
Come to think of it you don't need the minus one on the array's length when giving the arg to nextInt, rereading the docs it seems that the method returns an int from 0 to one less than the arg you give (i.e. it's inclusive of zero but exclusive of the arg, naturally you'll want to test this), which is exactly the behavior you want to get valid array indexes. Here's a simple little class that prints out 40 random integers from 0 to 10 using the methods described above: Code:
import java.util.Random;
public class test {
static Random rand = new Random();
public static void main(String[] args) {
int i;
for (i = 0; i < 40; i++) {
System.out.println(rand.nextInt(11));
}
}
}
(naturally, you'll have to stick that into a file called test.java to compile and test it... and it does presume the existence of a command line (dos, unix, whatever)) One advantage to java.util.Random is that it's been around since JDK 1.0 so it should run on just about any browser jvm out there... |
|
#4
|
|||
|
|||
|
u can use javax.swing.Timer class to change the shapes after 5secs...
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Java Help > Help ! Java Random Graphics |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|