ellandrd
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!
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!
can you explain the trouble??
Are u meant that on dragging of mouse, Line should be drawn??
Are u meant that on dragging of mouse, Line should be drawn??
ASKER
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.LineBor der(new java.awt.Color(0, 0, 0)));
setToolTipText("Grid Panel");
addMouseMotionListener(new java.awt.event.MouseMotion Adapter() {
public void mouseDragged(java.awt.even t.MouseEve nt evt) {
formMouseDragged(evt);
}
});
addMouseListener(new java.awt.event.MouseAdapte r() {
public void mousePressed(java.awt.even t.MouseEve nt evt) {
formMousePressed(evt);
}
public void mouseReleased(java.awt.eve nt.MouseEv ent evt) {
formMouseReleased(evt);
}
});
}
private void formMousePressed(java.awt. event.Mous eEvent evt) {
}
private void formMouseReleased(java.awt .event.Mou seEvent evt) {
}
private void formMouseDragged(java.awt. event.Mous eEvent evt) {
}
public void paintComponent(Graphics g) {
d = getSize();
if (theBuffer == null) {
theBuffer = new BufferedImage(d.width, d.height,BufferedImage.TYP E_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
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.LineBor
setToolTipText("Grid Panel");
addMouseMotionListener(new
public void mouseDragged(java.awt.even
formMouseDragged(evt);
}
});
addMouseListener(new java.awt.event.MouseAdapte
public void mousePressed(java.awt.even
formMousePressed(evt);
}
public void mouseReleased(java.awt.eve
formMouseReleased(evt);
}
});
}
private void formMousePressed(java.awt.
}
private void formMouseReleased(java.awt
}
private void formMouseDragged(java.awt.
}
public void paintComponent(Graphics g) {
d = getSize();
if (theBuffer == null) {
theBuffer = new BufferedImage(d.width, d.height,BufferedImage.TYP
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
ASKER
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
So while working with complex algorithms like bresenham's line algorithm, to fill pixels in a grid, they depends on available awt methods
Regards
ASKER
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.LineBor der(new java.awt.Color(0, 0, 0)));
setToolTipText("Grid Panel");
addMouseMotionListener(new java.awt.event.MouseMotion Adapter() {
public void mouseDragged(java.awt.even t.MouseEve nt evt) {
formMouseDragged(evt);
}
});
addMouseListener(new java.awt.event.MouseAdapte r() {
public void mousePressed(java.awt.even t.MouseEve nt evt) {
formMousePressed(evt);
}
public void mouseReleased(java.awt.eve nt.MouseEv ent evt) {
formMouseReleased(evt);
}
});
}
private void formMousePressed(java.awt. event.Mous eEvent evt) {
}
private void formMouseReleased(java.awt .event.Mou seEvent evt) {
}
private void formMouseDragged(java.awt. event.Mous eEvent evt) {
}
public void paintComponent(Graphics g) {
d = getSize();
if (theBuffer == null) {
theBuffer = new BufferedImage(d.width, d.height,BufferedImage.TYP E_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(xStar t,yStart);
myline.setEndPoint(xEnd,yE nd);
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.bl ue);
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?
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.LineBor
setToolTipText("Grid Panel");
addMouseMotionListener(new
public void mouseDragged(java.awt.even
formMouseDragged(evt);
}
});
addMouseListener(new java.awt.event.MouseAdapte
public void mousePressed(java.awt.even
formMousePressed(evt);
}
public void mouseReleased(java.awt.eve
formMouseReleased(evt);
}
});
}
private void formMousePressed(java.awt.
}
private void formMouseReleased(java.awt
}
private void formMouseDragged(java.awt.
}
public void paintComponent(Graphics g) {
d = getSize();
if (theBuffer == null) {
theBuffer = new BufferedImage(d.width, d.height,BufferedImage.TYP
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(xStar
myline.setEndPoint(xEnd,yE
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.bl
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?
ASKER
well how come the exmaple i found in the applet worked and it was in java?
ASKER
i think i need to insert that code form applet into my code adn get it to work?
would that 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);
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
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
ASKER
can you help me incode the applet code into my my application?
ASKER
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...
just that the other guy just left me in the dark after i told him what i was trying to do...
ASKER
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
I can try to post the answer by this night or tomorrow morning.
It is 6:00 pm here
ASKER
ok, its 1.00 pm here
thank you
thank you
So are you from Europe?? Probably U.K
as time diff for G.M.T and our's is 5:30 hrs
as time diff for G.M.T and our's is 5:30 hrs
ASKER
yes scotland!
ASKER
i work for a oil company
ASKER
well any luck?
how are you getting?
how are you getting?
why is this question only for sudhakar??:)
Thanks
Thanks
ASKER
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!
ASKER
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
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
ASKER
kinda urgent! company wants it by this monday coming
ASKER
Wow ..... sudhakar_koundinya is a girl ......
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER