FileDocCategorySizeDatePackage
TestGraphicsClipping.javaAPI DocphoneME MR2 API (J2ME)3481Wed May 02 18:00:08 BST 2007javax.microedition.lcdui

TestGraphicsClipping

public class TestGraphicsClipping extends TestCase

Fields Summary
static final int
BACKGROUND_COLOR
int
width
int
height
int
backgroundColor
Constructors Summary
Methods Summary
voiddraw(Graphics g)


       
	g.setColor(0);
	g.fillRect(0, 0, width, height);
    
public voidrunTests()

        declare("testOne");
        testOne();
    
voidtestOne()

        int[] unclippedData = new int[width*height];
        int[] clippedData = new int[width*height];

	Image image = Image.createImage(width, height);
	Graphics g = image.getGraphics();

	backgroundColor = g.getDisplayColor(BACKGROUND_COLOR);

	for (int cx = 1; cx < width; cx++) {
	    for (int cy = 1; cy < height; cy++) {
		for (int cw = 1; cw < width-cx; cw++) {
		    for (int ch = 1; ch < height-cy; ch++) {
			g.setClip(0, 0, width, height);
			g.setColor(backgroundColor);
			g.fillRect(0, 0, width, height);
			draw(g);
			image.getRGB(unclippedData, 0, width, 0, 0, width, height);

			g.setColor(backgroundColor);
			g.fillRect(0, 0, width, height);
			g.setClip(cx, cy, cw, ch);
			draw(g);
			image.getRGB(clippedData, 0, width, 0, 0, width, height);
	
			boolean result = verifyClip(unclippedData, clippedData, cx, cy, cw, ch);

			assertTrue(result);
		    }
		}
	    }
	}
    
booleanverifyClip(int[] unclippedData, int[] clippedData, int cx, int cy, int cw, int ch)

        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
		int index = y*width + x;
		if (y >= cy && y < cy+ch && x >= cx && x < cx + cw) {
		    /* should match unclipped version when alpha is ignored */
		    if ((clippedData[index]) != (unclippedData[index])) {
			System.out.println("FAILURE: inside clip (" + x + "," + y + ")" + 
					   " clipped=" + 
					   Integer.toHexString(clippedData[index]) + 
					   " unclipped=" + 
					   Integer.toHexString(unclippedData[index]));
			return false;
		    }
		} else {
		    /* should be background color when alpha is ignored */
		    if ((clippedData[index]&0x00ffffff) != (backgroundColor&0xffffff)) {
			System.out.println("FAILURE: outside clip (" + x + "," + y + ")" + 
					   " clipped=" + 
					   Integer.toHexString(clippedData[index]&0xffffff) + 
					   " backgroundColor=" + 
					   Integer.toHexString(backgroundColor&0xffffff));
			return false;
		    }
		}
            }
        }

	return true;