Oxygene for Java Version of MultiDraw

by Ihsan Fazal: U6 Age ~17

Introduction

This version has similar functionality to the original, but the code is very different; the Oxygene version uses Java units instead of the Windows-specific WinGraph, WinMouse, and WinCrt.

Note the following points.
  • When implementing an interface such as KeyListener, you must include all of its methods in the interface and implementation sections even if a method is empty.
  • We cast the Graphics object g to a Graphics2D object to gain additional functionality.
  • We paint to a buffered image then copy it to the applet when all of the changes have been made.

We compiled the applet with the command Oxygene g2d.oxygene. The Oxygene code g2d.pas, the project file g2d.oxygene, the HTML file including the applet and a screenshot follow. You can try the applet yourself here.

The Program

namespace g2d;
{
    Copyright (c) 2011 Ihsan Fazal
    
    Licensed under the Apache License, Version 2.0 (the "License"); you may not 
    use this file except in compliance with the License, as described at
    http://www.apache.org/licenses/ and http://www.pp4s.co.uk/licenses/

    Converted to Oxygene for Java by PPS.
}

interface

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

type
  g2d = public class(Applet, KeyListener, MouseListener, MouseMotionListener)
  private
    const TOP_MARGIN = 30;
    var myfont : Font;
    var s : String := '';
    var offset, Width, Height, mx, my : Integer;
    var redRGB : Integer  := Color.red.getRGB;
    var orangeRGB : Integer  := Color.orange.getRGB;
    var yellowRGB : Integer  := Color.yellow.getRGB;
    var greenRGB : Integer  := Color.green.getRGB;
    var blueRGB : Integer  := Color.blue.getRGB;
    var img : BufferedImage;    
  public
    method init(); override;
    method paint(g: Graphics); override;
    method keyPressed(e: KeyEvent);
    method keyReleased(e: KeyEvent);
    method keyTyped(e: KeyEvent);
    method mouseEntered(e: MouseEvent);
    method mouseExited(e: MouseEvent);
    method mouseClicked(e: MouseEvent);
    method mousePressed(e: MouseEvent);
    method mouseReleased(e: MouseEvent);
    method mouseMoved(e: MouseEvent);
    method mouseDragged(e: MouseEvent);
  end;
  
implementation

method g2d.init();
begin
  Background := Color.gray;  
  addKeyListener(self);
  addMouseListener(self);
  addMouseMotionListener(self);
  Width := Size.width;
  Height := Size.height;
  img  := new BufferedImage(Width, Height - TOP_MARGIN,                
                            BufferedImage.TYPE_4BYTE_ABGR);
  myfont := new Font('TimesRoman', Font.BOLD, 16);
end;

method g2d.paint(g: Graphics);
var
  twodg : Graphics2D;  
begin
  twodg := Graphics2D(g);
  twodg.setColor(Color.yellow);
  twodg.setFont(myfont);
  twodg.drawString('Please enter the offset:', 10, 20);
  if s <> '' then
    begin
      twodg.drawString(s, 175, 20);
      img.setRGB(mx, my - TOP_MARGIN, redRGB);
      img.setRGB(mx + offset, my + offset - TOP_MARGIN, orangeRGB);
      img.setRGB(mx + 2 * offset, my + 2 * offset - TOP_MARGIN, yellowRGB);
      img.setRGB(mx + 3 * offset, my + 3 * offset - TOP_MARGIN, greenRGB);
      img.setRGB(mx + 4 * offset, my + 4 * offset - TOP_MARGIN, blueRGB);
      twodg.drawImage(img, 0, TOP_MARGIN, self);
  end;
end;

method g2d.mouseEntered(e: MouseEvent);
begin
end;

method g2d.mouseExited(e: MouseEvent);
begin
end;

method g2d.mouseClicked(e: MouseEvent);
begin
  s := '';
  img  := new BufferedImage(Width, Height - TOP_MARGIN,                
                            BufferedImage.TYPE_4BYTE_ABGR);
  repaint;
  e.consume;
end;

method g2d.mousePressed(e: MouseEvent);
begin
end;

method g2d.mouseReleased(e: MouseEvent);
begin
end;

method g2d.mouseMoved(e: MouseEvent);
begin 
end;

method g2d.mouseDragged(e: MouseEvent);
begin
  mx := e.X;
  my := e.Y;
  repaint;
  e.consume;
end;

method g2d.keyPressed(e: KeyEvent);
begin
end;

method g2d.keyReleased(e: KeyEvent);
begin
end;

method g2d.keyTyped(e: KeyEvent);
begin
  var c := e.KeyChar;
  if c <> KeyEvent.CHAR_UNDEFINED then
    begin
      s := s + c;
      offset := Integer.valueOf(s.trim);
      repaint;
      e.consume;
    end;
end;

end.

The 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>G2D</Name>
    <RootNamespace>g2d</RootNamespace>   
    <AssemblyName>g2d</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="g2d.pas" />
  </ItemGroup>
  <ItemGroup>
    <Folder Include="Properties\" />
  </ItemGroup>
  <ItemGroup>
    <None Include="g2d.html">
      <SubType>Content</SubType>
    </None>
  </ItemGroup>
  <Import Project="$(MSBuildExtensionsPath)\RemObjects Software\Oxygene\RemObjects.Oxygene.Cooper.targets" />
</Project>

The HTML file

<html>
<head>
    <title>MultiDraw Applet</title>
</head>
<body>
    <h3 align="Center">MultiDraw Applet</h3>
    <center>
   ( <font color="red">You need to enable Java to see this applet. 
Click in the applet before typing the offset. Drag to draw and click to start 
again.</font> )  

        <applet archive="g2d.jar" code="g2d/g2d.class"
            codebase="bin/Release" width="400" height="300">          
        </applet>
    </center>
</body>
</html>

Screenshot

MultiDraw

MultiDraw

Programming - a skill for life!

by Ihsan Fazal: U6 Age ~17