/*
Bresenham's Line Drawing Algorithm
Created By : Pirate
*/
import java.awt.Graphics;
import java.util.Scanner;
import javax.swing.*;
public class Line extends JFrame implements Runnable{
int a,b,x,y,p=0,q=0;
public static void main(String[] args) {
System.out.println("*** Bresenham Line Algorithm ***");
System.out.println("\nEnter the starting point of line");
Scanner scan = new Scanner(System.in);
Line line = new Line();
line.a = scan.nextInt();
line. b = scan.nextInt();
System.out.println("\nEnter the ending point of line");
line.x = scan.nextInt();
line.y = scan.nextInt();
Thread t= new Thread(line);
t.start();
scan.close();
}
public Line(){
this.pack();
this.setVisible(true);
this.setTitle("Wave The World");
this.setSize(400, 400);
this.setContentPane(getContentPane());
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.drawLine(a, b, p, q);
}
public void putPixel(int x,int y){
p=x;
q=y;
repaint();
}
public void run(){
Bre_Line();
}
public void Bre_Line(){
int x1=a;
int y1=b;
int dx, dy;
x2=x;
y2=y;
dx=x2-x1;
if(dx < 0){
dx = dx*(-1);
}
dy=y2-y1;
if(dy < 0){
dy= dy*(-1);
}
int temp;
if(dx<dy){
temp = dx;
dx = dy;
dy = temp;
}
int p=2*dy-dx;
i=dx;
while(i>0){
try{
Thread.sleep(100);
}catch(Exception e){
System.out.println(e.getMessage());
}
putPixel(x1,y1);
if(p<0)
{
x1=x1+1;
p=p+2*dy;
}
else
{
x1=x1+1;
y1=y1+1;
p=p+2*dy-2*dx;
}
i--;
}
}
}
Output :
Bresenham's Line Drawing Algorithm
Created By : Pirate
*/
import java.awt.Graphics;
import java.util.Scanner;
import javax.swing.*;
public class Line extends JFrame implements Runnable{
int a,b,x,y,p=0,q=0;
public static void main(String[] args) {
System.out.println("*** Bresenham Line Algorithm ***");
System.out.println("\nEnter the starting point of line");
Scanner scan = new Scanner(System.in);
Line line = new Line();
line.a = scan.nextInt();
line. b = scan.nextInt();
System.out.println("\nEnter the ending point of line");
line.x = scan.nextInt();
line.y = scan.nextInt();
Thread t= new Thread(line);
t.start();
scan.close();
}
public Line(){
this.pack();
this.setVisible(true);
this.setTitle("Wave The World");
this.setSize(400, 400);
this.setContentPane(getContentPane());
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.drawLine(a, b, p, q);
}
public void putPixel(int x,int y){
p=x;
q=y;
repaint();
}
public void run(){
Bre_Line();
}
public void Bre_Line(){
int x1=a;
int y1=b;
int dx, dy;
x2=x;
y2=y;
dx=x2-x1;
if(dx < 0){
dx = dx*(-1);
}
dy=y2-y1;
if(dy < 0){
dy= dy*(-1);
}
int temp;
if(dx<dy){
temp = dx;
dx = dy;
dy = temp;
}
int p=2*dy-dx;
i=dx;
while(i>0){
try{
Thread.sleep(100);
}catch(Exception e){
System.out.println(e.getMessage());
}
putPixel(x1,y1);
if(p<0)
{
x1=x1+1;
p=p+2*dy;
}
else
{
x1=x1+1;
y1=y1+1;
p=p+2*dy-2*dx;
}
i--;
}
}
}
Output :
Enjoy :)