my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Help with this applet! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with this applet!" Watch "Help with this applet!" New topic
Author

Help with this applet!

Neon Iyabode
Greenhorn

Joined: Mar 24, 2003
Posts: 4
I have an assignment to design an applet which generates positive integers- with a message "The Number is: "
I am not sure how to incorporate this into the drawString argument i.e. the variable which holds the number as well as how to display the message also.

Pls see below my attempt, hope its clear:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Font;
import java.io.*;
public class tma9_numbers extends java.applet.Applet
implements Runnable {
Font myFont=new Font ("Courier", Font.BOLD,24);
Thread tasker;
int i;
String j=new String();
public void start(){
if (tasker==null){
tasker= new Thread(this);
tasker.start();
}
}
public void stop(){
if (tasker!=null){
tasker.stop();
tasker=null;
}
}
public void run(){
while (true){
for(i=0;i<=100;i++)
{

}
repaint();
try {Thread.sleep(2000);}
catch(InterruptedException e){}
}
}
public void paint(Graphics g){
g.setFont(myFont);
g.drawString("The number is : " + i.toString(),10,50);
setBackground(Color.white);
}
I left the for loop blank cos i dont know what to put in it
Thanks in advance- as you have guessed am totally new to this, but its fun
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1821

Hi there, Iyabode!
Before I answer your question here, let me point out that we would appreciate it if you would change your name to conform to JavaRanch's Naming Policy. Just a friendly reminder....
(Oh, and to help us out here, if you surround your code blocks with [ CODE ] and [ /CODE ] it makes it more readable....)
Those two things being said, your code was almost there. To print an int, just include the int in the string concatenation expression. You can't call .toString() on it because it is a primitive type (not an object). Secondly, your for loop should probably contain the repaint and Thread.sleep calls, so that it repaint and sleeps on every iteration through the loop.
I made these few changes and tested it out; I got an applet that slowly counts from 0 to 100 and then starts at 0 again. (I commented out the Thread.stop() call because that method is no longer used....


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Neon Iyabode
Greenhorn

Joined: Mar 24, 2003
Posts: 4
Thanks a bunch, Joel, for your response you saved me another sleepless night
Now for my name.......it is my realname, It's a Western Nigerian name , where I am originally from, hope that clears the air.
Thanks again.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9047
    
  10
Hello, Iyabode

Welcome to JavaRanch!

You will note when you read the JavaRanch Naming Policy that two names are required -- FirstName (a space) LastName.
You can change your display name here.

Thank you.
[ May 25, 2003: Message edited by: Marilyn de Queiroz ]

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Help with this applet!