Java Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJava Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today!
  #1  
Old February 2nd, 2004, 09:20 AM
aloo aloo is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 26 aloo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
applet number generator

Hi Everyone

im new to java so any help will be much appreciated. im trying to create an applet which generates positive integers (1,2,3....) and displays each one, together with a message, at 2 second intervals. i need to use clipping to minimise flicker. below is what i have done so far.

the output needs to look somthing like this:
The number is: 3

import java.awt.*;

public class NumberGenerator extends TaskingApplet
{
Font theFont;
FontMetrics fm;
int stringwidth, stringheight, count;

public void init()
{
theFont = new Font ("Courier", Font.BOLD, 24);
setForeground(Color.blue);
fm = getFontMetrics (theFont);
String str="12345";
count = count + 1;
stringheight = fm.getHeight();
stringwidth = fm.stringWidth(str);
}

public void run()
{
while (true)
{
count = new count();
repaint();
try {Thread.sleep (1000);}
catch (InterruptedException e) { }
}
}

public void update (Graphics g)
{
g.setColor(getBackground());
g.clipRect(10, 50 - stringheight, stringwidth, stringheight);
g.fillRect(0, 0, this.size().width, this.size().height);
g.setColor(getForeground());
paint(g);
}

public void paint (Graphics g)
{
g.setFont (theFont);
g.drawString (count.toString(), 10, 50);
}
}

thanks

Reply With Quote
  #2  
Old February 6th, 2004, 08:33 PM
FrankieShakes FrankieShakes is offline
Frank The Tank!
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Jun 2002
Location: Toronto, Canada
Posts: 1,246 FrankieShakes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
Send a message via ICQ to FrankieShakes Send a message via MSN to FrankieShakes
Aloo,

Where are you having problems? Could you further explain yourself... I don't see the code for actually looping through and displaying your integers... Is this what you're looking for?

I see a string object which stores "12345"... If this is what you're using to display the integers, may I recommend using an array instead. Looping through the array will be much easier...
__________________
____________________________________________
Developer Shed Weekly Writer | DevArticles Forum Moderator
Build Your Own KlipFolio Klip With PHP
FrankManno.com - Under Construction
Design Interactive Group - Under Construction

Reply With Quote
  #3  
Old February 8th, 2004, 12:44 PM
aloo aloo is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 26 aloo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
thanks for for your reply

yes i need some sort of array. but im new to java and still learning. could you post a similiar example which i could follow.

thanks

Reply With Quote
  #4  
Old February 9th, 2004, 08:43 PM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 4 m 48 sec
Reputation Power: 8
Here's a quick example of how to generate ten random numbers between 0 and 100

Code:
Random rgen = new Random();
int[] nums = new int[10];  

for (int i=0; i<10; i++) {
	nums = rgen.nextInt(100);
}


If you need help with arrays, I suggest you [i]Google
for any simply java tutorials.

A couple things to notice... the nextInt(int n) function returns a number between 0 and n...
The Random class is a part of the java.util.Random package

More information here:
http://java.sun.com/j2se/1.4.2/docs...til/Random.html

Reply With Quote
  #5  
Old February 10th, 2004, 06:17 AM
aloo aloo is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 26 aloo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Smile

hello

thanks for your help!

i don't need the numbers to be random what im trying to achieve is more of a count. i simply need numbers 1 to 10 to be displayed with the numbers increasing every 2 seconds, for example the output should be:

The number is: 1
(2 seconds later)
The number is: 2
(2 seconds later)
The number is: 3

the numbers are just changing and counting upto 10.

the exercise im working on is to do with animation and reducing flicker using clipping.
if you can guide me further on this will be much appreciated!
Thanks again for your help!

Reply With Quote
  #6  
Old February 10th, 2004, 10:43 AM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 4 m 48 sec
Reputation Power: 8
oohh... number generator...
I mis-interpreted it... =)

If its a delay you want?
perhaps you should look into using Threads

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJava Development > applet number generator


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 | 
  
 

Iron Speed




© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway