控制台打印的值与屏幕上显示的值完全不同

时间:2014-11-20 23:46:03

标签: java canvas polygon

我正在关注一本书中的一个例子"基础2D游戏编程与Java"。在那本书中, 其中一个例子展示了如何绘制多边形,在这种情况下,它是一个矩形。我开始抛出sysouts来更好地理解,如何 多边形绘图有效。

......那些系统让我完全糊涂了。 sysouts打印的值与屏幕上显示的值完全不同。 控制台中的值毫无疑问是错误的。但是可能导致什么呢?

编辑:附加信息:Vector2f是一个包含X和Y浮点值的类。它还具有翻译,缩放,剪切和旋转整个多边形的方法

这是绘制多边形的代码:

public static void drawPolygon(Graphics g, Vector2f[] polygon) 
{
    Vector2f P;
    Vector2f S = polygon[polygon.length - 1];

    System.out.println("Starting drawing:");

    for (int i = 0; i < polygon.length; ++i) 
    {           
        //Just for testing, calls g.setColor(), nothing relevant to the problem
        setColor(i, g);                     
        P = polygon[i];

        //Debug drawing, draws the coordinates (Like in screenshot, in red color: '(322, 250)')
        g.drawString("(" + (int)S.x + ", " + (int)S.y + ")", (int)S.x, (int)(S.y));

        //Draws the rectangle, line by line in for loop (first red, then green, blue, orange)
        g.drawLine((int) S.x, (int) S.y, (int) P.x, (int) P.y);

        //System outs that prints completely different values than what is shown on screen!
        System.out.println("S.x = " + (int)S.x);
        System.out.println("S.y = " + (int)S.y);
        System.out.println("P.x = " + (int)P.x);
        System.out.println("P.y = " + (int)P.y);
        System.out.println("Drawing line from (" + (int)S.x + "," + (int)S.y + ") to (" + (int)P.x + "," + (int)P.y + ")");
        S = P;
    }

    System.out.println("End of drawing a polygon");
}

这是此特定电话的控制台输出:

  Starting drawing:
  S.x = -316
  S.y = -388
  P.x = -316
  P.y = -468
  Drawing line from (-316,-388) to (-316,-468)
  S.x = -316
  S.y = -468
  P.x = -236
  P.y = -468
  Drawing line from (-316,-468) to (-236,-468)
  S.x = -236
  S.y = -468
  P.x = -236
  P.y = -388
  Drawing line from (-236,-468) to (-236,-388)
  S.x = -236
  S.y = -388
  P.x = -316
  P.y = -388
  Drawing line from (-236,-388) to (-316,-388)
  End of drawing polygon

这里是Rectangle和drawString() - 方法

的截图

Rectangle-polygon draw

我真的很困惑。我开始没有想法了。我正在使用Eclipse。有什么想法吗?

0 个答案:

没有答案