Demonstrations of the use of Colour

You can see these and the two preceding applets displaying here.

The code of ColourApplet.pas

namespace colour_applet;
{
  Author: Michael McGuffin
  Converted to Oxygene for Java by Dharmesh Tailor
}
interface

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

type
  ColourApplet = public class(Applet)
  private
    var width, height : Integer;
    var N : Integer := 25;
    var spectrum : array [0 .. 24] of Color;
  public
    method init; override;
    method paint(g : Graphics); override;
  end;

implementation

method ColourApplet.init;
var
  i : Integer;
begin
  width := getSize.width;
  height := getSize.height;
  setBackground(Color.black);

  //Generate the colours and store them in an array
  for i := 0 to (N - 1) do
    begin
      //Colours are specified by Hue, Saturation and Brightness in the range [0,1]
      spectrum[i] := new Color(Color.HSBtoRGB(i / Double(N), 1, 1));
    end;
end;

method ColourApplet.paint(g : Graphics);
var
  radius, i, x, y : Integer;
  angle : Double;
begin
  radius := width / 3;
  for i := 0 to N - 1 do
    begin
      //Computes the (x,y) positions along a circle
      //using the sine and cosine of an appropriately computed angle
      angle := 2  *  Math.PI  *  i / Double(N);
      x := Integer(radius * Math.cos(angle));
      y := Integer(radius * Math.sin(angle));

      g.setColor(spectrum[i]);
      g.drawString('Colour', width / 2 + x, height / 2 + y);
    end;
end;

end.

A minimal project file colour_applet.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 colour_applet.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="ColourApplet.pas" />
  </ItemGroup>
  <Import Project="$(MSBuildExtensionsPath)\RemObjects Software\Oxygene\RemObjects.Oxygene.Cooper.targets" />
</Project>

Web page ColourApplet.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>Colour Demo</title>
  </head>
  <body>
    <center>
      <h3>Colour Demo</h3>   
      <p>Java must be enabled.</p>  
      <applet archive="colour_applet.jar"                                                    
                 code="colour_applet/ColourApplet.class" 
                width="300" height="300" />    
    </center>
  </body>
</html>

The code of ColouredArcsApplet.pas

namespace coloured_arcs_applet;
{
  Author: Michael McGuffin
  Converted to Oxygene for Java by Dharmesh Tailor
}
interface

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

type
  ColouredArcsApplet = public class(Applet)
  private
    var width, height : Integer;
    var N : Integer := 25; //Number of colours created
    var spectrum : array [0 .. 24] of Color; //An array of elements of type 'Color'
    var spectrum2 : array [0 .. 24] of Color; //Another array
  public
    method init; override;
    method paint(g : Graphics); override;
  end;

implementation

method ColouredArcsApplet.init;
var
  i : Integer;
begin
  width := getSize.width;
  height := getSize.height;
  setBackground(Color.black);

  //Generate the colours and store them in the arrays
  for i := 1 to N do
    begin
      //The three numbers passed to the Color constructor
      //are the RGB components in the range [0,1]

      //This colour goes from almost black to white
      spectrum[i - 1] := new Color(i / Double(N), i / Double(N), i / Double(N));

      //This colour goes from almost pure green to pure red
      spectrum2[i - 1] := new Color(i / Double(N), (N - i) / Double(N), 0);
    end;
end;

method ColouredArcsApplet.paint(g : Graphics);
var
  step, i : Integer;
begin
  step := 90 / N;
  for i := 0 to N - 1 do
    begin
      g.setColor(spectrum[i]);
      g.fillArc(0, 0, 2 * width, 2 * height, 90 + i * step, step + 1);

      g.setColor(spectrum2[i]);
      g.fillArc(width / 3, height / 3, 4 * width / 3, 4 * height / 3, 90 + i * step, step + 1);
    end;
end;

end.

A minimal project file coloured_arcs_applet.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 coloured_arcs_applet.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="ColouredArcsApplet.pas" />
  </ItemGroup>
  <Import Project="$(MSBuildExtensionsPath)\RemObjects Software\Oxygene\RemObjects.Oxygene.Cooper.targets" />
</Project>

Web page ColouredArcsApplet.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>Coloured Arcs Demo</title>
  </head>
  <body>
    <center>
      <h3>Coloured Arcs Demo</h3>   
      <p>Java must be enabled.</p>  
      <applet archive="coloured_arcs_applet.jar"                                              
                 code="coloured_arcs_applet/ColouredArcsApplet.class" 
                width="300" height="300" />    
    </center>
  </body>
</html>
Programming - a skill for life!

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