?
Solved

Bresenham's line tutorial

Posted on 2005-03-05
21
Medium Priority
?
1,187 Views
Last Modified: 2008-03-17
hello experts

im looking for the Bresenham's line done in java.

tutorials, source code, .java, links, you name it i want it... but remember it has to be in Java.

0
Comment
Question by:ellandrd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 13
  • 5
  • 3
21 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 1000 total points
ID: 13466618
0
 
LVL 92

Accepted Solution

by:
objects earned 1000 total points
ID: 13467449
0
 
LVL 16

Author Comment

by:ellandrd
ID: 13469712
objects

in your 1st URL, can you draw a line in any direction?

this is what i need to do? be able to draw in any octant...
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Author Comment

by:ellandrd
ID: 13469715
any other example... really looking for.java files, that give a really good explaination with code too
0
 
LVL 92

Expert Comment

by:objects
ID: 13469978
yes, just adjust the values accordingly.

heres some explanation


http://www.cs.helsinki.fi/group/goa/mallinnus/lines/bresenh.html
0
 
LVL 16

Author Comment

by:ellandrd
ID: 13469991
because what ive got, just allows you to draw in the 8th octant but want to know how to draw in all 8... thta why im looking for .java, example, tutorials, you name it, the works...
0
 
LVL 16

Author Comment

by:ellandrd
ID: 13470001
these tutroials dont show you how to draw the grid and get it to fill in the pixels?

mostly drawn the grid is what i need too...
0
 
LVL 16

Author Comment

by:ellandrd
ID: 13470006
http://www.cs.helsinki.fi/group/goa/mallinnus/lines/bresenh.html
the code in this tut is for c++. i need java...
0
 
LVL 16

Author Comment

by:ellandrd
ID: 13470021
ok on my panel to draw these line, im use the following:

g2.setPaint(Color.red);        
       
       
        // 1st octant
        int xStart = d.width / 2;
        int yStart = d.height / 2;
        int xEnd = 575;
        int yEnd = 400;      // Line start, end
        myLine.setStartPoint(xStart, yStart);            // Pass values to myLine
        myLine.setEndPoint(xEnd, yEnd);
        myLine.drawMyLine(g2);      


in my class myLine.java my ocde is:

import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.lang.Math;

public class MyLine {
   
    float x, y, e, m, xStart, yStart, xEnd, yEnd;
   
    /** Creates a new instance of MyLine */
    public MyLine() {
    }
 
    public void drawMyLine(Graphics2D g2Buffer) {
        x = xStart;
        y = yStart;
        e = 0;
        m = (yEnd - yStart)/(xEnd - xStart);
       
        g2Buffer.drawLine((int) x, (int) y, (int) x, (int) y);
        while ( x < xEnd) {
            x = x + 1;
            e = e + m;
            if  (e > 0.5) {
                y = y + 1;
                e = e - 1;
            }                                          //  Non-Integer
            g2Buffer.drawLine((int) x, (int) y, (int) x, (int) y);
        }
   
<<<<<<<<<<<>>>>>>>>>>>>
        while ( y < yEnd) {
                  y = y + 1;
                  e = e + m;
                  if  (e > 0.5) {
                        x = x + 1;
                        e = e - 1;
                  }                                          //  Non-Integer
                  g2Buffer.drawLine((int) x, (int) y, (int) x, (int) y);
            }
<<<<<<<<<<<<<<>>>>>>>>>>
THIS LINE DONT DRAW
    }

    public void setStartPoint(float x, float y) {
        xStart = x;
        yStart = y;
    }
   
    public void setEndPoint(float x, float y) {
        xEnd = x;
        yEnd = y;
    }
}

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13470815
>>mostly drawn the grid is what i need too...

The source at the very first link i posted shows how to draw the grid

http://www.cse.psu.edu/~plassman/sjthomas/bres/Line.java
0
 
LVL 16

Author Comment

by:ellandrd
ID: 13470924
yes but i dont under stand this code? im only learning java and have nobody to help me unless i ask you guys here.

the reason i asked for tutorials, was, because i was hoping to get full examples, to show you how to draw girds, explain and provide the code if possible for all 8 octants... and really hoping to get a fully working example like a .java or something, but all ive got is code in c++ or tutorials that explain it with code for 1st octant and no grid.

this is no good to me, its like giving my mother this stuff, she wuldnt have a clue where to start! thats what im like. i dont know java (thats why i want to learn it) everything ive learned to date is by online tutorials. java is next to learn, but with tutorials like these, i aint getting to far!

even if i got puesdo code for a full example with grid, i'd do my best to translate into java and then im come here for help the with errors, but so far i havnt even got this far!
0
 
LVL 16

Author Comment

by:ellandrd
ID: 13470933
is it possible to convert .class into .java to see the code?
0
 
LVL 16

Author Comment

by:ellandrd
ID: 13471085
ok got a gird draw using CEHJ link, but the rest of that code, is double dutch - dont understand? how am i meant to draw lines on this and fill in pixel??
0
 
LVL 16

Author Comment

by:ellandrd
ID: 13471470
ok guys, hoping you can help?

this is what ive got so far.  ive managed to draw lines in the 1st octant. but not on grid...

what ive got is 2 classes.

the grid panel class and a class called myLine that holds the algorithm to draw lines in all 8 octants.

setGridPanel is where i want to click on a gird and draw lines in all 8 octants

my code :

import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.lang.Math;

public class SetGridPanel extends javax.swing.JPanel {
   
    private BufferedImage theBuffer;
    private Graphics2D g2;
    private Dimension d;
   
    private MyLine myLine = new MyLine();
   
    public SetGridPanel() {
        initComponents();
    }
   
    private void initComponents() {

        setLayout(new java.awt.BorderLayout());

        setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0)));
        setToolTipText("Grid Panel");
        addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                formMouseDragged(evt);
            }
        });
        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                formMousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                formMouseReleased(evt);
            }
        });

    }
   
    private void formMousePressed(java.awt.event.MouseEvent evt) {
       
    }
   
    private void formMouseReleased(java.awt.event.MouseEvent evt) {
       
    }
   
    private void formMouseDragged(java.awt.event.MouseEvent evt) {
       
    }
   
    public void paintComponent(Graphics g){
        d = getSize();
        if (theBuffer == null){
            theBuffer = new BufferedImage(d.width, d.height,BufferedImage.TYPE_INT_ARGB_PRE);
            g2 = (Graphics2D) theBuffer.getGraphics();
            g2.setColor(Color.white);
            g2.fillRect(0, 0, d.width, d.height);
        }
 

        /////////////////// 1ST OCTANT START/////////////////

        // Draw the line
        g2.setPaint(Color.green);
        int xStart = d.width / 2;
        int yStart = d.height / 2;
        int xEnd = 575;
        int yEnd = 400;      // Line start, end
        myLine.setStartPoint(xStart, yStart);            // Pass values to myLine
        myLine.setEndPoint(xEnd, yEnd);
        myLine.drawMyLine(g2);                        // Draw the line

        /////////////////// 1ST OCTANT END/////////////////
       
        g.drawImage(theBuffer, 0, 0, this);
    }
}


import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.lang.Math;

public class MyLine {
   
    private Graphics2D g2Buffer;
    float x, y, e, m, xStart, yStart, xEnd, yEnd;
   
    public MyLine() {
    }
 
    public void drawMyLine(Graphics2D g2Buffer) {
        x = xStart;
        y = yStart;
        e = 0;
        m = (yEnd - yStart)/(xEnd - xStart);
       
        g2Buffer.drawLine((int) x, (int) y, (int) x, (int) y);
        while ( x < xEnd) {
            x = x + 1;
            e = e + m;
            if  (e > 0.5) {
                y = y + 1;
                e = e - 1;
            }
            g2Buffer.drawLine((int) x, (int) y, (int) x, (int) y);
        }
    }

    public void setStartPoint(float x, float y) {
        xStart = x;
        yStart = y;
    }
   
    public void setEndPoint(float x, float y) {
        xEnd = x;
        yEnd = y;
    }
}

can you show me how to create a grid that i can draw lines on fill in the squares that the line is in...and also i need some guide to completing the rest of the algorithm for the other 7 octants...

please help

ellandrd
0
 
LVL 92

Expert Comment

by:objects
ID: 13472102
> the code in this tut is for c++. i need java...

Was just intended as a explanation of the algorithm, you can use one of the java code examples I provided.
0
 
LVL 92

Expert Comment

by:objects
ID: 13472140
> can you show me how to create a grid that i can draw lines on

you just need to paint a series of parallel and vertical lines (no need for bresenham here).
No need to create anything either just add two loops (one for vertical and one for horizontal lines).

 public void drawGrid (Graphics g, int gridWidth, int gridHeight)
 {
      for (int i=gridWidth; i < getWidth(); i +=gridWidth)
      {
           g.drawLine (i, 0, i, getHeight();
      }
      for (int i=gridHeight; i < getHeight(); i+=gridHeight)
      {
          g.drawLine (0, i, getWidth(), i);
      }
    }

> and also i need some guide to completing the rest of the algorithm for the other 7 octants...

just create another 7 instance of MyLine
0
 
LVL 16

Author Comment

by:ellandrd
ID: 13474984
ok so how will this fill in the pixels on the grid when a line is drawn?

i have your grid drawn.

<<<< just create another 7 instance of MyLine

in myline class the code is only for the first octant, i need little help getting the others working too...
0
 
LVL 92

Expert Comment

by:objects
ID: 13482215
create additional instance of MyLine, one for each line you need to draw.
0
 
LVL 16

Author Comment

by:ellandrd
ID: 13484584
yes you told me that a few posts ago,

im looking for help on how to draw lines on the grid. -->  how to fill in the pixels where the line is drawn.  none of these tutorials above, tell you how to do this part!!, they just all tell you how to draw lines in all 8 octants but the most important part is filling in pixels in grid...

this is what i require help on? filling in pixel on grid!! im not worried bout drawn lines in all octant yet, i can draw line in 1st octant and now i want to fill in the pixels on grid...

0
 
LVL 16

Author Comment

by:ellandrd
ID: 13486964
ok i got my line drawn in all 8 octants, so its the filling in of the pixels on the grid next?

any help on this one?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13498377
:-)

I see you now have a new question ...
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question