Graphics2D Path Applet

A path allows you to combine straight lines and curves to form an object to which you can apply transforms. This demonstration shows a blue stick man becoming larger as it approaches, while the red stick man remains in the same place. We apply the scale to the translation transform to achieve simultaneous translation (movement) and zooming. We have converted this applet to an application. See the following page for the code and a screenshot of the application running on the Raspberry Pi.

namespace path_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.*;

type
  PathApplet = public class(Applet, Runnable)
  private
    var stickman : GeneralPath;
    var aT, at_translate, at_translate2 : AffineTransform;
    var i : Integer;
    var t : Thread := nil;
    var threadSuspended : Boolean;
  public
    method init; override;
    method start; override;
    method stop; override;
    method run;
    method paint(g : Graphics); override;
  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;

end.

Programming - a skill for life!

Applets using Graphics2D for transforms, gradient painting and pixel drawing in Oxygene for Java