Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

Cohen clipping on J2me

Thu May 03, 2007 11:10 pm

This is a cohen clipping algorithm implemented using java 2 microedition(j2me) for mobile application.
Any Points outside the window is not drawen.




java code
package hello;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;


/**
*
* @author Mohammed

*/
public class cohen extends MIDlet implements CommandListener {
Form form;
TextField x1Field,y1Field,x2Field,y2Field;
Command okCommand;
myCanvas can;

/** Creates a new instance of Line */
public cohen() {
okCommand=new Command("OK",Command.OK,0);
form=new Form("Enter The Cordinates");
x1Field=new TextField("X: ","",20,TextField.DECIMAL);
y1Field=new TextField("Y: ","",20,TextField.DECIMAL);
x2Field=new TextField("X: ","",20,TextField.DECIMAL);
y2Field=new TextField("Y: ","",20,TextField.DECIMAL);

form.append(x1Field);
form.append(y1Field);
form.append(x2Field);
form.append(y2Field);

form.addCommand(okCommand);
form.setCommandListener(this);
}

protected void startApp() throws MIDletStateChangeException {
Display display=Display.getDisplay(this);
display.setCurrent(form);
}

protected void pauseApp() {
}

protected void destroyApp(boolean b) throws MIDletStateChangeException {
}

public void commandAction(Command command, Displayable displayable) {
if(command.getCommandType()==Command.OK) {

int x1=Integer.parseInt(x1Field.getString());
int y1=Integer.parseInt(y1Field.getString());
int x2=Integer.parseInt(x2Field.getString());
int y2=Integer.parseInt(y2Field.getString());

can=new myCanvas(x1,y1,x2,y2);

Display.getDisplay(this).setCurrent(can);
can.repaint();

}
}

}
class myCanvas extends Canvas {
public myCanvas(int x1,int y1,int x2,int y2) {
this.x1=x1;
this.y1=y1;
this.x2=x2;
this.y2=y2;

}
protected void paint(Graphics g) {
g.fillRect(0,0,300,300);
g.setColor(200,23,43);

int dy=y2-y1;
int dx=x2-x1;
System.out.println(" ");
System.out.println(" "+dx);
System.out.println(" "+dy);
System.out.println(" ");
m=((float)dy/dx);
System.out.println(" "+m);
b=-m*x2+y2;
int x=0;
int y=0;
g.drawRect( min_x+115,150-max_y,200,200);
if(x1max_x) {
tag1[2]=1;//Right
}

if(x2max_x) {
tag2[2]=1;//Right
}
////////////////////////////////////////////////////////////////////
if(y1max_y) {
tag1[0]=1;//Top
}

if(y2max_y) {
tag2[0]=1;//Top
}
System.out.println(" ");
System.out.println("Point 1: ");
for(int i=0;i<4;i++)
System.out.print(tag1[i]);
System.out.println(" ");
System.out.println("Point 2: ");
for(int i=0;i<4;i++)
System.out.print(tag2[i]);
System.out.println(" ");
for(int i=0;i<4;i++) {

And_tag[i]=(byte)(tag1[i]&tag2[i]);
if(And_tag[i]==1) {
System.out.println("Total Rejected (Outside the window !!!");
}

}
for(int i=0;i<4;i++) {
if(tag1[i]==1) {
if(i<2) {

x1=move(i);
System.out.println(" x1 = "+ x1+" "+i);
if(i==0)
y1=max_y;
else
y1=min_y;
} else {

y1=move(i);
System.out.println("y1 = "+y1+" "+i);
if(i==2)
x1=max_x;
else
x1=min_x;
}
}
}
for(int i=0;i<4;i++) {
if(tag2[i]==1) {
if(i<2) {

x2=move(i);
System.out.println("x2 = "+x2+" "+i);
if(i==0)
y2=max_y;
else
y2=min_y;
} else {

y2=move(i);
System.out.println("y2 = "+y2+" "+i);
if(i==2)
x2=max_x;
else
x2=min_x;
}
}
}
g.drawLine(x1+115,150-y1,x2+115,150-y2);
}
public int move(int i) {
System.out.println(" "+m);
switch(i) {
case 0:return ((int)((max_y-b)/m));
case 1:return ((int)((min_y-b)/m));
case 2:return (int)(max_x*m+b);
case 3:return (int)(min_x*m+b);

}
return -1;
}
int min_x=-100;
int max_x=100;
int min_y=-100;
int max_y=100;
byte[] tag1=new byte[4];
byte[] tag2=new byte[4];
byte[] And_tag=new byte[4];
int ones1;
int ones2;
float m;
float b;
int x1=0;
int y1=0;
int x2=0;
int y2=0;

}




Post a reply
  Related Posts  to : Cohen clipping on J2me
 java Clipping     -  
 Area clipping     -  
 What is the J2ME!!!!     -  
 J2ME     -  
 J2ME Datagrid     -  
 calendet j2me     -  
 J2me some rules     -  
 bluetooth in j2me     -  
 j2me Red Snake 3D     -  
 looking for J2ME opportunities     -  

Topic Tags

Java Graphics, Java J2ME