Demonstration of Drawing Lines

You can see this and three other applets here. This code is reused in the demonstration of the use of threads in applets.

You can compare this code with the RemObjects C# version below.

The code of LinesApplet.pas

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

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

type
  LinesApplet = public class(Applet)
  private
    //Declare two variables of type integer
    var width, height : Integer;
  public
    method init; override;
    method paint(g : Graphics); override;
  end;

implementation

//This is executed when the applet starts
method LinesApplet.init;
begin
  //Store the height and width of the applet for future reference
  width := getSize.width;
  height := getSize.height;

  //Make the default background colour black
  setBackground(Color.black);
end;

//This gets executed whenever the applet is asked to redraw itself
method LinesApplet.paint(g: Graphics);
var
  i : Integer;
begin
  //Set the current drawing colour to green
  g.setColor(Color.green);

  //Draw ten lines using a loop
  for i := 0 to 9 do
    begin
      //The 'drawLine' method requires 4 numbers:
      //the x and y coordinates of the starting point.
      //and the x and y coordinates of the ending point
      //NOTE: in 2D graphics programming, the origin is at the
      //upper left corner where the x-axis increases to the right
      //and the y-axis increases downward
      g.drawLine(width, height, i * width / 10, 0);
    end;
end;

end.

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

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

RemObjects C# Version of LinesApplet

/*
  Author: Michael McGuffin
  Converted to Oxygene for Java by Dharmesh Tailor then converted
  to RemObjects C# by Norman Morrison
*/

using java.util;
using java.applet;
using java.awt;

namespace lines_applet_cs
{
  public class MyApplet: Applet
  {

    private Integer width, height;

    public override void init()
    {
      //Store the height and width of the applet for future reference
      width = getSize().width;
      height = getSize().height;

      //Make the default background colour black
      setBackground(Color.black);
    }
    
    public void paint(Graphics g)
    {
      //Set the current drawing colour to green
      g.setColor(Color.green);

      //Draw ten lines using a loop
      for (Integer i = 0 ; i < 10 ; i++)
      {
        //The 'drawLine' method requires 4 numbers:
        //the x and y coordinates of the starting point.
        //and the x and y coordinates of the ending point
        //NOTE: in 2D graphics programming, the origin is at the
        //upper left corner where the x-axis increases to the right
        //and the y-axis increases downward
        g.drawLine(width, height, i * width / 10, 0);
      }
    } 
  }
}


Programming - a skill for life!

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