This is part of the labels / documentation for <a href='http://jcm.chooseclimate.org'>Java Climate Model</a><hr/>

#javafuture		¨fut		§<ul>
<li>@JCM4 (and earlier) was limited to Java 1.1, due to its reliance on the old @jvm built into early web browsers.
<li> @JCM5 made a big step to Java5, running as an application to escape the constraints of the browser, and making use of many new language features, especially  the new "generics" code particularly convenient for Collections (of curves, regions, scenarios, gases, etc.), and also the new GUI features in Swing.
<li>@JCM6 is anticipated to make use of Java6, particuarly its built-in capability to interpret scripting languages (see @scripts).
</ul>

#aboutjava		¨oldJCM4 addJCM5		§Java is: 
<ul>
<li>A computer language, with an object-oriented structure similar to C++.
<li> developed by Sun microsystems.See <a href="http://java.sun.com"  target="new">java.sun.com</a>.
<li>a powerful language that can do almost anything, from scientific models to business applications, even controlling robots on mars. A particular strength of java is for web-based applications.
<li>very different from the interpreted scripting language JavaScript (although the syntax seems similar).
</ul>

££jvm
££writejava
££whyjava
££javafuture

#jvm		¨oldJCM4		§Java is semi-compiled, operating via a "Java Virtual Machine" (JVM).

  The original source code is written as plain text files (ending in .java).
  This must be compiled to java bytecode class files (ending in .class).
  These class files are then interpreted by the JVM.

  Many class files may also be packaged together in a "java archive" (.jar) file, which is compressed like zip to make it easy to send across the web.

  The class files are the same for any type of computer,  but the java virtual machine (JVM) is different and must be set up locally,   as it converts java bytecode to raw machine code, and interacts with the operating system.  In theory, this JVM arrangement means you can "write once run anywhere",   yet the calculations run much faster than directly interpreted languages such as basic or javascript.

  Originally, web browsers (Internet Explorer and Netscape) provided their own JVM's for running applets. As this led to some inconsistency, more recently most applications and browsers use the JVM ("JRE") provided by Sun. 
This continues evolving, with a new version of Java released approximately annually.  JCM5 is designed to work only with Java5 and above. 

The JVM also incorporates security checks which can restrict what an application loaded across the web can do - for instance to prevent it from loading or save files on a local disk. This makes it "safe" for applications started from web browsers. Java was also designed to make it easy to communicate across the web, with many networking and "internationalisation" features.

#whyjava		¨oldJCM4		§The original concept of JCM, up to @JCM4, was as san interactive climate model running in the web browser, without the need to download or install any big package. 

Java was chosen  because it was the only computer language which could both make big calculations efficiently, and draw vector graphics in a web browser. 
Hence this is the only way to make a climate model working fast on the web
  (note interactive tools in other languages, require that the server sends a new graphic every time you change something, which is intrinsically much slower).

Although JCM5 runs as an application rather than in the browser, it can still be started easily via JavaWebStart, with the same package working on any platform.

The modular "object oriented" structure is good for making an interactive graphical-user interface where the flow may jump about, and where components may be reused in different ways. However java has a steep learning curve, you have to construct a basic framework, before you can develop anything interesting.

#writejava		¨oldJCM4		§To write or modify JCM code, you need the **Java Development Kit*** (JDK or SDK) from Sun microsystems. You can download this from <a href="http://java.sun.com/">java.sun.com  </a> 
%%  (Note: Java was invented by Sun. Although the specification  is open so others can also develop tools, java.sun.com remains the best place to start)%%

You should also download the **Java Tutorial** from Sun. This is high-quality and comprehensive, so you do not need any big book to get started.

You are recommended to download the Integrated Development Environment **Netbeans**, either from <a href="http://www.netbeans.org/">www.netbeans.org  </a> or from Sun in one package together with the JDK. 

JCM5 is programmed using Netbeans. The Netbeans "project" of JCM5 includes various ANT (XML) scripts for compiling and organising the code, creating the JAR packages for distribution, managing documentation and todo lists etc. Since only a few "expert" programmers are likely to use these project  files, they are not provided or explained online, but can be provided on request (see @contact). 

Please note also @copyr

#getjdk		¨oldJCM4		§£>writejava

#simpleapplets		¨oldJCM4		§An interactive, object-oriented code has no simple beginning and end, but you must start somewhere.

  The best way to learn is by trial and error,
  frequently checking the API documentation that comes with the JDK
  which describes each class in detail.

  An applet is a special class that appears in a web browser.
  Here is the simplest possible:
  <pre>
  import java.applet.Applet;  import java.awt.<li>; 
  public class Myapplet extends Applet {
  public void paint(Graphics g) { g.drawString("Hello",4,20); }
  }
  </pre>

  The first line imports the necessary java packages (awt=abstract window toolkit).
  The next specifies you are adding to the existing Applet class.
  The paint method will be called by the browser which provides the graphics context "g".
  All applets must have either a paint or an init method, which must be declared "public".

  Copy the four lines above into a text file called Myapplet.java.
  Then compile this file, which should produce Myapplet.class
  Then make a very simple web page (put it in the same directory).
  <pre>

  &lthtml&gt&ltbody&gt
  Below is my applet&ltp&gt
  &ltapplet width=100% height40% code="Myapplet.class"&gt&lt/applet&gt
  &lt/body&gt&lt/html&gt

  </pre>

  Then just open that web page, in internet explorer.
  You should see it says "Hello" inside the grey rectangle.

  The example below is more fun, it makes a wavy sea with floating balls.

  <pre>

  import java.applet.Applet;  import java.awt.<li>; 
  public class Wave extends Applet {

  boolean loop; int i,x,y,w,h;

  public void paint(Graphics g) {
  w=this.size().width; h=this.size().height;
  i=0; while(loop) {
  i++; if (i&gt1256) i=0;
  for(x=0; x&ltw; x++) {
  y=(int)((0.5<li>h + (0.4<li>h)<li>Math.sin((double)(x+i)/50.0) + (0.05<li>h)<li>Math.sin((double)(x+i<li>4)/10.0) );
  g.setColor(Color.cyan); g.drawLine(x,0,x,y);
  g.setColor(Color.blue); g.drawLine(x,y,x,h);
  if (((x+(int)(1.5<li>i))%80)==0) {g.setColor(Color.yellow);g.fillOval(x-10,y-8,12,12);}
  if (((x+(int)(2.0<li>i))%150)==0) {g.setColor(Color.red);g.fillOval(x-20,y-16,20,20);}
  }}}

  public void start(){loop=true;}
  public void stop(){loop=false;}
  }

  </pre>

  Now you need this web page to run it:

  <pre>

  &lthtml&gt&ltbody&gt
  &ltapplet width=100% height40% code="Wave.class"&gt&lt/applet&gt
  &lt/body&gt&lt/html&gt

  </pre>

  Once you see what it does when you run it,
  you might be able to understand how the code works,
  by reading my notes on java syntax appended below.

  Notes specific to code above:

<li>start() and stop() are called when you open and close the page (applies to any applet)  <li>while(loop){} will keep looping so long as loop is true  <li>%80 calculates the remainder after dividing by 80  <li>the g.methods are part of java.awt.Graphics but it should be fairly obvious what they do  <li>note the type conversions (double) (int) etc.

  Changing the applet size will change the wave speed too, since the java is calculating as fast as it can. Math.sin is a rather slow function, so you could make this more efficient by storing an array of y values before the main loop.

#compiletips		¨oldJCM4		§This keeps changing with new versions of JDK, JCM etc. Please email ben@chooseclimate.org

#compiletipsold		¨oldJCM4		§Internet Explorer's JVM starts up the first time it meets an applet, and then stays open as long as Explorer does (note your desktop is also a copy of Explorer!). This JVM doesn't check whether you have changed a class file. So to force the JVM to start again after you changed a java class file, use CTRL-F5 immediately after refreshing or reopening the web page. 

 You can instead use the java appletviewer supplied with JDK, which always refreshes. However beware it doesn't understand percentage width/height in the html applet tag! 

 If the compiler reports an error, you won't get any class file, instead a text file describing the errors. 

 But if it succeeds, that doesn't mean you won't sometimes get errors, or "runtime exceptions" which may stop the applet from working. These will usually be reported in "Standard Output". In a web browser, the standard output is the Java Console, which you can find in the view menu in internet explorer (or tools in netscape). If it's not there, you need to enable it first by changing the web browser settings ("java console" or "java logging" under advanced options). The java console also catches anything you write to System.out, e.g. 
 System.out.println("Got to here OK"); 
 (you can put this into your code for debugging)

#javasyntax		¨oldJCM4		§Please see the simple introduction page found in the  jcm/code directory of the package (<a target='_new' href='../code/simplejavasyntax.html'>Here is a link to simplejavasyntax.html'</a>