CM10228 / Programming Ib:   Lecture 14


Applets & The History of Java


-I. House Keeping

  1. Coursework 3 notes:
    1. The hard parts of the assignment should be:
      1. Just learning all the Java components (not that hard, just time consuming).
      2. Putting all the pieces together.
    2. Basically, nothing should be hard in particular, it's just a lot of practice, which is what you need.
  2. Time & the Programmer:
    1. When I say something is going to take 15 hours, I am assuming something like:
      1. Intend to get started the weekend after you get the assignment.  Put it off until Sunday night, work on it 4-5 hours, get stuck.
      2. Go to lab, get help with the stuff you are stuck on.
      3. Spend another 4-5 hours finishing it, maybe go to lab again if necessary.
      4. Spend a couple hours writing up all the documentation & handing it in.
    2. If you try to do it in 15 hours the day before it is due, you will probably fail.
      1. Courseworks are 50% of the marks for a double unit.  That's like 100% on your other units.
      2. There are really only three of them.
      3. You cannot get a full unit's worth of marks from three all nighters!
  3. If you are really having trouble coding, spend time with the tutors in lab!
    1. There's no reason that the tutors should ever be alone while they are in lab.
    2. They get paid for being there.
  4. People last year were overly worried about CW3.  I want to address some possible falicies:
    1. Q: CW3 involves knowing and doing too many different things!
      1. A:  CW3 only suggests doing a lot of things.  You only need to do 3 or 4 of them to get an excellent mark.
    2. Q: you need to already know Java to do well on CW3.
      1. A: No, you can learn enough to do well if you've been keeping up with lectures & you have been putting the time you are supposed to (about 7 hours a week) into the course work.
      2. A: The reason there are so many different things you can do is that you can pick which parts of the most recent content are most interesting to you & try to get extra marks with them.  Though some things are mandatory.
    3. Q:  But it does help if you already know how to do Java.
      1. A:  Yes, and it helps your mark in Spanish if you're already fluent in Spanish / go to Spain every summer.
        1. That's the way it ought to be --- the whole point of assessing coursework is to see if you know how to program, not to find out where you learned!
        2. Though that is one reason why MIT used to teach people Scheme instead of a  more popular language.
    4. Q:  Well,  I think CW3 is much too easy!
      1. A: The point isn't just to get marks.
        1. The main point is to learn.  That's why you're here.
        2. If you build something cooler than you can get marks for, you can always put it on your web page & show it off to potential employers, your parents, or (geeky) dates.
  5. The most important thing for tackling a big problem like CW3 is to develop the code iteratively.
    1. Think about what you need to do & what you want to do.
    2. Figure out a piece you can do first, that you can test without having gotten everything else working.
    3. Build that piece.
    4. Test that piece & fix it until it works.
    5. Now go back to step I.2.1!
      1. You'll have learned things about the problem and the solution, so you want to think about the problem again before you choose the next piece to do.
  6. Don't forget this is a double unit.
    1. Counts for twice as much as your other units.
    2. Should get twice as much of your time.
  7. Programming is an art.  Practicing is the only way to get better.

II. 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 20 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.
  12. See further A Brief, Incomplete, and Mostly Wrong History of Programming Languages

III. 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 15 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 then ascendent) 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 done so well more recently is largely because of Swing.
  7. People don't actually use applets that much anymore anyway:
    1. lots of shockwave, ajax
    2. can also just use java applications (like we did in the component lecture
      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

IV. 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", Niemeyer & Knudsen.
    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

    }


    /* JFrame need this, applets don't --
    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 (2004 I think) -- I had to 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. In 2006 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>
    6. In 2007 the code that didn't work in 2006 did work!  I didn't change anything.  You can see why this is just a lecture now rather than an assignment...
    7. in 2010 the 2007 code still works.
    8. Which reminds me:  Hand in assignments even if they don't work! (or are late.)  You can get a lot of credit for non-working code, and 40% of a mark on a double unit is the same as 80% on a single unit!
      1. I got 94% for a coursework that didn't work when I was a second year UG.
      2. Of course that was America, so it's more like getting a 78% here.

V. 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 parameters.
      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>.

VI. Finalé

  1. I summarized the course & told you about the exam.
  2. Java has an interesting past.
  3. 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.
  4. It's still kind of fun to have your applications on the web though.
  5. All programmers should know something about intellectual property -- it will affect your jobs.
  6. Tuesday after break is a double lecture on AI.

page author: Joanna Bryson
24 March 2010