Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 31st, 2002, 05:28 PM
khfter khfter is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2002
Posts: 0 khfter User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Angry Help ! Java Random Graphics

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

Reply With Quote
  #2  
Old April 1st, 2002, 07:35 PM
p3rlm0nk p3rlm0nk is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2002
Location: Houston, TX USA
Posts: 0 p3rlm0nk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via Yahoo to p3rlm0nk
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?

Reply With Quote
  #3  
Old April 1st, 2002, 07:57 PM
p3rlm0nk p3rlm0nk is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2002
Location: Houston, TX USA
Posts: 0 p3rlm0nk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via Yahoo to p3rlm0nk
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...

Reply With Quote
  #4  
Old April 2nd, 2002, 02:41 AM
aabha aabha is offline
Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2002
Location: India
Posts: 7 aabha User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via Yahoo to aabha
u can use javax.swing.Timer class to change the shapes after 5secs...

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Help ! Java Random Graphics

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap