Select this to skip to main content [CSUSB] >> [CNS] >> [Comp Sci Dept] >> [R J Botting] >> [CSci620] >> lab13 [Source]
[Index] [Schedule] [Syllabi] [Text] [Labs] [Projects] [Resources] [Search] [Grading]
Notes: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10] [11] [12] [13]
Tue May 11 06:55:02 PDT 2004

Contents


    CS620: Java Applets

      Check out New things on the Course Web Page

      [ http://www.csci.csusb.edu/dick/cs620/ ]

      Goals for this Lab

      Today we learn about concurrency and applets. They fit together!

      Deliverable

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

      Process

      Start from with my exiisting 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.

      Your Public Web Site

      Do not abuse this site by overloading it with large graphic and audio files. It will be removed.

      In this lab your public WWW site appears under a directory that looks like this:

        /pool/web/public/your_group/your_name  (in JBH358)
        /web/public/your_group/your_name (in JBH359)
      or (sometimes)
        /pool/web/public/your_name
        /web/public/your_name
      and your login (HOME) directory is called:
        /pool/your_group/your_name
      or perhaps
        /u/your_group/your_name

      Warning

      Your pages will not change on the web until (1)you copy new versions on top of the old ones. And (2)you hit the reload/refresh button in the browser. And sometimes (3) stopping and reatrting the browser.

      If you work directly in the public directory:
      Net

      1. People see your partly finished work.
      2. You will have no backup copy when
        1. the server is broken into.
        2. the server breaks down.


      (End of Net)

      Review Java


      1. Have a look at this page, with four Applets, code, and documentation: [ 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: [ ColoredText.html ] [ ColoredText.java ] and so the "main" Applet is made simpler: [ test.Goodbye2.html ] 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 Netscape/MS IE 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 [ http://csci.csusb.edu/public/faculty/dick/ ]

      Concurrency

        Threads in Java

        In Netscape/IE 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.

        Prime Number Sieve

        I have developed some examples of various kinds of concurrency in Java and other languages using Tony Hoare's Prime Number Sieve [ Specification ]

        You can see implementation in the UNIX shell, Ada, C, C++, Java, etc, in [ http://www.csci.csusb.edu/dick/cs320/sieve/ ]

        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 ]

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

      More Java

        More Layouts

        You may need or want to do some sophisticated graphic interfaces if so this experiment may help you.

        A layout manager automagically places some graphic parts inside a larger graphic Container. Here is an example of some of the standard layouts.


        1. (FlowLayout): Parts follow each other Left-right and "word-wrap"
        2. (GridLayout): A two dimensional array
        3. (BorderedLayout): One part in the center and one to four parts at edge
        4. (Stack of CardsLayout): Only one part is visible at a time.
        5. (GridBagLayout): This is not a simple layout. Read The Famous Sun Documentation.

        The example below came from the Java developers. I have modified it to make it a little more obvious what of going on. Warning, you will need to read, study and think about both the code and Sun's documentation: [ CardTest.java ] and [ test.CardTest.html ]

        By the way take note of the relation

         		instanceof
        in the CardTest code it allows Java to find out the type of an object at run time.

        A Graphic Applet

        Java is one of the few languages to provide the functionality needed to graphics as part of its standard library. Previous languages have left it up to others to create libraries for doing graphics. Dr. Zemoudeh has a nice example of this in the Scribble applets and applications. The source code for an application is in [ StandaloneScribble.java ] and the compiled byte-code file [ Scribble.class ] Try this out at [ Scribble.html ]

        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.

        I'm still developing the following one. On some Browsers you have to reload to get the display to update:-) [ test.Surat4.html ]

        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 [ ColoredText.html ] and [ ColoredText.java ]

        Chaos

        Here is a variation of the [ 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: [ 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.

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

      Non-Java Concurrency

        The Cryptogram Problem

        Here is another problem that has a clean solution as two concurrent processes: [ Specification ] plus a sample of test data [ test.dat ] and the design [ Design ] using XBNF.

        The code includes a Makefile [ Makefile ] , to process that can be independently compiled, and run using a pipe to connect them ( p1 | p2 ): [ p1.c ] [ p2.c ]

        It is easy to convert p2.c into a restartable function: [ p2s.c ] and so produce [ crypto.c ] or you can use the UNIX C pipe() and fork() functions [ cryptop.c ] to get two concurrent processes.

        In Ada it is easy to create a main program [ crypto.ada ] that has a package with two tasks P1 and P2.

        You might like to develop a Java translation of the original design.

      . . . . . . . . . ( end of section Non-Java Concurrency) <<Contents | Index>>

    . . . . . . . . . ( end of section CS620: Java Applets) <<Contents | Index>>

    Glossary

  1. BNF::="Backus-Naur Form", for syntax and grammar, developed by Backus and Naur.
  2. EBNF::="Extended " BNF.
  3. HTML::= "HyperText Markup Language", used on the WWW.
  4. HTML_page::syntax= "<HTML>" head body.
  5. Java::="An " OO " Language from Sun".
  6. LISP::= "LISt Processing Language".
  7. LRM::="Language Reference Manual".
  8. OO::="Object-Oriented".
  9. Prolog::="Programming in Logic".
  10. TBA::="To Be Announced".
  11. UML::="Unified Modeling Language".
  12. URL::=Universal_Resource_Locator,
  13. Universal_Resource_Locator::syntax= protocol ":" location, where
    Net
    1. protocol::= "http" | "ftp" | "mailto" | ... ,
    2. location::= O( "//" host) O(pathname).

    (End of Net)
  14. WWW::= See http://www.csci.csusb.edu/dick/cs620/, index to web site for this class.
  15. XBNF::="eXtreme" BNF, developed by the teacher from EBNF, designed to ASCII input of syntax, semantics, and other formal specifications.


Formulae and Definitions in Alphabetical Order