CM10228 / Programming Ib:   Lecture 15


Applets & The History of Java

(ran long in 2012, a couple minutes long in 2013, self monitor, don't draw figure, show it.)

-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 in 2012 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. Programming is an art.  Practising 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. it probably has something to do with marketing.
  5. Story about Lisp
    1. Lisp Machines -- 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. Story about Objective C
    1. Allegedly written to impress Adele Goldberg.
    2. Wound up impressing Steve Jobs.
    3. NeXTSTEP brilliant, used to write first draft of many games.
    4. Next folds (problem micromanaging supply line?)
    5. Jobs saves NeXTSTEP by killing huge multi-year, dozens-of-programmers Apple OS project.
  7. 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. dominated market for a while in servers
  8. 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.
  9. 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.
  10. Microsoft knew this was the purpose of Java (everyone did)
    1. embrace, extend, extinguish
      1. Microsoft slogan about "Netsscape, Java & The Internet"/ standards
      2. Became public due to lawsuits / testimony.
    2. made a version of "Java" 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.
    4. There were two lawsuits!  Don't be confused! 
      1. Microsoft were found guilty of general anticompetitive behaviour (being a monopoly) in a lawsuit brought by the US Government.
      2. Sun sued Microsoft over their special version of Java (see below) and Microsoft settled out of court, including discontinuing their Java VM.
  11. Are monopolies bad?
    1. Aren't they 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.
      1. Need to maintain a diverse, creative marketplace so in the position to do the next big thing.  Variation is necessary for evolution / learning through competition.
      2. Microsoft "saved" (supported) Apple at one point to help themselves out of their antitrust problems, (Apple didn't need the money really & bought the stock back shortly thereafter.)
    5. This is kind of like the offside rule in football.
    6. 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.
    7. 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.
        1. Politics is not just about corruption &/or fame!
        2. It really is about managing contributions to the public good.
  12. 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 + ~50-70 years (keeps changing, lots of legislation).
    4. patents monopolize an entire problem for a long time, stifles other innovators.
      1. patents monopolize a process, an entire solution. 
        1. Do not patent a program,  patent a problem solved – part of a program,
        2. May be more than one patent in a program.
      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.
        1. could argue for copyright, but doesn't do much good since easy to change a few things & yet maintain functionality.
        2. Often companies redescribed the algorithm as a hardware circuit & patented that!
      4. recently (last few decades) patent offices have been allowing software patents.
      5. But in June 2014, the US Supreme Court reaffirmed that you cannot patent "abstract ideas", and nullified a patent on a SMOP of an abstract idea.
    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 and/or opensource software
        1. viral:  if you include copylefted software, your software must be free too.
        2. not all free software has a copyleft statement!  
          1. Wikipedia page listing & comparing free and open source licenses.  As of 2013, over 14 listed.  
          2. I use the MIT / X11 one.
        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.
  13. 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. This is one of the reasons Apple is more usable than Microsoft or Android.
    4. However, users are much more experienced / less conservative now than they were 20 years ago.
    5. Though the average user still has trouble with Google, autocomplete.
  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 very portable!
      1. the whole point of Java!
      2. Basically AWT is a big C program with a Java API.
    4. Also, AWT used to be really buggy.
  3. Applets were supposed to be the "killer ap" for Java.
    1. (the whole term "killer ap" sounded really outdated when I was first lecturing at Bath, now the term "ap" is cool again)
    2. "Dynamic content" for the web.
    3. Netscape agreed to support them (dominant browser at the time)
    4. MS was forced to do so too (then Netscape did Embrace, Extend!)
    5. But Netscape insisted on AWT / native support.
    6. When Java finally fixed applets with Swing, MS (by then ascendant) refused to support the new standard.
  4. Swing is entirely written in Java, so really is portable.
    1. Also, just a lot of bugs got fixed.
  5. The reason Java has been done so well more recently is largely because of Swing.
  6. People don't actually use applets that much anymore anyway:
    1. used to be lots of shockwave, then ajax, flash, maybe next HTML 5
    2. can also just use java applications (like we did in the component lecture
      1. Java Web Start -- another API.
    3. But applets used to be a part of the course contents, & are still history, 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 a 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. in 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 & 2012 the 2007 code still works.  Sad that this is news.
    8. In 2013 it wasn't working or giving any clues why, but then I installed Java 7 to make the component lecture work, and THEN it worked after I said it was OK to run it with Java 7 – the first year that there was a pop-up to handle an applet's security...
      1. 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.
    9. In 2014 I couldn't get it to work, but in 2015 browsers started more-or-less supporting your ability to override their security.
      1. Using Mozilla (but not Chrome) I was able to get an error message that lead me to the control panel that let me set http://www.cs.bath.ac.uk/~jjb as a trusted site.
      2. Using Chrome (but not Mozilla) I was able to also tell Java I really truly did trust this BangPopApplet.
        1. Oh, now it is working in Mozilla (SeaMonkey) :-S
        2. I think the trick is to reload the page after you've changed the security for the laptop.
        3. Here's the Java web page my browsers directed me to.

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. 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.
  5. 2013: Richard Stallman is lecturing at 6pm today (21 Feb 2013) in the EB!

page author: Joanna Bryson
24 March 2015