使用 Java Graphics 进行内部剪裁
我需要使用 java.awt.Graphics 绘制一条线,但只应渲染位于矩形之外的线部分。
是否可以使用图形剪切支持,或者我是否需要自己计算交集并剪切线?
I need to draw a line using java.awt.Graphics, but only the portion of the line that lies outside of a rectangle should be rendered.
Is it possible to use the Graphics clipping support or do I need to calculate the intersection and clip the line myself?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要使用 Area 类。 此示例将演示如何执行您所要求的操作:
You need to use the Area class. This example will demonstrate how to do what you ask:
您可以使用 AWT 剪辑来完成此操作。 您需要知道要排除的矩形的边界以及绘图区域的外边界。
以下演示代码打开一个框架并在其中显示一个面板。 面板的绘制方法设置了一个示例剪辑,它看起来像一个中间有一个矩形孔的矩形,而实际上它是一个多边形,描述了我们要排除的区域周围的区域。 剪辑矩形应由排除矩形的边界和绘图区域的外边缘组成,但我保留了硬编码值以保持简单并更好地说明工作原理(我希望!)
此方法具有与手动计算线相交相比的好处是,它可以防止所有 AWT 绘画进入排除区域。 我不知道这对你是否有用。
然后,我的演示在整个区域上绘制一个黑色矩形,并穿过它一条白色对角线,以说明剪辑的工作原理。
You can do this with an AWT clip. You'll need to know the bounds of the rectangle you want to exclude, and the outer bounds of your drawing area.
The following demo code opens a frame and displays a single panel in it. The panel's paint method sets up an example clip which looks like a rectangle with a rectangular hole in the middle, when in fact it's a polygon that describes the area around the area we want to exclude. The clip rectangle should be composed of the bounds of the excluded rectangle, and the outer edge of the drawing area, but I've left hard-coded values in to keep it simple and illustrate the workings better (I hope!)
This method has the benefit over calculating the line intersection manually in that it prevents all AWT painting going into the excluded area. I don't know if that's useful to you or not.
My demo then paints a black rectangle over the whole area, and a single white diagonal line running through it, to illustrate the clip working.