File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with this applet!" Watch "Help with this applet!" New topic

Help with this applet!

Neon Iyabode

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;
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);
public void stop(){
if (tasker!=null){
public void run(){
while (true){

try {Thread.sleep(2000);}
catch(InterruptedException e){}
public void paint(Graphics g){
g.drawString("The number is : " + i.toString(),10,50);
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

Joined: Aug 20, 2001
Posts: 1824

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

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

Joined: Jul 22, 2000
Posts: 9059
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 ]

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
I agree. Here's the link:
subject: Help with this applet!
It's not a secret anymore!