Back Buffer Demonstration

The code of BackBufferApplet.pas

See Michael McGuffin's remarks at the start of the well-commented code for an explanation of the smooth nature of this impressive applet.
namespace back_buffer;

{
  Applet to demonstrate double-buffering
  Author: Michael McGuffin
  Converted to Oxygene for Java by Dharmesh Tailor
  Rather than perform drawing operations directly to screen,
  we draw onto an image buffer (the "backbuffer") in memory,
  and only after completing this image do we copy it onto the
  screen. There is no need to erase or clear the contents of
  the screen before copying (or "swapping", as it's called)
  the backbuffer onto the screen. During the swap, we simply
  overwrite the image on the screen. Hence the screen never
  displays a partial image: even in the middle of swapping,
  the screen will contain 50 % of the old image and 50 % of
  the new image. As long as the swap is not too slow, the eye
  is fooled into seeing a continuous, smooth flow of images.
}

interface

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

type
  BackBufferApplet = public class(Applet, MouseMotionListener)
  private
    var width, height : Integer;
    var mx, my : Integer; //The mouse coordinates
    var N : Integer := 300;
    var points : array [0 .. 299] of Point; //To store random points
    var img, backbuffer : Image;
    var backg : Graphics;
  public
    method init; override;
    method mouseMoved(e: MouseEvent);
    method mouseDragged(e: MouseEvent);
    method update(g: Graphics); override;
    method paint(g: Graphics); override;
  end;

implementation

method BackBufferApplet.init;
var
  i, x, y : Integer;
begin
  width := getSize.width;
  height := getSize.height;

  mx := width / 2;
  my := height / 2;

  for i := 0 to N - 1 do
    begin
      //Random number generator to produce series of random lines
      x := Integer((Math.random - 0.5) * width / 1.5);
      y := Integer((Math.random - 0.5) * height / 1.5);
      points[i] := new Point(x, y);
    end;

  //Image must be in the same directory as the applet
  img := getImage(getDocumentBase, 'internet.jpg');

  backbuffer := createImage(width, height);
  backg := backbuffer.getGraphics;
  backg.setColor(Color.white);

  addMouseMotionListener(self);
end;

method BackBufferApplet.mouseMoved(e: MouseEvent);
var
  j : Integer;
  A, B : Point;
begin
  mx := e.X;
  my := e.Y;

  backg.drawImage(img, 0, 0, self);
  for j := 1 to (N - 1) do
    begin
      A := points[j - 1];
      B := points[j];
      backg.drawLine(mx + A.x, my + A.y, mx + B.x, my + B.y);
    end;

  repaint;
  e.consume;
end;

method BackBufferApplet.mouseDragged(e: MouseEvent);
begin
end;

//Overrides the behaviour of 'update'
//By default, method 'update':
//1 - clears the applet by filling with the background colour
//2 - sets the colour of the graphics to applet's foreground colour
//3 - calls the applet's paint function
method BackBufferApplet.update(g : Graphics);
begin
  g.drawImage(backbuffer, 0, 0, self);
end;

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

end.

A minimal project file back_buffer.oxygene

Execute the compiler by typing at the command prompt (with the current directory set to the location of this project file and the source file):

msbuild back_buffer.oxygene
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
  <PropertyGroup>
    <OutputType>Library</OutputType>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="rt.jar" />
  </ItemGroup>
  <ItemGroup>
    <Compile Include="BackBufferApplet.pas" />
  </ItemGroup>
  <Import Project="$(MSBuildExtensionsPath)\RemObjects Software\Oxygene\RemObjects.Oxygene.Cooper.targets" />
</Project>

Web page back_buffer.html to run the applet

<!DOCTYPE html SYSTEM "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Back Buffer Test</title>
  </head>
  <body>
    <center>
      <h3>Back Buffer Test</h2>   
      <p>Java must be enabled and internet.jpg must be in the same folder as the applet. Move 
            the cursor just beneath the centre of this text to see the applet.</p>  
      < applet archive="back_buffer.jar" code="back_buffer/BackBufferApplet.class" 
                width="200" height="200" />    
    </center>
  </body>
</html>

Programming - a skill for life!

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