Graphics2D Application converted from Path Applet

See the previous page for the code of the applet that we converted to this application. The screenshot shows the application after the movement, which was jerky and flickery. We needed to set the OutputType to Executable in the .oxygene project file to obtain the jar file that we ran on the Pi with the command-line instruction /usr/bin/java -jar /media/usb/path_app_demo.jar.

Path Application running on the Pi

Path Application running on the Pi

namespace path_app_demo;
// Thread code based on an applet by Michael McGuffin which was 
// converted to Oxygene for Java by Dharmesh Tailor

interface

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

type
  PathApplet = public class(Applet, Runnable)
  private
    stickman : GeneralPath;
    aT, at_translate, at_translate2 : AffineTransform;
    i : Integer;
    t : Thread := nil;
    threadSuspended : Boolean;
  public
    method init; override;
    method start; override;
    method stop; override;
    method run;
    method paint(g : Graphics); override;
  end;

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

method PathApplet.init;
begin
  setBackground(Color.green);
  stickman := new GeneralPath;
  stickman.moveTo(50.0, 50.0); //Top of head
  stickman.quadTo(60.0, 55.0, 50.0, 60.0); //Bottom of head
  stickman.lineTo(50.0, 80.0);  //Pelvis
  stickman.lineTo(55.0, 110.0); //Right foot
  stickman.moveTo(50.0, 50.0);  //Back to top of head
  stickman.quadTo(40.0, 55.0, 50.0, 60.0); //Bottom of head
  stickman.moveTo(50.0, 80.0);  //Pelvis
  stickman.lineTo(45.0, 110.0); //Left foot
  stickman.moveTo(50.0, 63.0); //Shoulder
  stickman.lineTo(58.0, 85.0); //Right hand
  stickman.moveTo(50.0, 63.0); //Shoulder
  stickman.lineTo(40.0, 85.0); //Left hand
  at_translate2 := AffineTransform.getTranslateInstance(100, 0);
end;

//Executed after the applet is created
//also whenever the browser returns to the page containing the applet
method PathApplet.start;
begin
  if t = nil then
    begin
      t := new Thread(self);
      threadSuspended := false;
      t.start;
    end
  else
    begin
      if threadSuspended then
        begin
          threadSuspended := false;
            locking self do
            begin
              notify;
            end;
        end;
    end;
end;

//Executed whenever the browser leaves the page containing the applet
method PathApplet.stop;
begin
  threadSuspended := true;
end;

//Executed within the thread that this applet created
method PathApplet.run;
begin
  try
    t.sleep(1000); //Wait for a second before moving
    for i := 1 to 40 do
      begin
        //Thread checks to see if it should suspend itself
        if threadSuspended then
          begin
            locking self do
              begin
                while threadSuspended do
                  begin
                    wait;
                  end;
              end;
          end;
        repaint;
        t.sleep(40); //Interval given in milliseconds
     end;
  except
    on e : InterruptedException do
      begin
      end;
  end;
end;

method PathApplet.paint(g : Graphics);
var
  g2 : Graphics2D;
begin
  g2 := Graphics2D(g);
  aT := g2.getTransform;  //save existing transform
  
  at_translate  := AffineTransform.getTranslateInstance(i, i);
  g2.setColor(Color.blue);
  at_translate.scale(1.0 + 0.005 * i, 1.0 + 0.005 * i);  
  g2.transform(at_translate);
  g2.draw(stickman);
  
  g2.setTransform(at_translate2);
  g2.setColor(Color.red);
  g2.draw(stickman);
  g2.setTransform(aT); //restore original transform
end;

class method ConsoleApp.Main(args : array of String);
begin
  var AppletFrame : JFrame := new JFrame('Path Application');
  var MyPathApplet : PathApplet := new PathApplet;
  AppletFrame.add("Center", MyPathApplet);
  AppletFrame.setSize(250, 250);
  AppletFrame.setVisible(True);
  AppletFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  MyPathApplet.init;
  MyPathApplet.start;
end;

end.
Programming - a skill for life!

Applet using Graphics2D for a path which is translated and scaled