[Skip Navigation] [CSUSB] / [CNS] / [CSE] / [R J Botting] /[CS320 Course Materials] [Text Version] lab/17.html Wed May 30 06:45:03 PDT 2012
Labs: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20]

Contents


    CS320: Java Applets

      Check out New things on the Course Web Page

      [ http://cse.csusb.edu/dick/cs320/ ]

      Goals for this Lab

      Today we learn about applets. You will also learn more about concurrency, exceptions, and event handling

      Deliverable

      By the deadline you should have at least one original Java applet on a HTML web page with link to the source code. I hope this will be a public web page that is linked to your CS320 web page. I expect the code to include some thing original... and your page to have notes saying what is new about it.

      You page should also have a paragraph or UML diagram that defines the relationships between these classes: Throwable, Error, Exception, RuntimeException, and IOException.

      Warning: If you show a picture of the hierarchy it must be correct UML -- and Googling will only loose points.

      Process

      Start from with my existing Java and HTML samples. Modify them. You may have to take classes and extend them to do what you'd like them to do. You can(probably will) implement existing interfaces and abstract classes. Add comments indicating what you changed.

      Study the examples and experiments below. Pick one. Download the page and source code into your working directory and make changes. When done put the test page, the source code, and the compiled applet in your public web site(below). Find the index page and add a link to it to your Java page.

      Review Java


      1. Have a look at this page, with four Applets, code, and documentation: [ ../java/java.test.html ]

      2. It is easy to extend TextDemo to do new things by over-riding the stub something with a new something in the new class. I decided to write a something that downloads the result of requesting a URL from the WWW: [ ../../testURLDemo.html ] (this is a Java2 version...and works better on some browsers than others because of security issues with accessing remote computers).

      3. Here is an updated variation of the Hello, Goodbye series that is more object-oriented. It imports a class of Colored Text Objects: [ ../java/ColoredText.java ] and so the "main" test application is made simpler: [ ../java/Goodbye2.java ] Perhaps the ColoredText class may have other uses?
        Net
          The
           		import ColoredText;
          does not seem to be compatible with all versions of Java. Very old and newer ones automatically import local classes so I had to make it into a comment:
           		//import ColoredText;
          You may hit a Java where it has to be uncommented.

        (End of Net)


      Hints


      1. Look in your Previous Lab notes and my handout.
      2. Avoid using browsers to debug applets. Include a simple main in each Applet to let you debug it as an application before publishing it.
      3. Open the Java Console before running a new Applet in a browser for the first time.
      4. Put an applet called AaA in file AaA.java.
      5. More examples [ java.html ] and TBA

      Exceptions

      Explore the Sun documentation of the Throwable hierarchy starting from [ Throwable.html ] and taking note of what is special about Error, Exception, and RuntimeException. Where does IOException fit in this hierarchy? Which class do you not catch? Which subclass of Exception do you not need catch or declare in a throws clause?

      By drawing a UML diagram or in text describe the hierarchy of Throwable classes.

      Concurrency

        Threads in Java

        In browsers make sure you open the Java Console so you can see the "System.out" before looking at the following two pages:

        Warning: close down and reopen your browser after running the second one above.

        For more on Thread see [ Thread.html ]

        Sieving for Prime numbers

        In class we used Tony Hoare's Concurrent Sieve to find prime numbers. I have a directory full of example codes in different languages. In this lab you can, if you wish, play with the Java versions (and see if the still work) [ ../sieve/index.html#Java ]

        CPU Eaters

        Java has a nasty little flaw: it is inherently multithreaded. On the other hand if you avoid Threads you tend to get blank images:

        Worse several browsers do not allow you control of these thread. If you look at a page with an Applet that has a thread that doesn't stop itself it will continue running after you leave the page. Slowly and steadily as you view badly behaved applets your CPU gets full of things you can not see or control. The cursor flickers, and so on.

        It would be unethical for me to show you a demonstration page! I have perpetrated this kind of problem in some of my code. The following shows a program that shows a blank when run as an application and grabs the CPU if run in a browser: [ ../../Surat2.java ]

        For more see, [ mbox ]

        Here [ BouncingCircle.htm ] is a better way to handle stopping an animated thread.

      . . . . . . . . . ( end of section Concurrency) <<Contents | End>>

      More Java

        Graphic Recursion

        Here is a page+applet that uses the Le Riche algorithm to plot a curve. I've used the Henrici spiral as a test case. [ ../../test.RecursiveHenrici.html ] Try it out and then modify the curve (leave Le Riche alone!).

        Entropy and Randomness

        Here is a page that demonstrates how to get Random Numbers [ ../../test.HowRandom.html ] by painting 4000 random points. Here are a some more [ ../../test.Surat.html ] that produce more or less random pointilist paintings. George Seurat's real paintings are much better than these. And his name is better when spelled write.

        The Crispy Program in Color

      1. Turn the Crispy application into an applet... with lots of Snap/Crackle/Pop in random places and different colors on the screen. [ Entropy and Randomness ] and the ColoredText objects [ ../java/ColoredText.java ]

        Chaos

        Here is a variation of the [ ../java/test.Henrici.html ] technique that shows the iteration of a 2 or 3 dimensional map that produces some chaotic type shapes, one dot at a time: [ ../java/test.IterMap.html ] (this may not work on all browsers.....). Download and test. If it works try out some changes to the code to see what happens. Look at the comments for ideas.

        The classic Scibble Applet from OReilly

        This is an applet [ Scribble.java ] so it will need a web page to test it.... can you make it work?

        Or you can compare it with a more modern version [ ScribbleApplet.htm ] using Swing.

        Setting up a Graphic User Interface using AWT Class

        This ia a very old example that uses the AWT -- "Abstract Windowing Toolkit" -- to simulate a simple user interface with several buttons and a menu. The buttons themselves select different layouts for themselves -- Flowing across a panel, in a grid, a center plus 4 borders. You can find it at [ ../java/test.CardTest.html ] and that will take you to the code and some (old) documentation.

        Be aware that modern Java programmers use later toolkits like Swing... However the ideas of assembling an interface from pieces step by step in Java is still how it is done. On the other hand action(Event, Object) is deprecated and may go away.

      . . . . . . . . . ( end of section More Java) <<Contents | End>>

    . . . . . . . . . ( end of section CS320: Java Applets) <<Contents | End>>

    Check the Preparation for next class

    [ ../18.html ]

End