0

i am working on a Bresenham Line drawing algorithm in Java and i have been able to draw the line but i am having a problem with the coordinates. My line starts from the top left corner of the screen and i want it to start from the bottom left corner. I have tried Affine Transform but failed. Here is a sample of my code.

public void paintComponent( Graphics g )
{
    Graphics2D g2d = (Graphics2D) g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setColor(Color.GREEN);
    g2d.fillRect(0, 0, getWidth(), getHeight());    
    int xElemSize = this.getWidth() / this.screenPixel.getSizeX();
    int yElemSize = this.getHeight() / this.screenPixel.getSizeY();             
    int rectX = 0, rectY = 0;

    for (int i = 0; i < this.screenPixel.getSizeX(); i++)
    {
        for (int h = 0; h < this.screenPixel.getSizeY(); h++)
        {   
            if (screenPixel.matrix[i][h] != 0)
            {
                rectX = i * xElemSize;
                rectY = h * yElemSize;      
                Rectangle2D rect = new Rectangle2D.Double(rectX, rectY, xElemSize, yElemSize);
                g2d.setColor(Color.GREEN);
                g2d.fill(rect);
                g2d.draw (rect);
                bresenham_Linie(x1, y1, x2, y2);
            }
        }
    }
}

Thanks for ur help!!

Anthony Raymond
  • 7,434
  • 6
  • 42
  • 59
user3628617
  • 29
  • 1
  • 4
  • So instead of going top-down (like \\) you need the line to be bottom up(like /) ? – Hirak May 12 '14 at 13:25
  • HI Hirak, thats right. i want the line to be bottom up / – user3628617 May 12 '14 at 13:29
  • Not only you need to use 0,Height as your starting co ords, you will need to adjust the bresenham implementation to "subtract" the subsequent co-ords instead of adding in order to achieve the bottom-up. Just by changing 0,0 to 0,height will just start a top-down line from the middle of the screen. – Hirak May 12 '14 at 13:49
  • 1
    This [example](http://stackoverflow.com/a/9373195/230513) uses `AffineTransform`. – trashgod May 12 '14 at 16:10

1 Answers1

1

Java2D coordinates are different than you think. In Java2D, top-left is (0,0). Bottom-left is (0, HEIGHT).

Consult the official tutorial: http://docs.oracle.com/javase/tutorial/2d/overview/coordinate.html

for (int h = 0, maxH=this.screenPixel.getSizeY(); h < maxH; h++)
{   
    if (screenPixel.matrix[i][h] != 0)
    {
        rectX = i * xElemSize;
        rectY = (maxH-h) * yElemSize;      
        // ... 
    }
}
Dariusz
  • 21,561
  • 9
  • 74
  • 114