Uploaded to www.rart.com/j2me-presentation/html_Source/ March 19, 2002
This class tests ColorME. It is invoked by ColorTest, a midlet handling most user interactions. In particular the paintScreen method handles the different testing alternatives.
This version has been tested with j2mewtk 1.0.2 and 1.0.3
/*
* ColorCanvas Version 1.0 010815
*
* Jan Aminoff , August 2001
* © A-Square 2001.
*
*/
import javax.microedition.lcdui.*;
import rartme.ColorME;
/**
* ColorCanvas.java Version 1.0 010815
* Copyright (c) 2001 A-Square, Inc. All Rights Reserved.
*
* This software is designed to test and demonstrate the capabilities
* of the ColorME class.
*
* Version 1.0 released in August 2001 as part of the Banner project
*
* This version should compile and run under J2MEWTK 1.0.1
*
*/
public class ColorCanvas extends Canvas {
// implements Runnable{
Display display;
// Color variables
boolean color; // color or grayscale
int numColor; // number of grayscales or colors
int testnr; // the current test
int tmin = 1; // minimum test nr
int tmax = 3; // maximum test nr
int xm; // width of available display
int ym; // height of available display
int xs,ys,is,in;
int size;
private Graphics gc; // used for drawing
private Image imagebuffer; // Used for drawing when double buffering
private boolean manbuff; // true if not automatically doublebuffered
// or if unable to allocate memory for doublebuffering
public ColorCanvas(Display d, int testnr) {
xm = getWidth();
ym = getHeight();
display = d;
this.testnr=setTestNr(testnr);
// Color Handling
color = display.isColor();
numColor = display.numColors();
numColor = (color) ? numColor : - numColor;
System.out.println("\n numColor " + numColor);
// Check DoubleBuffering
boolean dbuff = isDoubleBuffered();
if (dbuff) manbuff = false;
else{
try {
// allocate an offscreen graphics
imagebuffer = Image.createImage(xm, ym);
gc = imagebuffer.getGraphics();
manbuff = true;
}
catch (Exception e) { manbuff = false; }
}
// initialize testing
} // end constructor
/*
* set testnr to between tmin = 1 (lowest testnr) and
* tmax (max testnr)
*/
public int setTestNr( int n){
int t = (n < tmin)? tmin: (n > tmax)? tmax: n;
return t;
}
public void start() {
display.setCurrent(this);
repaint();
} // end start
public void pause(){} // Do nothing for the time being
/**
* The paint method is prescribed by the MIDlet.
*
* The paint method is called periodically. It has two branches depending
* on wether or not we have automatic or programmed double
* buffering.
* If the imagebuffer is null we have to paint this midlet directly
* using the graphichs context g.
*/
public void paint(Graphics g){
if (manbuff) {
paintScreen(gc);
g.drawImage(imagebuffer, 0, 0, Graphics.TOP|Graphics.LEFT);
}
else paintScreen(g);
} // end paint
/**
* Draws the drawing frame (which also contains the logo(s)) and the
* controls using either the automatically doublebuffered context g
* or the context gc created from the offscrean image (see constructor)
*/
private void paintScreen(Graphics g) {
g.setColor(0xffffff); // white
g.fillRect(0, 0, xm, ym);
g.setColor(0); // black
g.drawRect(0, 0, xm-1, ym-1);
g.drawString("Test "+testnr, 3, 1, 0);
if (testnr == 1){
int x=0;
int y=20;
if (!color){
g.drawString("Needs Color Device!", 3, ym-34, 0);
} else {
g.drawString("RGB fr. Hue", 3, 12, 0);
for (int i=0; i<360; i++){
x = 1+i%(xm-2);
if (x == 1) y+=10;
int newcolor = ColorME.mkColorfromHue(i);
g.setColor(newcolor);
g.drawLine( x, y, x, y+10);
}
}
}
else if (testnr == 2){
is = 0;
in = (ym-14)/12;
int x=5;
int y=14;
if (!color){
g.drawString("Needs Color Device!", 3, ym-34, 0);
} else {
g.drawString("Standard Colors A", 3, 12, 0);
for (int i=is; i<in; i++){
y+=12;
int newcolor = ColorME.colorvalues[i];
g.setColor(newcolor);
g.fillRect( x, y+2, 10, 10);
g.setColor(0);
g.drawString(ColorME.colornames[i], 30, y-2, 0);
}
}
}
else if (testnr == 3){
is = (ym-14)/12+1;
in = 2*(ym-14)/12;
int x=5;
int y=14;
if (!color){
g.drawString("Needs Color Device!", 3, ym-34, 0);
} else {
g.drawString("Standard Colors B", 3, 12, 0);
for (int i=is; i<in; i++){
y+=12;
if (i == ColorME.colorvalues.length) break;
int newcolor = ColorME.colorvalues[i];
g.setColor(newcolor);
g.fillRect( x, y+2, 10, 10);
g.setColor(0);
g.drawString(ColorME.colornames[i], 30, y-2, 0);
}
if (y>ym-12) g.drawString( "That's all!", 15, y, 0);
}
}else g.drawString("No such testnr "+testnr, 3, ym-34, 0);
} // end drawscreen
/**
* Handle a pen down event.
*/
public void keyPressed( int keyCode) {
int action = getGameAction(keyCode);
int t = testnr;
//System.out.println("In keyPressed testnr "+t);
int tnew=tmin;
switch (action) {
case LEFT:
tnew = setTestNr(t-1);
break;
case RIGHT:
tnew = setTestNr(t+1);
break;
case UP:
tnew = tmin;
break;
case DOWN:
tnew = tmax;
break;
}
//System.out.println("In keyPressed tnew "+tnew);
testnr = tnew;
repaint();
} // end keyPressed
} // END ColorCanvas