XPMSample

From GICL Wiki
Jump to: navigation, search

Note this is some non-working code:

/**
 * Oct 3, 2007
 * nguyen.graphics:Camera.java
 * --------
 */
package nguyen.graphics;

import java.awt.Point;

/**
 * Camera
 * @author duc
 * 
 * Description: 
 * 
 */
public class Camera {
	private int lengthY;
	private int lengthX;
	private Point windowLowerLeftPoint;
	private Point windowUpperRightPoint;

	/**
	 * @param windowLowerLeftPoint of the viewing window
	 * @param windowUpperRightPoint of the viewing window
	 */
	public Camera(Point windowLowerLeftPoint, Point windowUpperRightPoint) {
		
		lengthY = (windowUpperRightPoint.y-windowLowerLeftPoint.y)+1;
		lengthX = (windowUpperRightPoint.x-windowLowerLeftPoint.x)+1;
		
		this.windowLowerLeftPoint = windowLowerLeftPoint;
		this.windowUpperRightPoint = windowUpperRightPoint;
	}
	
	/**
	 * 
	 */
	public void printWindow(Canvas canvas){
		
		char[][] c = canvas.getCanvas();
		System.err.println("canvas size: X"+c[0].length+" Y"+c.length+ " lengthX: "+lengthX+" lengthY: "+lengthY);
		System.err.println("window: lowerleft: "+windowLowerLeftPoint+" upperRight: "+windowUpperRightPoint);
		printHeader(canvas, lengthX, lengthY);
		int rows = 0, cols = 0;
		for( int i = windowUpperRightPoint.y; i >= windowLowerLeftPoint.y; i--){
		//for( int i = lengthY-1; i >= 0; i--){
			System.out.print("\"");
			
			//for( int j = windowLowerLeftPoint.x; j < windowUpperRightPoint.x; j++){
			for(int j = windowLowerLeftPoint.x; j < windowUpperRightPoint.x+1; j++){
				System.out.print(c[i][j]);
				if( i == windowLowerLeftPoint.y ) cols++;
			}
			if( i > windowLowerLeftPoint.y){
				System.out.println("\",");
			} 
			rows++;
		}
		System.err.println("rows: "+rows+" cols: "+cols);
		printFooter();
	}
	
	public void printWholeCanvas(Canvas canvas){
		
		char[][] c = canvas.getCanvas();
		System.err.println("canvas size: X"+c[0].length+" Y"+c.length+ " lengthX: "+lengthX+" lengthY: "+lengthY);
		
		printHeader(canvas, c[0].length, c.length);
		int rows = 0, cols = 0;
		for( int i = c.length-1; i >= 0; i--){
		//for( int i = lengthY-1; i>=0; i--){
			System.out.print("\"");
			for( int j = 0; j < c[i].length; j++){
				System.out.print(c[i][j]);
				if( i == 0 ) cols++;
			}
			if( i > 0){
				System.out.println("\",");
			} 
			rows++;
		}
		System.err.println("rows: "+rows+" cols: "+cols);
		printFooter();
	}
	
	/**
	 * prints a last " and };
	 */
	private void printFooter() {
		System.out.println("\"\n};");
		
	}
	/**
	 * prints the header
	 */
	private void printHeader(Canvas canvas, int lengthX, int lengthY) {
		System.out.println("/* XPM */\nstatic char *quad_bw[] = {\n/* columns rows colors chars-per-pixel */");
		System.out.println("\""+lengthX+" "+lengthY+" "+canvas.getNumColors()+" "+canvas.getCharsPerPixel()+"\",");
		System.out.println("/* pixels */\n" +
				"\""+Canvas.BLACK+" c #000000\",\n" +
				"\""+Canvas.WHITE+" c #FFFFFF\",");
	}

	/**
	 * @return the lower left point and upper right points as an array of Points
	 */
	public Point[] getWindowDimensions() {
		Point[] retVal = {windowLowerLeftPoint, windowUpperRightPoint};
		return retVal;
	}
}