You are here

Let's Do It! - Java Classes and Discussion

Author(s): 
Alexander Bogomolny

For instructions on using Java source files from JOMA, see the How-to page.

  1. View the source for CubicSpline.java
  2. View the source for Tridiagonal.java
  3. View the source for MMover.java
  4. View the source for MMoveable.java
  5. View the source for MPoint.java
  6. View the source for VPoint.java
  7. View the source for Scale.java
  8. View the source for SplineTest.java
  9. View the source for SplineTestCanvas.java
  10. View the source for SplineTestPanel.java

MMoveable is a Java interface which is a sort of abstract class whose methods have no bodies and whose attributes are necessarily final. Interfaces are the Java answer to the classical question of multiple inheritance. Formally speaking, a Java class cannot be derived from 2 or more other classes. When class A is derived from class B, it must be declared in the following manner:

class A extends B
{
...
}

It would be a mistake to extend a class from two or more classes. When class A derives from an interface B, the declaration is different:

class A implements B
{
...
}

and there is no limit on how many various interfaces a class may implement. For example, SplineTestCanvas extends an awt class Canvas while implementing two Java 1.1 interfaces: MouseListener and MouseMotionListener. MPoint is the simplest class I could think of that implements the MMoveable interface. It's a draggable point which is shown (if desirable) by a small circle. VPoint extends MPoint. It differs from MPoint in three respects. First, it may only be dragged up and down. Second, it stores information (in the form of two Scale attributes: XScale and YScale) about an additional coordinate system. Screen coordinates are a natural choice when a point must be dragged around. For numerical computations, "real world" coordinates are more convenient. Scales are used to convert between the two systems. Third, VPoint is more lax than MPoint in its response to a dragging initiative. MPoint responds to a mouse pressed event when the cursor is a small distance away. VPoint responds to a mouse press when the cursor is at a prescribed distance away both vertically and horizontally. Distances in the two directions are not necessarily the same. The idea here was to insure responsiveness to the cursor movements while avoiding unnecessary accidental proliferation of interpolation points.

I found the combination of MMover and MMoveable extremely convenient. Each MMoveable object "knows" by means of its IsHit method when dragging is about to start. All draggable objects are reported to a MMover object through its AddMoveable method. One (as in the SplineTest example) or more MMover objects are created per screen estate areas (usually, Canvases in awt) to manage MMoveable objects. MMover methods are invoked on three occasions:

public void mousePressed(MouseEvent event)
    {
        if (Mover.IsHit(event.getX(), event.getY()))
            repaint();
    }
 
    public void mouseReleased(MouseEvent event)
    {
        if (Mover.Dragging())
        {
            Mover.UpdateLocation(event.getX(), event.getY());
            Mover.StopDragging();
            repaint();
        }
    }
 
    public void mouseDragged(MouseEvent event)
    {
        if (Mover.Dragging())
        {
            Mover.UpdateLocation(event.getX(), event.getY());
            repaint();
        }
    }

I plan to create more applets that use the above classes. Meanwhile, comments, friendly critique, and suggestions are welcome. From the programming perspective, the nicest applet only deserves a place in The Applet Book if it's been designed according to simple principles and coded following simple guidelines. I know nothing more intuitive and natural than the object-oriented paradigm. As a hook for a possible discussion on OOD (OO design) and OOP (OO Programming), one of the classes, VPoint, has a built-in flaw, a violation of basic OO principles. Can you find it?  

Alexander Bogomolny, "Let's Do It! - Java Classes and Discussion," Loci (September 2004)

JOMA

Journal of Online Mathematics and its Applications

Dummy View - NOT TO BE DELETED