রবিবার, ১৬ অক্টোবর, ২০১১

Scroll Canvas in J2ME


It is fun to draw image, line, rectangle or strings in j2me. Sometimes objects we are drawing is larger than the screen (which is displayed). In that case we need to scroll the canvas to view the complete object. In order to do this we need to get the user keystroke and move the canvas according to key stroke.
In this tutorial we are displaying a large image and move it using up/down or left/right key.
We are using the following method to get key-stroke from user:

    public void keyReleased(int keyCode)
    {
        this.keyCode=keyCode;       
        int gameKey = getGameAction(keyCode);
        this.gameKey=gameKey;
        if ((gameKey == UP) || (keyCode == KEY_NUM2))
        {
            imgY+=5;
            //Moving Up 5 pixel here.
        }
        else if ((gameKey == LEFT) || (keyCode == KEY_NUM4))
        {
            imgX+=5;
            //Moving Left 5 pixel here.
        }
        else if ((gameKey == RIGHT) || (keyCode == KEY_NUM6))
        {
            imgX-=5;
            //Moving Right 5 pixel here.
        }
        else if ((gameKey == DOWN) || (keyCode == KEY_NUM8))
        {
            imgY-=5;
            //Moving Down 5 pixel here.
        }

        repaint();
        serviceRepaints();
    } 

Then in paint method we are passing the imgX and imgY variable and we are setting x coordinate of the image to imgX and y coordinate of the image to imgY. So that while we are moving image using up/down or left/right our image is moving. The pant method is as following:

    protected void paint(Graphics g)
    {
        this.g=g;
        try
        {
            imagePart = Image.createImage("/Education.jpg");
        }
        catch (IOException ex)
        {
            g.setColor(0xffffff);
            g.drawString("Failed to load image!", 0, 0, Graphics.TOP | Graphics.LEFT);
            return;
        }

        g.drawImage(imagePart, imgX,imgY, Graphics.TOP | Graphics.LEFT);
    }

I think it can help you. The source code of this project can be found from here.

বুধবার, ১৪ সেপ্টেম্বর, ২০১১

OCUnit Testing with Xcode

Unit Testing: Unit testing allows programmer to refactor code at a later time and make sure that every modules  works correctly. The procedure is to write test cases for all methods so that whenever a change causes a fault, it can be quickly identified and solved. OCUnit is a testing framework for Objective C in iOS and Mac OS. With OCUnit testing becomes integrated with development. We can test frameworks, bundles, or applications.These are some methods provided by OCUnit framework.

Unconditional Failure

STFail

Fails the test case.
STFail(failure_description, ...)

Equality Tests

STAssertEqualObjects

Fails the test case when two objects are different.
STAssertEqualObjects(object_1, object_2, failure_description, ...)

STAssertEquals

Fails the test case when two values are different.
STAssertEquals(value_1, value_2, failure_description, ...)

STAssertEqualsWithAccuracy

Fails the test case when the difference between two values is greater than a given value.
STAssertEqualsWithAccuracy(value_1, value_2, accuracy, failure_description, ...)

Nil Tests

STAssertNil

Fails the test case when a given expression is not nil.
STAssertNil(expression, failure_description, ...)

STAssertNotNil

Fails the test case when a given expression is nil.
STAssertNotNil(expression, failure_description, ...)

Boolean Tests

STAssertTrue

Fails the test case when a given expression is false.
STAssertTrue(expression, failure_description, ...)

STAssertFalse

Fails the test case when a given expression is true.
STAssertFalse(expression, failure_description, ...)

Exception Tests

STAssertThrows

Fails the test case when an expression doesn’t raise an exception.
STAssertThrows(expression, failure_description, ...)

STAssertThrowsSpecific

Fails the test case when an expression doesn’t raise an exception of a particular class.
STAssertThrowsSpecific(expression, exception_class, failure_description, ...)

STAssertThrowsSpecificNamed

Fails the test case when an expression doesn’t raise an exception of a particular class with a given name.
STAssertThrowsSpecificNamed(expression, exception_class, exception_name, failure_description, ...)

STAssertNoThrow

Fails the test case when an expression raises an exception.
STAssertNoThrow(expression, failure_description, ...)

STAssertNoThrowSpecific

Fails the test case when an expression raises an exception of a particular class.
STAssertNoThrowSpecific(expression, exception_class, failure_description, ...)

STAssertNoThrowSpecificNamed

Fails the test case when an expression doesn’t raise an exception of a particular class with a given name.
STAssertNoThrowSpecificNamed(expression, exception_class, exception_name, failure_description, ...)

STAssertTrueNoThrow

Fails the test case when an expression is false or raises an exception.
STAssertTrueNoThrow(expression, failure_description, ...)

STAssertFalseNoThrow

Fails the test case when an expression is true or raises an exception.
STAssertFalseNoThrow(expression, failure_description, ...)


in our next post we will provide a tutorial of OCUnit Testing with Xcode.