Image Demonstration

This applet demonstrates an approved way to move an object over a background image. A new thread is necessary for satisfactory movement and a back buffer reduces flicker. The object should have a transparent colour so that the background shows through between the outline of the object and the rectangular extremity of the image.

We supply below for the image demonstration:

  1. the code of image_demo.pas;
  2. a slimmed-down xml project file (image_demo.oxygene) to be opened in Visual Studio or used by the command line instruction msbuild image_demo.oxygene;
  3. a simple HTML file to run the applet.

You can see Gandalf moving here.

Oxygene code

namespace image_demo;
// Thread and backbuffer code  based on applets by Michael McGuffin which were 
// converted to Oxygene for Java by Dharmesh Tailor

interface

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

type
  ImageApplet = public class(Applet, Runnable)
  private
    var imgBackground, imgObject, backbuffer : Image;
    var i : Integer := 0;
    var t : Thread := nil;
    var threadSuspended : Boolean;
    var backg : Graphics;
    var width, height : Integer;
  public
    method init; override;
    method start; override;
    method stop; override;
    method update(g : Graphics); override;
    method run;
    method paint(g : Graphics); override;
  end;

implementation

method ImageApplet.init;
begin
  width := getSize.width;
  height := getSize.height;
  backbuffer := createImage(width, height);
  backg := backbuffer.getGraphics;
  imgBackground  := getImage(getDocumentBase, 'voxel.jpg');
  imgObject:= getImage(getDocumentBase, 'gandalf.gif');
end;

method ImageApplet.update(g : Graphics);
begin
  g.drawImage(backbuffer, 0, 0, self);
end;

//Executed after the applet is created
//also whenever the browser returns to the page containing the applet
method ImageApplet.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 ImageApplet.stop;
begin
  threadSuspended := true;
end;

//Executed within the thread that this applet created
method ImageApplet.run;
begin
  try
    for i := 0 to 80 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;
        backg.drawImage(imgBackground, 0, 0, self);
        backg.drawImage(imgObject, 300 + i, 250 + i,  self);
        repaint;
        t.sleep(30); //Interval given in milliseconds
     end;
  except
    on e : InterruptedException do
      begin
      end;
  end;
end;

method ImageApplet.paint(g : Graphics);
begin
  update(g);
end;

end.


Project file

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
  <PropertyGroup>
    <ProductVersion>3.5</ProductVersion>
    <OutputType>Library</OutputType>
    <Configuration Condition="'$(Configuration)' == ''">Release</Configuration>
    <Name>Image Demo</Name>
    <RootNamespace>image_demo</RootNamespace>   
    <AssemblyName>image_demo</AssemblyName>
    <StartupClass />
    <DefaultUses />
  </PropertyGroup> 
  <PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
    <Optimize>true</Optimize>
    <OutputPath>.\bin\Release</OutputPath>
    <GenerateDebugInfo>False</GenerateDebugInfo>
    <GenerateMDB>False</GenerateMDB>
    <EnableAsserts>False</EnableAsserts>
    <TreatWarningsAsErrors>False</TreatWarningsAsErrors>
    <CaptureConsoleOutput>False</CaptureConsoleOutput>
    <StartMode>Project</StartMode>
    <RegisterForComInterop>False</RegisterForComInterop>
    <CpuType>anycpu</CpuType>
    <RuntimeVersion>v25</RuntimeVersion>
    <XmlDoc>False</XmlDoc>
    <XmlDocWarningLevel>WarningOnPublicMembers</XmlDocWarningLevel>
    <WarnOnCaseMismatch>True</WarnOnCaseMismatch>
    <EnableUnmanagedDebugging>False</EnableUnmanagedDebugging>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="rt.jar" />
  </ItemGroup>
  <ItemGroup>
    <Compile Include="image_demo.pas" />
  </ItemGroup>
  <ItemGroup>
    <Folder Include="Properties\" />
  </ItemGroup>
  <ItemGroup>
    <None Include="image_demo.html">
      <SubType>Content</SubType>
    </None>
  </ItemGroup>
  <Import Project="$(MSBuildExtensionsPath)\RemObjects Software\Oxygene\RemObjects.Oxygene.Cooperc.targets" />
</Project>

HTML file

<html>
<head>
    <title>Image Demo Applet</title>
</head>
<body>
    <h3 align="Center">Image Demo Applet</h3>
    <center>
   ( <font color="red">You need to enable Java to see this applet.</font> )
        <applet archive="image_demo.jar" code="image_demo/image_demo.class"
            codebase="bin/Release" width="789" height="532">            
        </applet>
    </center>
</body>
</html>
Programming - a skill for life!

Mouse and keyboard input, graphics (including Graphics2D), threads and widgets in Oxygene for Java applets