I'm a high school student taking AP Computer Science, and I've come across a project I'm having an excessive amount of trouble with. The goal is to visualize an insertion sort and selection sort via bar graphs in an applet, showing the bars switching places with each step of the sort. Sorting the graphs was easy, but whenever I attempt to add any kind of timer or delay, a white screen is displayed for several seconds before showing the sorted graphs, without showing any of the intermediate steps. I've tried using sleep, util.Timer, and most recently simply referencing the system clock with System.currentTimeMillis. I attempted swing.Timer but I couldn't understand how it was supposed to function.
If I understand JApplet correctly (and I may well not, which is part of why I'm asking this question), this code should show the five bars in black, then wait five seconds, then display the bars in white. Instead, it shows an entirely white screen for 5 seconds, then displays the bars in white.
public void paint (Graphics page)
{
page.setColor(Color.black);
for (int count = 0; count < 5; count ++)
page.fillPolygon(xBars[count], yBars[count], xBars[count].length);
boolean repeat = true;
int iterations = 0;
long timeLastExecuted = System.currentTimeMillis();
while (iterations < 5)
{
if ((System.currentTimeMillis() - timeLastExecuted) > 1000)
{
timeLastExecuted = System.currentTimeMillis();
iterations++;
}
}
page.setColor(Color.white);
for (int count = 0; count < 5; count ++)
page.fillPolygon(xBars[count], yBars[count], xBars[count].length);
}
This entire exercise has been incredibly frustrating as there have been no explanations of what an applet is, the structure of an applet, or how it works, only example code and explanations of methods to be used within applets. Applets, as far as I can tell, don't even have a main method, which already puts them outside my usual range of understanding. Trying to write applets is usually a matter of copying example code from the book and messing with it until it works. Any explanation of what an applet is, why anyone would ever use the ungodly things, or how the various methods work together and in which order they execute without a main method would be very welcome.