Draw Clip Area Using Canvas
This Example is going to draw a clip with SOLID line. In
this picture only solid line show the clipping area. To draw a solid line, we
have used SOLID keyword as given below:
setStrokeStyle(Graphics.SOLID) |
and to draw the dotted line we are using DOTTED keyword, as given below:
setStrokeStyle(Graphics.DOTTED) |
Methods, that are used in our example code..
- setColor()
- fillRect()
- getWidth()
- getHeight()
- g.drawLine()
- g.setClip()
Source Code of ClipExample.java
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class ClipExample extends MIDlet{
private Display display;
public void startApp(){
display = Display.getDisplay(this);
display.setCurrent (new ClipCanvas());
}
public void pauseApp(){}
public void destroyApp (boolean unconditional){}
public void commandAction(){}
}
class ClipCanvas extends Canvas{
public void paint(Graphics g){
g.setColor(255, 0, 0);
g.fillRect(0, 0, getWidth(), getHeight());
int map = Math.min(getWidth(), getHeight());
g.setColor(0, 0, 255);
g.setStrokeStyle(Graphics.DOTTED);
g.drawLine(0, 0, map, map);
g.setClip(map/4, map/4, map/2, map/2);
g.setStrokeStyle(Graphics.SOLID);
g.drawLine(0, 0, map, map);
}
}
|
Download Source Code