alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *			Sun Public License Notice
 *
 * The contents of this file are subject to the Sun Public License Version
 * 1.0 (the "License"). You may not use this file except in compliance
 * with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is Terminal Emulator.
 * The Initial Developer of the Original Code is Sun Microsystems, Inc..
 * Portions created by Sun Microsystems, Inc. are Copyright (C) 2001-2004.
 * All Rights Reserved.
 *
 * Contributor(s): Ivan Soleimanipour.
 */

package org.netbeans.lib.terminalemulator;

/**
 * A cartesian coordinate class, similar to Point.
 * The equivalent of 'offset' in swing.text.Document.
 * 
* Rows are 0-origin, columns are 0-origin. *

* Why not the regular Java Point? Because ... *

    *
  • Point with 'x' and 'y' is not as clear. *
  • Point doesn't implement Comparable, which we depend on a lot. *
*/ class BCoord implements Comparable { public int row; public int col; /** * Create a BCoord at the origin (top-left) */ public BCoord() { this.row = 0; this.col = 0; } public BCoord(int row, int col) { // Note this is flipped from Points(x, y) sense this.row = row; this.col = col; } public BCoord(BCoord coord) { this.row = coord.row; this.col = coord.col; } public void copyFrom(BCoord src) { this.row = src.row; this.col = src.col; } // Overrides of Object: public Object clone() { return new BCoord(row, col); } public boolean equals(BCoord target) { // XXX param should be Object and also hashCode should be overriden if (row != target.row) return false; return col == target.col; } public String toString() { return "(r=" + row + ",c=" + col + ")"; // NOI18N } /** * Examples: * To satisfy Comparable. *

*

     * a < b	=== a.compareTo(b) < 0
     * a >= b	=== a.compareTo(b) >= 0
     * 
*/ public int compareTo(Object o) throws ClassCastException { BCoord target = (BCoord) o; // -1 or negative -> this < o // 0 -> this == o // +1 or positive -> this > o if (this.row < target.row) return -1; else if (this.row > target.row) return +1; else { return this.col - target.col; } } public void clip(int rows, int cols) { // BE CAREFUL and clip view BCoords with a view box and buffer // BCoords with a buffer box! if (row < 0) row = 0; else if (row > rows) row = rows; if (col < 0) col = 0; else if (col > cols) col = cols; } }
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.