Graphics2D Quadratic Curve Applet

This applet show how you can code for a quadratic curve. The constructor's parameters are, in order, the x and y coordinates of one end, of the control point, and of the other end. You can think of the control point as an attractor for the line. In this example the control point moves diagonally.

namespace quad_curve_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
  CurveApplet = public class(Applet, Runnable)
  private
    var curve : QuadCurve2D.Float;
    var width, height : Integer;
    var i : Integer := -11;
    var t : Thread := nil;
    var increment : Boolean := true;
    var threadSuspended : Boolean;
  public
    method init; override;
    method start; override;
    method stop; override;
    method run;
    method paint(g : Graphics); override;
  end;

implementation

method CurveApplet.init;
begin
  width := getSize.width;
  height := getSize.height;
  setBackground(Color.red);
end;

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

//Executed within the thread that this applet created
method CurveApplet.run;
begin
  try
    while true do
      begin
         //Where the thread does some work
        if Increment then
          inc(i)
        else
          dec(i);
        if (i < -10) or (i > width + 10) then
          Increment := not Increment;
        //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(10); //Interval given in milliseconds
     end;
  except
    on e : InterruptedException do
      begin
      end;
  end;
end;

method CurveApplet.paint(g : Graphics);
var
  g2 : Graphics2D;
begin
  g2 := Graphics2D(g);
  curve := new QuadCurve2D.Float(0, height / 2, i, i, width, height / 2);
  g2.setColor(Color.blue);
  g2.draw(curve);
end;

end.

Programming - a skill for life!

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