short, simple applet question
Hi,
I am very new to java and am just trying to get to grips with the basics of
applets.
I currently have two problems. I have wrote a short applet that shows these
(along with the HTML code to display it). This is not the code I am working
on, but it shows the issues I am having with the other code. It compiles
fine.
By looking at the code you should be able to make out my first problem quite
easily. Here, I want to display the 'counter' variable on the screen.
However, this varaible is displayed only after the program has finished
(instead of displaying numbers from 1 to 45000 on the screen, it just goes
through the program and displays 45000 only).
My second problem is that I do not want the counter variable to ever
increase beyond 45000 (to 90000, to 135000, to 180000, etc) every time the
HTML screen is refreshed.
Thanks in advance for any suggestions you can offer:
Tom (tomee@bigfoot.com):
import java.awt.*;
public class TestLoop extends java.applet.Applet {
static int counter = 0;
public void start()
{
TestLoop loop1 = new TestLoop();
loop1.runTheLoop();
} //end of start
public void runTheLoop()
{
int index = 1;
while (index <= 45000)
{
index++;
counter++;
repaint();
}}
public void paint(Graphics screen) {
screen.drawString("The number is ... " + counter, 5, 30);
}
} // end of class
//*********** TestLoop.html ***********-
<HTML>
<HEAD>
<TITLE>Person</TITLE>
</HEAD>
<BODY>
<APPLET
CODE=TestLoop.class
WIDTH=500
HEIGHT=500>
</APPLET>
</BODY>
</HTML>
Re: short, simple applet question
your applet just does what you wanted, but before it is shown. If you want
to see the running of the number, you should do it after the applet is
shown. Many ways such as add button to trigger it, or use javascript to
trigger the applet, or ......
Not exactly understand the second question.
"Tom" <tomee@bigfoot.com> ¼¶¼g©ó¶l¥ó news:3cbfe71c$1@10.1.10.29...
>
> Hi,
>
> I am very new to java and am just trying to get to grips with the basics
of
> applets.
>
> I currently have two problems. I have wrote a short applet that shows
these
> (along with the HTML code to display it). This is not the code I am
working
> on, but it shows the issues I am having with the other code. It compiles
> fine.
>
> By looking at the code you should be able to make out my first problem
quite
> easily. Here, I want to display the 'counter' variable on the screen.
> However, this varaible is displayed only after the program has finished
> (instead of displaying numbers from 1 to 45000 on the screen, it just goes
> through the program and displays 45000 only).
>
> My second problem is that I do not want the counter variable to ever
> increase beyond 45000 (to 90000, to 135000, to 180000, etc) every time the
> HTML screen is refreshed.
>
> Thanks in advance for any suggestions you can offer:
>
> Tom (tomee@bigfoot.com):
>
>
>
> import java.awt.*;
>
> public class TestLoop extends java.applet.Applet {
>
> static int counter = 0;
>
> public void start()
> {
>
> TestLoop loop1 = new TestLoop();
> loop1.runTheLoop();
>
> } //end of start
>
> public void runTheLoop()
> {
> int index = 1;
> while (index <= 45000)
> {
> index++;
> counter++;
> repaint();
> }}
>
> public void paint(Graphics screen) {
> screen.drawString("The number is ... " + counter, 5, 30);
>
> }
>
> } // end of class
>
>
>
>
>
> //*********** TestLoop.html ***********-
>
>
> <HTML>
> <HEAD>
> <TITLE>Person</TITLE>
> </HEAD>
> <BODY>
> <APPLET
> CODE=TestLoop.class
> WIDTH=500
> HEIGHT=500>
> </APPLET>
> </BODY>
> </HTML>
>
>
>
>
>
Re: short, simple applet question
Problems:
static int counter = 0 ;
counter is shared among all class instances.
TestLoop loop1 = new TestLoop();
Browser automatically creates copy of applet
class, in this case TestLoop. Your statement
creates a second instance of the class.
I tried some threading with sleeping which
seemed to allow repainting to proceed.
My version seemed to work OK on my system.
(Ignore the deprecation error for the stop method.
It might be replaced by the interrupt method???).
Here's my version:
Hope this helps.
import java.awt.*;
public class TestLoop extends java.applet.Applet
implements Runnable
{
Thread runner ;
int counter = 0 ;
public void start ()
{
if ( runner == null )
{
runner = new Thread ( this ) ;
runner.start () ;
}
}
public void stop ()
{
if ( runner != null )
{
runner.stop () ;
runner = null ;
}
}
public void run ()
{
int index = 1;
while (index <= 45000)
{
index++;
counter++;
showStatus ( "The number is ... " + counter ) ;
repaint();
try { Thread.sleep ( 1 ) ; }
catch ( InterruptedException e ) { }
}
}
public void paint ( Graphics screen )
{
screen.drawString("The number is ... " + counter, 5, 30);
}
}
"Tom" <tomee@bigfoot.com> wrote:
>
>Hi,
>
>I am very new to java and am just trying to get to grips with the basics
of
>applets.
>
>I currently have two problems. I have wrote a short applet that shows these
>(along with the HTML code to display it). This is not the code I am working
>on, but it shows the issues I am having with the other code. It compiles
>fine.
>
>By looking at the code you should be able to make out my first problem quite
>easily. Here, I want to display the 'counter' variable on the screen.
>However, this varaible is displayed only after the program has finished
>(instead of displaying numbers from 1 to 45000 on the screen, it just goes
>through the program and displays 45000 only).
>
>My second problem is that I do not want the counter variable to ever
>increase beyond 45000 (to 90000, to 135000, to 180000, etc) every time the
>HTML screen is refreshed.
>
>Thanks in advance for any suggestions you can offer:
>
>Tom (tomee@bigfoot.com):
>
>
>
> import java.awt.*;
>
> public class TestLoop extends java.applet.Applet {
>
>static int counter = 0;
>
> public void start()
> {
>
> TestLoop loop1 = new TestLoop();
> loop1.runTheLoop();
>
> } //end of start
>
> public void runTheLoop()
> {
> int index = 1;
> while (index <= 45000)
> {
> index++;
> counter++;
> repaint();
> }}
>
> public void paint(Graphics screen) {
> screen.drawString("The number is ... " + counter, 5, 30);
>
> }
>
>} // end of class
>
>
>
>
>
>//*********** TestLoop.html ***********-
>
>
><HTML>
><HEAD>
><TITLE>Person</TITLE>
></HEAD>
><BODY>
><APPLET
> CODE=TestLoop.class
> WIDTH=500
> HEIGHT=500>
></APPLET>
></BODY>
></HTML>
>
>
>
>
>