user3628617
user3628617

Reputation: 29

Java Coordinate System

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!!

Upvotes: 0

Views: 3206

Answers (1)

Dariusz
Dariusz

Reputation: 22241

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;      
        // ... 
    }
}

Upvotes: 1

Related Questions