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.

Draw Clip Area Using Canvas

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(25500);
  g.fillRect(00, getWidth(), getHeight());

  int map = Math.min(getWidth(), getHeight());  
  g.setColor(00255);

  g.setStrokeStyle(Graphics.DOTTED);
  g.drawLine(00, map, map);

  g.setClip(map/4, map/4, map/2, map/2);

  g.setStrokeStyle(Graphics.SOLID);
  g.drawLine(00, map, map);
  }
}

 

Download Source Code