PenApplet

by Joel Sutcliffe: L6 Age ~17

Introduction

Joel used demo applets by Dharmesh Tailor to develop this effective applet in Oxygene for Java. It enables you to draw or doodle with a pen in the applet window in an HTML file. The line is continuous, but if you drag the pen out of then back into the applet you clear the screen. Try it for yourself here. You can compare the code with the RemObjects C# version below it.

The code follows a screenshot of a masterpiece being created with PenApplet.

Pen Drawing

Pen Drawing

The Code

namespace pen_applet;
{
    Copyright (c) 2013 Joel Sutcliffe

    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/ }
      
interface
      
uses
  java.util, java.applet.*, java.awt, java.awt.event;

type
  PenApplet = public class(Applet, MouseListener, MouseMotionListener)
  private
    width, height : Integer;
    mx, my, j : Integer; //Mouse coordinates
    isButtonPressed : Boolean := false; 
    listOfPositions : Vector := new Vector; //Stores list of mouse positions
  public
    method init; override;
    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);
    method paint(g : Graphics); override;
  end;

implementation

method PenApplet.init;
begin
  setBackground(Color.black);
  width := getSize.width;
  height := getSize.height;
  mx := width / 2;
  my := height / 2;
  addMouseListener(self);
  addMouseMotionListener(self);
end;

method PenApplet.mouseEntered(e : MouseEvent);
begin
  if isButtonPressed = true then
    begin
      listOfPositions.removeAllElements;
    end;
end;

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

method PenApplet.mouseClicked(e : MouseEvent);
begin
end;

method PenApplet.mousePressed(e : MouseEvent);
begin
  isButtonPressed := true;
  repaint;
  e.consume;
end;

method PenApplet.mouseReleased(e : MouseEvent);
begin
  isButtonPressed := false;
  j := listOfPositions.size - 1;
  listOfPositions[j - 1] := listOfPositions[j];
  repaint;
  e.consume;
end;

method PenApplet.mouseDragged(e : MouseEvent);
begin
  mx := e.X;
  my := e.Y;
  repaint;
  e.consume;
  listOfPositions.addElement(new Point(e.X, e.Y));
  e.consume;
end;

method PenApplet.mouseMoved(e : MouseEvent);
begin
  mx := e.X;
  my := e.Y;
  repaint;
  e.consume;
  // Add the new position to the end of the list.
  listOfPositions.addElement(new Point(e.X, e.Y));
  e.consume;
end;

method PenApplet.paint(g: Graphics);
var
  A, B : Point;
begin
  if isButtonPressed then
    begin
      g.setColor(Color.magenta); 
    end
  else
    g.setColor(Color.green);
  g.fillRect(mx, my - 5, 5, 10);
  if isButtonPressed then
    begin
      g.setColor(Color.red);
    end
  else
    g.setColor(Color.blue);
  g.fillRect(mx + 5, my - 10, 5, 10);
  g.fillRect(mx + 10, my - 15, 5, 10);
  g.fillRect(mx + 15, my - 20, 5, 10);
  g.fillRect(mx + 20, my - 25, 5, 10);
  if isButtonPressed then
    begin
      g.setColor(Color.magenta);
    end
  else
    g.setColor(Color.green);
  for j := 1 to (listOfPositions.size - 1) do
    begin
      if isButtonPressed then
      else 
        begin
          A := Point(listOfPositions.elementAt(j - 1));
          B := Point(listOfPositions.elementAt(j)); 
          g.drawLine(A.x, A.y, B.x, B.y);  
        end;
    end;    
end;

end.

RemObjects C# Version of PenApplet

/*
    Oxygene Original Copyright (c) 2013 Joel Sutcliffe

    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 RemObjects C# by Norman Morrison
*/

using java.util;
using java.applet;
using java.awt;
using java.awt.@event;

namespace pen_applet_cs
{  
  public class MyApplet : Applet, MouseListener, MouseMotionListener
  {
    Integer width, height;
    Integer mx, my, j; //Mouse coordinates
    Boolean isButtonPressed = false; 
    Vector listOfPositions = new Vector(); //Stores list of mouse positions

    public override void init()
    {
      setBackground(Color.black);
      width = getSize().width;
      height = getSize().height;
      mx = width / 2;
      my = height / 2;
      addMouseListener(this);
      addMouseMotionListener(this);
    }

    public void mouseEntered(MouseEvent e )
    {
      if (isButtonPressed == true )
        listOfPositions.removeAllElements();
    }

    public void mouseExited(MouseEvent e){}
    public void mouseClicked(MouseEvent e){}

    public void mousePressed(MouseEvent e)
    {
      isButtonPressed = true;
      repaint();
      e.consume();
    }

    public void mouseReleased(MouseEvent e)
    {
      isButtonPressed = false;
      j = listOfPositions.size() - 1;
      listOfPositions[j - 1] = listOfPositions[j];
      repaint();
      e.consume();
    }

    public void mouseDragged(MouseEvent e)
    {
      mx = e.X;
      my = e.Y;
      repaint();
      e.consume();
      listOfPositions.addElement(new Point(e.X, e.Y));
      e.consume();
    }

    public void mouseMoved(MouseEvent e)
    {
      mx = e.X;
      my = e.Y;
      repaint();
      e.consume();
      // Add the new position to the end of the list.
      listOfPositions.addElement(new Point(e.X, e.Y));
      e.consume();
    }
    
    public void paint(Graphics g)
    {
      Point A, B;
      if (isButtonPressed) 
        g.setColor(Color.magenta); 
      else
        g.setColor(Color.green);
      g.fillRect(mx, my - 5, 5, 10);
      if (isButtonPressed) 
        g.setColor(Color.red);
      else
        g.setColor(Color.blue);
      g.fillRect(mx + 5, my - 10, 5, 10);
      g.fillRect(mx + 10, my - 15, 5, 10);
      g.fillRect(mx + 15, my - 20, 5, 10);
      g.fillRect(mx + 20, my - 25, 5, 10);
      if (isButtonPressed)
        g.setColor(Color.magenta);
      else
        g.setColor(Color.green);
      for (j = 1 ; j < listOfPositions.size() ; j++) 
      {
        if (!isButtonPressed)
        {
          A = (Point)(listOfPositions.elementAt(j - 1));
          B = (Point)(listOfPositions.elementAt(j)); 
          g.drawLine(A.x, A.y, B.x, B.y);  
        }    
      } 
    }
  }
}

Remarks

Could you write a graphics applet using Oxygene for Java?

Programming - a skill for life!

Student programs to inspire you!