fillRect() 中的负数

发布于 2024-10-03 02:25:41 字数 136 浏览 1 评论 0原文

我使用负数让 fillRect() 走相反的方向。它在我的计算机上运行良好,但我的朋友看不到它正常运行。我该怎么办?

page.fillRect (12, 12, -5, 10);

I am using a negative number to have fillRect() go the opposite way. It works well on my computer, but my friends cannot see it working properly. What do I do?

page.fillRect (12, 12, -5, 10);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

给妤﹃绝世温柔 2024-10-10 02:25:41

对此有两种观点。

一是任何具有负尺寸的多边形都是无意义的,不应该被渲染 - 你是什么意思它是 -10 像素宽?

另一个是负尺寸只是简单地反转该尺寸轴上的多边形。

大多数绘画系统都应用后一种逻辑。我用Java绘画的经验一直都是这样,尽管我不会经常有负面的维度,所以我的经验可能没有多大意义。

至于你能做什么:

  1. 你可能需要让你的朋友更新他的 Java 版本。
  2. 否则,您必须自己执行反演,方法是检查负尺寸并转置 x,y 原点以使尺寸为正。也就是说,矩形 [10,10,-5,5] 在空间上等效于 [5,10,5,5]。将负尺寸添加到原点坐标,然后设为绝对尺寸。请注意,这可能会导致负原点,但希望绘图系统不会完全混乱。

就我个人而言,如果可能的话,我更愿意需要更新的 JVM。

否则我推荐的代码是:

public void fillRect(Graphics gc, int x, int y, int width, int height) {
    if(width <0) { x+=width;  width =-width;  }
    if(height<0) { x+=height; height=-height; }
    gc.fillRect(x,y,width,height);
    }

另外,请注意,您将需要类似的其他绘图操作。

最后,我会怀疑这一切中的另一个错误,因为这种行为非常令人惊讶 - 我会将所有内容简化为一个最小的测试程序,在包装大量绘画基元之前进行调试输出和验证。

There are two schools of thought on this.

One is that any polygon with a negative dimension is nonsensical and should not be rendered - what do you mean it's -10 pixels wide??

The other is that a negative dimension simply inverts the polygon on that dimension's axis.

Most painting systems apply the latter logic. My experience in painting with Java has always been this, though I would not often have had negative dimensions, so my experience might not count for much.

As to what you can do:

  1. You might need to have your friend update his Java version.
  2. Otherwise you must perform the inversion yourself, by checking for a negative dimension and transposing your x,y origin in order to make the dimension positive. That is, the rectangle [10,10,-5,5] is spatially equivalent to [5,10,5,5]. The negative dimension is added to the origin coordinate, and then made absolute. Note, this can result in a negative origin, but hopefully the drawing system is not quite that messed up.

Personally, I would prefer to require a newer JVM, if possible.

Otherwise my recommended code would be:

public void fillRect(Graphics gc, int x, int y, int width, int height) {
    if(width <0) { x+=width;  width =-width;  }
    if(height<0) { x+=height; height=-height; }
    gc.fillRect(x,y,width,height);
    }

Also, note that you'll need similar for other drawing operations.

Lastly, I would be suspicious of another mistake in all this, since the behavior is very surprising - I would simplify everything down to a minimal test program, with debug output and verify before wrapping a crap load of painting primitives.

脱离于你 2024-10-10 02:25:41

我的猜测是您和您的朋友正在使用 2 个不同版本的 Java。你支持 fillrect 为负值,而你朋友则不支持。 (甚至是 sdk 与 jre)。

这表明您应该以不同的方式编写它,以使您的小程序在所有版本上运行。

为什么不简单地移动你的 x 为 -5 呢?

如果你想以更简洁的方式做到这一点

public void myfillRect(graphics page, int x, int y, int width, int height){
    if(width <0)
        x-=Math.abs(width);
    if(height <0)
        y-=Math.abs(height);

    page.rectfill(x,y,Math.abs(width), Math.abs(height));
} 

My guess is that you and your freind are using 2 different versions of Java. your supports fillrect with a negative value and your freind's doesn't. (or even sdk vs jre).

what that shows is that you should write this a different way to have your applet run on all versions.

why not simply move your x of -5 ?

if you want to do it in a more neat way

public void myfillRect(graphics page, int x, int y, int width, int height){
    if(width <0)
        x-=Math.abs(width);
    if(height <0)
        y-=Math.abs(height);

    page.rectfill(x,y,Math.abs(width), Math.abs(height));
} 
哥,最终变帅啦 2024-10-10 02:25:41

只是指出劳伦斯代码中的一个小问题,当您向上拖动给出错误高度值的矩形时出现该问题。

问题出在行中

if(height<0) { x+=height; height=-height; }

必须是 y 而不是 x :

if(height<0) { y+=height; height=-height; }

,总共

public void fillRect(Graphics gc, int x, int y, int width, int height) {
    if(width <0) { x+=width;  width =-width;  }
    if(height<0) { y+=height; height=-height; }
    gc.fillRect(x,y,width,height);
}

Just indicating a small issue in Lawrence code that appear when you drag up the rect which give wrong height value.

The problem is in the row

if(height<0) { x+=height; height=-height; }

and it has to be y not x

if(height<0) { y+=height; height=-height; }

in total:

public void fillRect(Graphics gc, int x, int y, int width, int height) {
    if(width <0) { x+=width;  width =-width;  }
    if(height<0) { y+=height; height=-height; }
    gc.fillRect(x,y,width,height);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文