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:-
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
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
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
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
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
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)
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)
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)
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)
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)
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()
- public void setFont(Font font)
- public void drawRect(int x, int y, int width, int height)
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)
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