Bresenham's line drawing algorithm program in Java | Wave the world

Bresenham's line drawing algorithm program in Java

/*
    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 :)


2 comments:

  1. nice, by using multi-threading it shows every pixel

    ReplyDelete
  2. <TAKE A MINUTE OF YOUR TIME TO READ THIS. This is my story, and how one person changed my life for good.
    I lost my job and there was no hope for me. I suffered looking for a job and luckily I came across
    Mr Bernie DOran on a review about Forex, NFT, Binary, and Crypto trade. I borrowed
    $500 from a colleague to invest with Mr Bernie, thank goodness I made a profit of $4000 on the first week,
    reinvested 1500, and cashed out the remaining balance directly to my bank account. I. was able to pay my friend,
    and convinced him to invest as well. all this happens within a week. I'm very grateful to him for helping me
    manage my trading account. I will advise all of you who want to invest for the future and a better tomorrow
    should start with Mr Bernie DOran. Some might think this is a joke, but a trial won't hurt. give
    this a try and come back to thank me after.
    Contact him via:
    WhatsApp:+1 (424) 285-0682
    Gmail: Bernie.doranfx01@gmail.com

    ReplyDelete

 

Pro

About