Switch to full style
General Java code examples
Post a reply

Bresenham (midpoint) algorithm (integer DDA) drawing line

Mon Jan 28, 2013 8:33 pm

Bresenham (midpoint) algorithm (integer DDA) implementation using java , note in this version the drawing is done on mobile platform but the drawing code can be used on all platforms.
java code
package aa;
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;

/*
* Line.java
*
* Created on 10 , 2007, 11:16

*/

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

/** Creates a new instance of Line */
public Line()
{
okCommand=new Command("OK",Command.OK,0);
form=new Form("Enter The Cordinates");
x1Field=new TextField("X1: ","",20,TextField.DECIMAL);
y1Field=new TextField("Y1: ","",20,TextField.DECIMAL);
x2Field=new TextField("X2: ","",20,TextField.DECIMAL);
y2Field=new TextField("Y2: ","",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 x=0,y=0,dx=0,dy=0;
int incE,incNE,c;

dx=x2-x1;
dy=y2-y1;
c=dy*2-dx;
incE=dy*2;
incNE=(dy-dx)*2;

y=y1;
for(int i=x1;i<x2;i++)
{
g.drawLine(i,y,i,y);
if(c<=0)
{
c=c+incE;
}
else
{
c=c+incNE;
y++;
}
}
g.drawLine(x2,y,x2,y);

}

int x1=0;
int x2=0;
int y1=0;
int y2=0;

}




Post a reply
  Related Posts  to : Bresenham (midpoint) algorithm (integer DDA) drawing line
 Line Midpoint algorithm (J2ME)     -  
 DDA Line Drawing Algorithm     -  
 Circle drawing using Bresenham     -  
 Circle direct drawing algorithm     -  
 Circle drawing using Polar based algorithm with C++     -  
 Reading a File Line by Line in php     -  
 integer value overflow     -  
 Invalid integer formats     -  
 finding the largest integer     -  
 Dividing two Integer values     -  

Topic Tags

Java Graphics