Mouse Input Demonstrations

When using interfaces such as MouseListener and MouseMotionListener, you must provide an implementation of all of their methods, even if your method bodies are empty. The mouse methods, in alphabetical order are

  • mouseClicked (MouseListener interface)
  • mouseDragged (MouseMotionListener interface)
  • mouseEntered (MouseListener interface)
  • mouseExited (MouseListener interface)
  • mouseMoved (MouseMotionListener interface)
  • mousePressed (MouseListener interface)
  • mouseReleased (MouseListener interface)

(For some interfaces there are adapters that eliminate the need for empty implementations). The four examples between them cover all of these methods. Notice that, as usual with object-oriented programming, the methods are public but the variables are private.

You can try using the applets here.

See below the Oxygene source code for all the applets. For mouse_move we also provide:
  1. a minimal project file mouse_move.oxygene (for use by the command-line compiler with the command msbuild mouse_move.oxygene which creates mouse_move.jar;
  2. the code of an html file to test it;
  3. a screenshot of the applet in use.
You should find it easy to change the names of files to create and test the other three applets and those you write yourself.

Oxygene code of MoveApplet.pas

namespace mouse_move;
{
  Author: Michael McGuffin
  Converted to Oxygene for Java by Dharmesh Tailor
}
      
interface
      
uses
  java.util,
  java.applet.*,
  java.awt,
  java.awt.event;

type
  MoveApplet = public class(Applet, MouseListener, MouseMotionListener)
  private
    //Stores list of mouse positions
    listOfPositions : Vector := new Vector;
  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 MoveApplet.init;
begin
  setBackground(Color.black);
  addMouseListener(self);
  addMouseMotionListener(self);
end;

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

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

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

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

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

method MoveApplet.mouseDragged(e : MouseEvent);
begin
end;

method MoveApplet.mouseMoved(e : MouseEvent);
begin
  if listOfPositions.size >= 50 then
    begin
      // Delete first element in the vector.
      listOfPositions.removeElementAt(0);
    end;

  // Add the new position to the end of the list.
  listOfPositions.addElement(new Point(e.X, e.Y));

  repaint;
  e.consume;
end;

method MoveApplet.paint(g: Graphics);
var
  j : Integer;
  A, B : Point;
begin
  g.setColor(Color.white);
  for j := 1 to (listOfPositions.size - 1) do
    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.

Oxygene code of PressApplet.pas

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

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

type
  PressApplet = public class(Applet, MouseListener, MouseMotionListener)
  private
    var width, height : Integer;
    var mx, my : Integer; //Mouse coordinates
    var isButtonPressed : Boolean := false;
  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 PressApplet.init;
begin
  setBackground(Color.black);
  width := getSize.width;
  height := getSize.height;

  mx := width / 2;
  my := height / 2;

  addMouseListener(self);
  addMouseMotionListener(self);
end;

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

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

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

method PressApplet.mousePressed(e : MouseEvent);
begin
  isButtonPressed := true;
  setBackground(Color.gray);
  repaint;
  e.consume;
end;

method PressApplet.mouseReleased(e : MouseEvent);
begin
  isButtonPressed := false;
  setBackground(Color.black);
  repaint;
  e.consume;
end;

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

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

method PressApplet.paint(g : Graphics);
begin
  if isButtonPressed then
    begin
      g.setColor(Color.black);
    end
  else
    g.setColor(Color.gray);
  g.fillRect(mx - 20, my - 20, 40, 40);
end;

end.

Oxygene code of DragApplet.pas

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

interface

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

type
  DragApplet = public class(Applet, MouseListener, MouseMotionListener)
  private
    var width, height : Integer;
    var x, y : Integer; //Coordinates of upper-left corner of box
    var mx, my : Integer; //Mouse coordinates
    var isMouseDraggingBox : Boolean := false;
  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 DragApplet.init;
begin
  width := getSize.width;
  height := getSize.height;
  setBackground(Color.black);

  x := width/2 - 20;
  y := height/2 - 20;

  addMouseListener(self);
  addMouseMotionListener(self);
end;

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

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

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

method DragApplet.mousePressed(e : MouseEvent);
begin
  mx := e.X;
  my := e.Y;
  if (x < mx) and (mx < x + 40) and (y < my) and (my < y + 40) then
  begin
    isMouseDraggingBox := true;
  end;
  e.consume;
end;

method DragApplet.mouseReleased(e : MouseEvent);
begin
  isMouseDraggingBox := false;
  e.consume;
end;

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

method DragApplet.mouseDragged(e : MouseEvent);
var
  new_mx, new_my : Integer;
begin
  if isMouseDraggingBox then
    begin
      //Get the latest mouse position.
      new_mx := e.X;
      new_my := e.Y;

      //Displace the box by the distance moved since the last event.
      x := x + new_mx - mx;
      y := y + new_my - my;

      //Update the coordinates.
      mx := new_mx;
      my := new_my;

      repaint;
      e.consume;
    end;
end;

method DragApplet.paint(g : Graphics);
begin
  g.setColor(Color.gray);
  g.fillRect(x, y, 40, 40);
end;

end.

Oxygene code of MouseApplet.pas

namespace mouse_event_demo;

{
  Applet to demonstrate mouse event handlers
  Copyright (c) 2012 Dharmesh Tailor

  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.applet.*,
  java.awt,
  java.awt.event;

type
  MouseApplet = public class(Applet, MouseListener, MouseMotionListener)
  private
    var msg : String := ''; 
    //Coordinates of mouse
    var mouseX : Integer := 0;
    var mouseY : Integer := 0;
  public
    method init; override;
    method mouseClicked(me : MouseEvent);
    method mouseEntered(me : MouseEvent);
    method mouseExited(me : MouseEvent);
    method mousePressed(me : MouseEvent);
    method mouseReleased(me : MouseEvent);
    method mouseDragged(me : MouseEvent);
    method mouseMoved(me : MouseEvent);
    method paint(g : Graphics); override;
  end;

implementation

method MouseApplet.init;
begin
  Background := Color.gray;
  addMouseListener(self);
  addMouseMotionListener(self);
end;

method MouseApplet.mouseClicked(me : MouseEvent);
begin
  mouseX := 0;
  mouseY := 10;
  msg := 'Mouse clicked.';
  repaint;
end;

method MouseApplet.mouseEntered(me : MouseEvent);
begin
  mouseX := 0;
  mouseY := 10;
  msg := 'Mouse entered.';
  repaint;
end;

method MouseApplet.mouseExited(me : MouseEvent);
begin
  mouseX := 0;
  mouseY := 10;
  msg := 'Mouse exited.';
  repaint;
end;

method MouseApplet.mousePressed(me : MouseEvent);
begin
  mouseX := me.X;
  mouseY := me.Y;
  msg := 'Down';
  repaint;
end;

method MouseApplet.mouseReleased(me : MouseEvent);
begin
  mouseX := me.X;
  mouseY := me.Y;
  msg := 'Up';
  repaint;
end;

method MouseApplet.mouseDragged(me : MouseEvent);
begin
  mouseX := 0;
  mouseY := 10;
  msg := 'Dragging mouse at ' + me.X + ', ' + me.Y;
  repaint;
end;

method MouseApplet.mouseMoved(me : MouseEvent);
begin
end;

method MouseApplet.paint(g : Graphics);
begin
  g.drawString(msg, mouseX, mouseY);
end;

end.

Project file mouse_move.oxygene

Compile this project from the command line with the simple instruction msbuild mouse_move.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="MoveApplet.pas" />
  </ItemGroup>
  <Import Project="$(MSBuildExtensionsPath)\RemObjects  Software\Oxygene\RemObjects.Oxygene.Cooper.targets" />
</Project>

HTML file and screenshot for mouse_move

<html>
  <head>
      <title>Mouse Move Test</title>
  </head>
  <body>
    <center>
      <h3>Mouse Move Test</h2>   
      <p>Java must be enabled.</p>  
      <applet archive="move_applet.jar" code="mouse_move/MoveApplet.class" 
                width="200" height="200" />    
    </center>
  </body>
</html>

Screenshot Screenshot
Programming - a skill for life!

Introduction to Applets in Oxygene for Java