GraphMaker.java
import lejos.nxt.LCD;
 
/**
 * A class that draws a graph on the NXT display, drawing a value as one pixel.
 * 
 * @author Jakob Holm
 */
public class GraphMaker {
 
    private int minValue;
    private int maxValue;
    private int diff;
    private int x = 0;
    private int height = LCD.SCREEN_HEIGHT;
    private int width = LCD.SCREEN_WIDTH;
 
    /**
     * Create a graph maker with adjustable minimum and maximum values.
     * 
     * If the minimum is larger than the maximum the graph will be inverted.
     * 
     * @param minValue    The maximum possible value
     * @param maxValue    The minimum possible value
     */
    public GraphMaker(int minValue, int maxValue) {
        this.minValue = minValue;
        this.maxValue = maxValue;
        diff = maxValue - minValue;
    }
 
    /**
     * Create a graph maker with default minimum (0) and maximum (100) values.
     */
    public GraphMaker() {
        this.minValue = 0;
        this.maxValue = 100;
        diff = maxValue - minValue;
    }
 
    /**
     * Prints the value as the next point on the graph.
     * 
     * If the value is higher than the maximum value it will be drawn at the top. 
     * If the value is less than the minimum value nothing will be drawn.
     * 
     * @param value    The value to be printed
     */
    public void drawNext(int value) {
        // print the numeric value at the top right corner
        LCD.drawInt(value, 13, 0);
        // print next value
        value = (int) (height * ((value+0.0-minValue)/diff));
        if (value >= height) { value = height-1; }
        colorPixel(x, value);
        // increment x
        x++; if (x >= width) { x = 0; }
    }
 
    /**
     * Prints the values as the next point on the graph.
     * This can be used f.i. comparing readings from two sensors.
     * 
     * If the value is higher than the maximum value it will be drawn at the top. 
     * If the value is less than the minimum value nothing will be drawn.
     * 
     * @param value1    The first value to be printed
     * @param value2    The second value to be printed
     */
    public void drawNext(int value1, int value2) {
        // print the numeric value at the top right corner
        LCD.drawInt(value1, 13, 0);
        LCD.drawInt(value1, 13, 1);
        // print next values
        value1 = (int) (height * ((value1+0.0-minValue)/diff));
        if (value1 >= height) { value1 = height-1; }
        value2 = (int) (height * ((value2+0.0-minValue)/diff));
        if (value2 >= height) { value2 = height-1; }
        colorPixel(x, value1, value2);
        // increment x
        x++; if (x >= width) { x = 0; }
    }
 
    /**
     * Prints a single pixel (x,y) and clears the rest of the column x.
     * 
     * @param x
     * @param y
     */
    private void colorPixel(int x, int y) {
        // clear the column x
        for (int i = 0 ; i < height ; i++) {
            LCD.setPixel(x, i, 0);
        }
        // print (x,y)
        if (y >= 0) { LCD.setPixel(x, height - y, 1); }
        LCD.refresh();
    }
 
    /**
     * Prints a single pixel (x,y) and clears the rest of the column x.
     * 
     * @param x
     * @param y1
     * @param y2
     */
    private void colorPixel(int x, int y1, int y2) {
        // clear the column x
        for (int i = 0 ; i < height ; i++) {
            LCD.setPixel(x, i, 0);
        }
        // print (x,y1) and (x,y2)
        if (y1 >= 0) { LCD.setPixel(x, height - y1, 1); }
        if (y2 >= 0) { LCD.setPixel(x, height - y2, 1); }
        LCD.refresh();
    }
 
    public void setMax(int max) {
        maxValue = max;
        diff = maxValue - minValue;
    }
 
    public void setMin(int min) {
        minValue = min;
        diff = maxValue - minValue;
    }
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License