Thursday, December 16, 2010

Canvas in J2ME

A Canvas component represents a blank rectangular area of the screen onto which the application can draw or from which the application can trap input events from the user.


An application must subclass(extend) the Canvas class in order to get useful functionality such as creating a custom component. The paint method must be overridden in order to perform custom graphics on the canvas.


To View tutorial video Online CLICK HERE
(Download and watch for better quality)
To download Click here


Lets assume you create function paint(Graphics g).
You can view the list of functions available as well as their syntax ,usage and details can be seen by just typing the g.(g dot) and the list automatically opens up .Just move to required function and its details apppears as well.
Some of the common functions available in canvas are:-


  • public void drawImage(Image img, int x, int y, int anchor)
Draws the specified image by using the anchor point. The image can be drawn in different positions relative to the anchor point by passing the appropriate position constants. See anchor points.
If the source image contains transparent pixels, the corresponding pixels in the destination image must be left untouched. If the source image contains partially transparent pixels, a compositing operation must be performed with the destination pixels, leaving all pixels of the destination image fully opaque.
If img is the same as the destination of this Graphics object, the result is undefined. For copying areas within an Image, copyArea should be used instead.


Parameters:
img - the specified image to be drawn
x - the x coordinate of the anchor point
y - the y coordinate of the anchor point
anchor - the anchor point for positioning the image




  • public void drawString(String str, int x, int y, int anchor)
Draws the specified String using the current font and color. The x,y position is the position of the anchor point. See anchor points.


Parameters:
str - the String to be drawn
x - the x coordinate of the anchor point
y - the y coordinate of the anchor point
anchor - the anchor point for positioning the text


  • public void fillRect(int x, int y, int width, int height)
Fills the specified rectangle with the current color. If either width or height is zero or less, nothing is drawn.


Parameters:
x - the x coordinate of the rectangle to be filled
y - the y coordinate of the rectangle to be filled
width - the width of the rectangle to be filled
height - the height of the rectangle to be filled


  • public void setColor(int red, int green, int blue)
Sets the current color to the specified RGB values. All subsequent rendering operations will use this specified color.


Parameters:
red - the red component of the color being set in range 0-255
green - the green component of the color being set in range 0-255
blue - the blue component of the color being set in range 0-255


  • public int getColor()
Gets the current color.


  • public void setFont(Font font)
Sets the font for all subsequent text rendering operations. If font is null, it is equivalent to setFont(Font.getDefaultFont()).


  • public void drawRect(int x, int y, int width, int height)
Draws the outline of the specified rectangle using the current color and stroke style. The resulting rectangle will cover an area (width + 1) pixels wide by (height + 1) pixels tall. If either width or height is less than zero, nothing is drawn.


Parameters:
x - the x coordinate of the rectangle to be drawn
y - the y coordinate of the rectangle to be drawn
width - the width of the rectangle to be drawn
height - the height of the rectangle to be drawn


  • public void translate(int x, int y)
Translates the origin of the graphics context to the point (x, y) in the current coordinate system. All coordinates used in subsequent rendering operations on this graphics context will be relative to this new origin.
The effect of calls to translate() are cumulative. For example, calling translate(1, 2) and then translate(3, 4) results in a translation of (4, 6).






CODE:-


import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;




public class Midlet1 extends MIDlet {


    Canvas1 c1=new Canvas1();
    public void startApp() {
        Display.getDisplay(this).setCurrent(c1);
        c1.repaint();
    }


    public void pauseApp() {
    }


    public void destroyApp(boolean unconditional) {
    }
}
class Canvas1 extends Canvas
{


    protected void paint(Graphics g) {
        int width = getWidth();
        int height = getHeight();
        g.drawString("Hello",10 ,30, 20);
        g.drawRect(40, 40, width-80, height-80);
        g.setColor(220, 0, 0);
        g.fillRect(40, 40, width-80, height-80);
    }
}

                                                  Next Tutorial:-  Canvas Cont.(Simple game)

No comments:

Post a Comment

customised by Vaibhav