Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Redrawing Box

Status
Not open for further replies.

tzzdvd

Programmer
Aug 17, 2001
52
IT
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=&quot;FilledBox.class&quot; align=&quot;baseline&quot; width=&quot;80&quot;height=&quot;80&quot;>
</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
 
To do something similar in a frame (essentially the same for an applet):
thread269-315087
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top