0

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.

Clifford
  • 1
  • 2
  • 1
    Just a few thoughts: I am not sure why you are calling `repaint()` from your `paint()` method. Imho this should not be required. Also waiting should not be part of the paint method. I could think of a call to repaint() after every step in your sorting algorithm. – Stefan Freitag Jan 31 '18 at 19:12
  • 1
    Start by having a look at [Concurrency in Swing](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html), [Performing custom painting](https://docs.oracle.com/javase/tutorial/uiswing/painting/index.html) and [Painting in Swing](http://www.oracle.com/technetwork/java/painting-140037.html) for more details about "why" it's not working. The basics are, painting is done within a single thread, but, the results of which aren't pushed to the hardware until AFTER the paint cycle has completed, so running the inside the paint method is stopping it – MadProgrammer Jan 31 '18 at 19:23
  • 1
    Also, don't ever call `repaint` directly or indirectly within a `paint` method, this is going to have unpleasant results – MadProgrammer Jan 31 '18 at 19:23
  • 2
    Next, have a read of [Why applets in JDK 9 are deprecated?](https://stackoverflow.com/questions/45535112/why-applets-in-jdk-9-are-deprecated), [Oracle reveals Java Applet API deprecation plan](https://www.theregister.co.uk/2016/08/24/oracle_reveals_java_applet_api_deprecation_plan/), [The clock is ticking: The Java browser plugin will be deprecated soon](https://jaxenter.com/clock-ticking-java-browser-plugin-will-deprecated-soon-131546.html) - applets are dead, time to move on – MadProgrammer Jan 31 '18 at 19:25
  • 1
    As to the solution to your problem. Without more context, I would recommend having a look at [How to use Swing Timers](https://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html) – MadProgrammer Jan 31 '18 at 19:25
  • 1
    [As an example](https://stackoverflow.com/questions/15756210/java-multiple-graphics/15756352#15756352) – MadProgrammer Jan 31 '18 at 19:29
  • *"Any explanation of what an applet is, why anyone would ever use the ungodly things"* Applets were a good option for adding interactive content into your webpage, but as per @MadProgrammer's comments you should consider not using them. Why? For security reasons they are no longer supported by browsers, and thus, are useless now... So, if you still want to work with Swing components, try using `JFrame`s and `JPanel`s instead. – Frakcool Feb 01 '18 at 16:19
  • *"Any explanation of what an applet is, why anyone would ever use the ungodly things"* - An applet executes with the context of an Applet container, typically executed by a browser plugin, this is why it doesn't have a `main` method, it's also why the `init`, `start` and `stop` methods are important. This is because the lifecycle of an applet is different then off a typical application. Also consider that a web app doesn't have a main method, but executes within a web container. Applets provided the possibilities for a reach user interface when HTML wasn't as dynamic as it is today – MadProgrammer Feb 01 '18 at 19:27
  • *"Any explanation of what an applet is, why anyone would ever use the ungodly things"* - Applets are based on the AWT/Swing API, so much of the functionality which those APIs provide can be used. It's an attempt to provide "desktop" like functionality to web based deployment. Applets also run within a tight security container, preventing them from accessing things like the file system or network. Due to security issues in the plugin, increased functionality of HTML/Javascript and a move by most browser developers to drop plugins, Applets have fallen out of favour and are now deprecated/dead. – MadProgrammer Feb 01 '18 at 19:30
  • You might also like to have a look at [Why CS teachers should stop teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/) – MadProgrammer Feb 01 '18 at 19:32

0 Answers0