|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
|||
|
|||
|
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 |
|
#3
|
|||
|
|||
|
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 |
|
#4
|
||||
|
||||
|
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 |
|
#5
|
|||
|
|||
|
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! |
|
#6
|
||||
|
||||
|
oohh... number generator...
I mis-interpreted it... =) If its a delay you want? perhaps you should look into using Threads |
![]() |
| Viewing: Dev Articles Community Forums > Programming > Java Development > applet number generator |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|