CM10135 / Programming II:   Lecture 13


Applets & The History of Java


I. History of Java / Intro to Intellectual Property

  1. Where do languages come from?  
  2. Someone invents them to fill a need.
  3. Why do languages become popular?
  4. No one's sure
    1. many excellent languages die,
    2. many mediocre languages get cursed for generations,
    3. but it probably has something to do with marketing.
  5. Story about Lisp
    1. Lispmachines -- easy to program, great language, nice development environment.
    2. "not efficient" -- everyone starts using C,
    3. Lucid makes an efficient lisp, but doesn't care about development environment,
    4. everyone buys the efficient lisp, can't figure out how to use it.
    5. lisp seriously loses market share.
  6. Java was written by people at Sun.
    1. hardware company, didn't try to make much money off software.
    2. "Sun is networking", uses Unix, sells relatively expensive computers
      1. first workstations,
      2. servers
  7. Sun were worried about a certain monopoly,
    1. most popular software was only getting written for one OS.
    2. It wasn't theirs!
    3. Many people buy machines for software.
  8. Sun decided to write a platform-independent language & give it away free so they could keep selling machines.
    1. Had to be the best thing out there so everyone would use it.
  9. Microsoft knew this was the purpose of Java (everyone did)
    1. embrace, extend, extinguish (slogan about standards that came out during lawsuits)
    2. made a version that was different from everyone else's, so not really platform independent.
    3. "since java isn't an open standard, we couldn't help improve it because it's maker's didn't want our help" Bill Gates at IJCAI 2001 after losing Sun/Java lawsuit.
  10. Are monopolies bad?
    1. Maybe they are the reward to innovators for being the best?
    2. It is important for people to be rewarded for excellence -- they try harder.
    3. Generally, transient monopolies are seen as OK / benign.
    4. But if you use your strength to keep your position by damaging your opponents rather than by improving your own product, that's considered bad for society as a whole.
    5. Making & enforcing this discrimination is very, very hard:
      1. not always obvious to people from outside the industry,
      2. some people inside the industry may make false accusations,
      3. monopolies are often very, very powerful; excellent lawyers.
    6. Governing is very hard work
      1. even in the machine sense, let alone for enormous societies.
      2. public service really is a noble profession.
      3. political problems are interesting & hard.
  11. Related issue:  is intellectual property good or bad?
    1. rewards innovation, lets innovators innovate more.
    2. nothing unethical about being able to support yourself, be financially independent.
    3. copyright is what you get for novels, movies
      1. only protects the actual item, mostly used for art.
      2. lifetime of creator + ~40 years.
    4. patents monopolize an entire problem for a long time, stifles other innovators.
      1. patents monopolize a process, an entire solution.  
      2. Cannot sell different, competing means of solving the same process until patent expires.
      3. Can develop such means, use in academia, wait for patent to expire.
      4. tend to be around 7-14 years -- forever in software / high tech terms.
        1. laws were created to protect manufacturing, which used to take a while to set up.
    5. again, very complicated, hard to legislate.
      1. math & algorithms & facts of nature (e.g. physics discoveries) cannot be patented or copyrighted.
      2. considered to belong to everyone, discovered not invented.
      3. for a long time, software was considered an algorithm, so not IP.
      4. could argue for copyright, but doesn't do much good since easy to change a few things & yet maintain functionality.
      5. recently (last few decades) patent offices have been allowing software patents.
    6. Richard Stallman, copyleft
      1. Stallman's ideal:  sell service (of writing a good program), not algorithms.
        1. Make money by creating programs company needs,
        2. but afterwards, no one owns the program.
        3. Can't get as rich as Bill Gates this way, but can make a fair amount of money as a consultant.
      2. copyleft scares companies off of free software
        1. viral:  if you include copylefted software, your software must be free too.
        2. not all free software has a copyleft statement! 
        3. many people are happy to help anyone, even those who make money.
      3. most companies are getting smarter about this now,
        1. less afraid of free software, will check license.

II. What this has to do with GUIs:  Applets, AWT & Swing

  1. For a while, it was considered very important to have applications look very uniform with the rest of the OS.
    1. Otherwise users might get "confused"
    2. Probably more true of functionality than exact appearance.
    3. Users are anyway much more experienced now than they were 10 years ago.
  2. AWT (Abstract Windowing Toolkit) uses native code on the OS
    1. allows it to be a little faster
    2. allows it to look exactly like the other application, but, 
    3. not as portable!
    4. Basically a big C program with a Java API.
    5. Also, AWT used to be really buggy.
  3. Applets were supposed to be the "killer ap" for Java.
    1. "Dynamic content" for the web.
    2. Netscape agreed to support them (dominant browser at the time)
    3. MS was forced to do so too.
    4. But Netscape insisted on AWT / native support.
    5. When Java finally fixed applets with Swing, MS (by the ascended) refused to support the new standard.
  4. Swing is entirely written in Java, so really is portable.
  5. Also, just a lot of bugs got fixed.
  6. The reason Java has been doing so well lately is largely because of Swing.
  7. People don't actually use applets that much anymore anyway:
    1. lots of shockwave
    2. can just use java applications (like I was this morning) 
      1. Java Web Start -- another API.
    3. But applets part of the course contents, so I'll tell you about it quickly.
    4. Sun has made a Java plug-in, so runs a uniform, up-to-date VM for all browsers.
java inheritance network

III. Applets

  1. Part of a web page
  2. Owns some rectangular area of the user's screen
    1. draw what it wants
    2. respond to KB, mouse input.
    3. webpages know how to load the classes for applets & run them.
  3. "A panel with a mission" N&K
    1. GUI container with extra structure to run on alien environment.
    2. you could also see the applet as the alien -- system must protect itself from applet's code since could be downloaded from anywhere/anyone.
  4. HTML code is pretty simple:
    <APPLET code="BangPopApplet" width="100" height="100"></APPLET>
  5. Arbitrary size
    1. often small, but only limit is download time.
    2. could be any application.
  6. Applet Life Cycle
    1. class contains 4 methods that can be overridden,
      1. init()  -- use this instead of constructor, because constructor called too early to be useful.
      2. start()  -- called when applet becomes visible
      3. stop()  -- called when you can't see applet (refrigerator light thing).  supposed to save CPU, but you can let the applet keep running when it's not visible if you want to.
      4. destroy() -- called by browser when sure you aren't going back to the applet, e.g. before deleting it from the cache.  Not predictable!
  7. Here's an applet version of the BangPopAp from Lecture 12:
    import java.awt.Container;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    import javax.swing.JApplet;
    import javax.swing.JButton;
    import javax.swing.JOptionPane;

    /**
    * @author joanna
    *
    * Applet version, from Barbara Johnston's "Java Programming Today"
    */
    public class BangPopApplet extends JApplet implements ActionListener {

    JButton bangButton = new JButton ("Bang");
    JButton popButton = new JButton ("Pop");

    // public BangPopAp() { // JFrame
    //this.setTitle("Bangs and Pops!"); //JFrame

    public void init () { // JApplet

    Container canvas = getContentPane();
    canvas.setLayout (new GridLayout(2,1));

    canvas.add(bangButton);
    canvas.add(popButton);

    bangButton.addActionListener(this);
    popButton.addActionListener(this);

    this.setSize(250,150); // not nec for applet?
    // this.show(); // not meaningful for applet

    }


    /* If JFrame need this
    public static void main(String[] args) {
    BangPopAp theApp = new BangPopAp();
    theApp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    */
    public void actionPerformed(ActionEvent e) {
    if (e.getSource()==bangButton) {
    JOptionPane.showMessageDialog(this, "Bang!");
    } else if (e.getSource()== popButton) {
    JOptionPane.showMessageDialog(this, "Pop!");
    }
    }
    } // class
  8. Not much difference!
  9. Now to see if we can see it:
    If you can see this text your Browser has Java issues
  10. OK, to get that working I had to:
    1. Make sure there was no package info in the top of my file (there had been & that confused it to death)
    2. compile it with javac, produce a .class file
    3. stick the class file in the same directory as this page, and
    4. last year -- insert this code in my html:
      <applet code="BangPopApplet" width="200" height="200"> If you can see this text your Browser has Java issues</applet>
    5. This year that didn't work!  I had to tell it which directory, and then I had another security bug...
      <applet code="./BangPopApplet" width="200" height="200"> If you can see this text your Browser has Java issues</applet>


IV. Security & Other Miscellanea

  1. Applets run in a "Sandbox" inside the browser.
    1. Quarantined by the SecurityManager class.
  2. Most browsers don't allow untrusted applets do the following:
    1. read or write files on the local host
    2. open sockets / network connections other than to the server from which they originated.
    3. start other processes on the local host
    4. run native (OS) methods.
  3. There is a way to establish trust & get rid of these restrictions using digital signatures, but I don't know anything about it myself.
  4. You can pass your applet parameters inside the HTML 
    1. by putting <param> tags inside the <applet> ones, 
      1. <param name="lives" value="9">
      2. can have multiple ones.
      3. then use getParameter("lives") (an Applet method) from in the java to get the value (in this case, 9).
      4. may need exception handling in case the parameter values are not valid / got munged.
  5. Other useful methods:
    1. getDocumentBase() returns base URL where the document is
    2. getCodeBase() returns base URL of the Applet's class files (often the same as getDocumentBase)
    3. getImage(URL) can get an image...
    4. guess what getAudioClip(URL) does...
  6. Other arguments to <applet>
    1. besides width & height, shown above.
    2. codebase, archive, name, alt, align, vspace, hspace
      1. codebase is probably the most useful, let's you put the code in it's own directory (as long as it's still accessable to the web server.)
      2. name, alt, align, vspace, hspace are all just inherited from standard HTML tags, like <a></a>.

V. Summary

  1. Java has an interesting past.
  2. Applets were supposed to be the killer ap, but Java managed to survive despite their issues.
    1. Thanks to some litigation,
    2. and the invention of Swing.
  3. It's still kind of fun to have your applications on the web though.
  4. All programmers should know something about intellectual property -- it will affect your jobs.

page author: Joanna Bryson
13 March 2005