Converting Applets to Applications

You can compare the code of this application with that of Dharmesh Tailor's original applet and see that we have made additions without changing any of his code. We have added javax.swing (for the JFrame to contain an object of the existing applet class) to the uses clause, declared the Main method in the interface section, then coded the Main method at the end of the program. We must call the init and start methods of the applet (which are normally called by the browser). The browsers installed on the Raspberry Pi are not yet Java-enabled, but we can run the application on the Pi with the java program in the open JDK:

Java application (from applet) running on the Pi Java application (from applet) running on the Pi

We needed to set the OutputType to Executable in the .oxygene project file to obtain this jar file.

Oxygene code

namespace mouse_application;
{
  Original applet to demonstrate mouse event handlers
  Copyright (c) 2012 Dharmesh Tailor
  Converted from applet to application by PPS 

  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  use this file except in compliance with the License, as described at
  http://www.apache.org/licenses/ and http://www.pp4s.co.uk/licenses/
}

interface

uses
  java.applet.*,
  java.awt,
  java.awt.event,
  javax.swing;

type
  MouseApplet = public class(Applet, MouseListener, MouseMotionListener)
  private
    var msg : String := ''; 
    //Coordinates of mouse
    var mouseX : Integer := 0;
    var mouseY : Integer := 0;
  public
    method init; override;
    method mouseClicked(me : MouseEvent);
    method mouseEntered(me : MouseEvent);
    method mouseExited(me : MouseEvent);
    method mousePressed(me : MouseEvent);
    method mouseReleased(me : MouseEvent);
    method mouseDragged(me : MouseEvent);
    method mouseMoved(me : MouseEvent);
    method paint(g : Graphics); override;
  end;

  ConsoleApp = class    
  public
    class method Main(args : array of String);    
  end;

implementation

method MouseApplet.init;
begin
  Background := Color.gray;
  addMouseListener(self);
  addMouseMotionListener(self);
end;

method MouseApplet.mouseClicked(me : MouseEvent);
begin
  mouseX := 0;
  mouseY := 10;
  msg := 'Mouse clicked.';
  repaint;
end;

method MouseApplet.mouseEntered(me : MouseEvent);
begin
  mouseX := 0;
  mouseY := 10;
  msg := 'Mouse entered.';
  repaint;
end;

method MouseApplet.mouseExited(me : MouseEvent);
begin
  mouseX := 0;
  mouseY := 10;
  msg := 'Mouse exited.';
  repaint;
end;

method MouseApplet.mousePressed(me : MouseEvent);
begin
  mouseX := me.X;
  mouseY := me.Y;
  msg := 'Down';
  repaint;
end;

method MouseApplet.mouseReleased(me : MouseEvent);
begin
  mouseX := me.X;
  mouseY := me.Y;
  msg := 'Up';
  repaint;
end;

method MouseApplet.mouseDragged(me : MouseEvent);
begin
  mouseX := 0;
  mouseY := 10;
  msg := 'Dragging mouse at ' + me.X + ', ' + me.Y;
  repaint;
end;

method MouseApplet.mouseMoved(me : MouseEvent);
begin
end;

method MouseApplet.paint(g : Graphics);
begin
  g.drawString(msg, mouseX, mouseY);
end;

class method ConsoleApp.Main(args : array of String);
begin
  var AppletFrame : JFrame := new JFrame('Mouse Application');
  var MyMouseApplet : MouseApplet := new MouseApplet;
  AppletFrame.add("Center", MyMouseApplet);
  AppletFrame.setSize(300, 200);
  AppletFrame.setVisible(True);
  AppletFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  MyMouseApplet.init;
  MyMouseApplet.start;
end;

end.

Programming - a skill for life!

Introduction to Applets in Oxygene for Java