Link to home
Start Free TrialLog in
Avatar of ellandrd
ellandrdFlag for Ireland

asked on

QUESTION for sudhakar_koundinya

OK im creating a 2d drawing tool

i have a setGridPanel and MyLine Class

myline class holds the bresenham algorithm to draw lines in all octants

and my setgridpanel is where the user can draw lines by mouse clicks, (dragging mouse and mouse release)

as you probably are aware, the pixels get filled in on the grid!

but im having alot of trouble getting this to work!
Avatar of ellandrd
ellandrd
Flag of Ireland image

ASKER

first off all, it is soooo hard to find tutorials on bresenham, they all tell you how to draw lines on all 8 octant, but not how to fill in the pixels on the gird?



Avatar of sudhakar_koundinya
sudhakar_koundinya

can you explain the trouble??


Are u meant  that on dragging of mouse, Line should be drawn??

since now ive just been search google etc to examples and when i find them, i try to get them to work, and you got the last example working for me.

and now you can earn alots and lots of points now if you can help me...

here is my SetGridPanel class

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;
   
    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);
        }
}


what i need here is to draw a grid as ive got an algoirthm to draw lines in all octant

then i need to fill tin the pixels on the grid where the line passes through
first of all, im only learning java and need to draw grid for work with bresenham algorithm
Basically problem in Java is we do not have methods like SetPixel and GetPixel like methods as we have in other languages like C/C++/Pascal.

So while working with complex algorithms like bresenham's  line algorithm, to fill pixels in a grid, they depends on available awt methods


Regards
ok here is an updated version of SetGridPanel() class that works with MyLine() class:


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();
   
    float xStart, yStart, xEnd, yEnd;

    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);
        }
       
        xStart = 30.0f;
        yStart = 30.0f;
        xEnd = 210.0f;
        yEnd = 210.0f;
        myline.setStartPoint(xStart,yStart);
        myline.setEndPoint(xEnd,yEnd);
        myline.drawMyLine(g2);
       
        g.drawImage(theBuffer, 0, 0, this);
    }
}

MyLine class:


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

public class MyLine {
   
    public MyLine(){}
   
    float xStart, yStart, xEnd, yEnd;
   
    public void setStartPoint(float x, float y) {
        xStart = x;
        yStart = y;
    }
   
    public void setEndPoint(float x, float y) {
        xEnd = x;
        yEnd = y;
    }
   
    public void drawMyLine(Graphics2D g2Buffer) {
 
        int x1 = (int) xStart;
        int y1 = (int) yStart;
        int x2 = (int) xEnd;
        int y2 = (int) yEnd;
 
        int temp;
        int dy_neg = 1;
        int dx_neg = 1;
        int switch_x_y = 0;
        int neg_slope = 0;
        int tempx, tempy;
        int dx = x2 - x1;
 
        if(dx == 0) {
            if(y1 > y2) {
                for(int n = y2; n <= y1; n++) {
                    drawLines(x1,n,g2Buffer);
                }
                return;
            }
            else {
                for(int n = y1; n <= y2; n++) {
                    drawLines(x1,n,g2Buffer);
                }
                return;
            }
        }
 
        int dy = y2 - y1;
        if(dy == 0) {
            if(x1 > x2) {
                for(int n = x2; n <= x1; n++) {
                    drawLines(n,y1,g2Buffer);
                }
                return;
            }
            else {
                for(int n = x1; n <= x2; n++) {
                    drawLines(n,y1,g2Buffer);
                }
                return;
            }
        }
        float m = (float) dy/dx;
 
        if(m > 1 || m < -1) {
            temp = x1;
            x1 = y1;
            y1 = temp;
            temp = x2;
            x2 = y2;
            y2 = temp;
            dx = x2 - x1;
            dy = y2 - y1;
            m = (float) dy/dx;
            switch_x_y = 1;
        }
 
        if(x1 > x2) {
            temp = x1;
            x1 = x2;
            x2 = temp;
            temp = y1;
            y1 = y2;
            y2 = temp;
            dx = x2 - x1;
            dy = y2 - y1;
            m = (float) dy/dx;
        }
 
        if(m < 0) {
            if(dy < 0) {
                dy_neg = -1;
                dx_neg = 1;
            }
            else {
                dy_neg = 1;
                dx_neg = -1;
            }
            neg_slope = 1;
        }
 
        int d = 2 * (dy * dy_neg) - (dx * dx_neg);
        int incrH = 2 * dy * dy_neg;
        int incrHV = 2 * ( (dy * dy_neg)  - (dx * dx_neg) );
        int x = x1;
        int y = y1;
        tempx = x;
        tempy = y;
 
        if(switch_x_y == 1) {
            temp = x;
            x = y;
            y = temp;
        }
        drawLines(x, y,g2Buffer);
        x = tempx;
        y = tempy;
 
        while(x < x2) {
            if(d <= 0) {
                x++;
                d += incrH;
            }
            else {
                d += incrHV;
                x++;
                if(neg_slope == 0) {
                    y ++;
                }
                else {
                    y --;
                }
            }
            tempx = x;
            tempy = y;
 
            if(switch_x_y == 1) {
                temp = x;
                x = y;
                y = temp;
            }
            drawLines(x, y,g2Buffer);
            x = tempx;
            y = tempy;
        }
    }
 
    private void drawLines(int x, int y, Graphics2D g2Buffer) {
        g2Buffer.setColor(Color.blue);
        g2Buffer.drawLine(x,y,x,y);
    }
}

as i said above, i need to draw a grid and where the line passes throught a pixel fill it in...

plus i dont know if this alogithm is the correct one for filling in pixels on grid as i cant seem to find one unless you know of one?
well how come the exmaple i found in the applet worked and it was in java?

i think i need to insert that code form applet into my code adn get it to work?

would that work?
Here is an idea to draw a grid

int i=0;
 for (i=1; i < 1; i+=1)
         g.drawLine (i, 0, i, 1);
        for (i=1; i < 1; i+=1)
         g.drawLine (0, i, 1, i);
would that work?>>
Yes

>> well how come the exmaple i found in the applet worked and it was in java?

You can find that they have used fillRect method to draw a Pixel

can you help me incode the applet code into my my application?
are you working on example or have you gone?

just that the other guy just left me in the dark after i told him what i was trying to do...
if your working on example, i'd like to have pixel width 25 & height 25 so it is easy to see for other users
OK, Give me sometime. I got some urgent works to be completed.

I can try to post the answer by this night or tomorrow morning.

It is 6:00 pm here
ok, its 1.00 pm here
 thank you
So are you from Europe?? Probably U.K

as time diff for G.M.T and our's is 5:30 hrs
yes scotland!
i work for a oil company
well any luck?

how are you getting?
why is this question only for sudhakar??:)
Thanks
because she was helping me with my previous project and provided excellant help, and i have other question for her to answer so i opened another question after closing the previous one!
are you still helping me?
yes.
 no worries :-)

I am getting confusions in understanding bresenham's algorithm. Just copied and paste to new class didn't solve the problem.

And also as I have maximum work to be done to resolve the issues of my curent project. Last night I went to home at night 1:00 AM. Still some problems are there in code.

That is the reason it is becoming slow to me in understanding the bresenham's code

BTW, is this urgent?? Or will you give me some time??

Best regards
Sudhakar
kinda urgent! company wants it by this monday coming
Wow ..... sudhakar_koundinya is a girl ......  
ASKER CERTIFIED SOLUTION
Avatar of sudhakar_koundinya
sudhakar_koundinya

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial