This is an example of getting a Scala applet running in your browser, and incidentally it shows with what minimal changes you can port at least simple code, from Java with. This example is from O'Reilly's 'Learning Java', it doesn't do much, and not everything there does something (it's meant to get improved as the chapter goes along), but it's a start.
First, the Java version, ShowApplet.java:
import javax.swing.*;
import java.awt.event.*;
public class ShowApplet extends JApplet {
JTextArea text = new JTextArea();
int startCount;
public void init() {
JButton button = new JButton("Press here!");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
text.append("Button Pressed!\n");
}
} );
getContentPane().add("Center", new JScrollPane(text));
JPanel panel = new JPanel();
panel.add(button);
getContentPane().add("South", panel);
text.append("Java Version: " +
System.getProperty("java.version")+"\n");
text.append("JAVA Applet Init()\n");
}
public void start() {
text.append("Applet started: " + startCount++ + "\n");
}
public void stop() {
text.append("Applet stopped.\n");
}
}
Compile above with:
> javac ShowApplet.java ### (produces ShowApplet.class)
Now the Scala version, ShowApplet.scala:
import javax.swing._
import java.awt.event._
class ShowApplet extends JApplet {
// 'val' or 'var' both ok; 'val' is ok because although
// JTextArea changes, the reference to it, 'text' itself, does not.
val text = new JTextArea()
// 'int' or 'Int' both ok here, but 'Int' is more idiomatic
var startCount: Int = 0
override def init() {
var button = new JButton("Press here!")
button.addActionListener(new ActionListener() {
def actionPerformed(e: ActionEvent) {
text.append("Button Pressed!\n")
}
} )
getContentPane().add("Center", new JScrollPane(text));
var panel = new JPanel()
panel.add(button)
getContentPane().add("South", panel)
text.append("Java Version: " +
System.getProperty("java.version")+"\n")
text.append("SCALA Applet Init()\n")
}
override def start() {
text.append("Applet started: " + startCount + "\n")
startCount = startCount + 1
}
override def stop() {
text.append("Applet stopped.\n")
}
}
Compile with:
> scalac ShowApplet.scala ### (also produces ShowApplet.class)
Now, we'll make the ShowApplet.html file to embed it in. Put it in the same directory as the ShowApplet.class file (whether the Java or Scala version):
Note: the Scala version calls the Scala libraries which neither your
Java plugin (nor the wider world's) will know anything about; we have
to tell your browser where it can download it in the ARCHIVE tag. My
Scala is installed in /usr/local/scala, make ARCHIVE tag point to wherever
'scala-library.jar' is located in your installation. Note, I believe
that giving a file system path like this will only work on your local machine.
To set it up correctly for the external world, you'll have to put it where your web-server can see it, in the usual way.
ShowApplet.html:
Now, just point your browser at ShowApplet.html, eg, from the same directory,
> firefox ./ShowApplet.html