我正在使用Java进行Bresenham Line绘图算法,我已经能够画线但是我的坐标有问题。我的线从屏幕的左上角开始,我希望它从左下角开始。我尝试了仿射变换,但失败了。以下是我的代码示例。
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);
}
}
}
}
感谢您的帮助!!
答案 0 :(得分:1)
Java2D坐标与您的想法不同。在Java2D中,左上角是(0,0)。左下角是(0,HEIGHT)。
参考官方教程: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;
// ...
}
}