Hi,
I am a new Java programmer and I try to write my first applet and put it in a html page.
My first code should draw a box that change color from white to red and to blue and then stop.
Here is the code I used
(copied from a book and modified by myself)
import java.awt.*;
import java.applet.Applet;
public class FilledBox extends Applet {
Color boxColor;
public void init() {
int i;
i=0;
boolean ok = true;
while ( ok ){
i++;
if ( i < 200 ) {
boxColor = Color.white;
}
else
if ( i<400 ) {
boxColor = Color.red;
}
else
if ( i<600 ) {
boxColor = Color.blue;
}
else ok = false;
}
}
public void paint(Graphics g) {
g.setColor(boxColor);
g.fillRect (0,0,getSize().width,getSize().height);
}
}
and this is the html code of the applet
<applet code="FilledBox.class" align="baseline" width="80"height="80">
</applet>
There is a problem: the box appears grey and it doesn't change color. Also, looking at the status bar, seems that there is something else to download (now it is more than to minutes and it is still working) as the page is not finished.
The applet was correctly compiled and downloaded (2k)
In VisualC++, I used the funciont Invalidate() to force the call to the member Paint(). I'have read a past thread published in this forum that suggests to use the function invalidate() too (I put this function in the while statement) but it doesn't work.
Any Idea?
Is it possible to use a timer that wake up every (ex.) 1 second and change the color?
How can I use this timer?
Thank's for All
Davide
I am a new Java programmer and I try to write my first applet and put it in a html page.
My first code should draw a box that change color from white to red and to blue and then stop.
Here is the code I used
(copied from a book and modified by myself)
import java.awt.*;
import java.applet.Applet;
public class FilledBox extends Applet {
Color boxColor;
public void init() {
int i;
i=0;
boolean ok = true;
while ( ok ){
i++;
if ( i < 200 ) {
boxColor = Color.white;
}
else
if ( i<400 ) {
boxColor = Color.red;
}
else
if ( i<600 ) {
boxColor = Color.blue;
}
else ok = false;
}
}
public void paint(Graphics g) {
g.setColor(boxColor);
g.fillRect (0,0,getSize().width,getSize().height);
}
}
and this is the html code of the applet
<applet code="FilledBox.class" align="baseline" width="80"height="80">
</applet>
There is a problem: the box appears grey and it doesn't change color. Also, looking at the status bar, seems that there is something else to download (now it is more than to minutes and it is still working) as the page is not finished.
The applet was correctly compiled and downloaded (2k)
In VisualC++, I used the funciont Invalidate() to force the call to the member Paint(). I'have read a past thread published in this forum that suggests to use the function invalidate() too (I put this function in the while statement) but it doesn't work.
Any Idea?
Is it possible to use a timer that wake up every (ex.) 1 second and change the color?
How can I use this timer?
Thank's for All
Davide