单个 java.awt.Rectangle 可以用两种不同的边界颜色绘制吗?
我有一个简单的 Java 程序,允许用户在 JPanel 上绘制矩形,然后移动它们、调整它们的大小并删除它们。
绘图面板实现了MouseListener和MouseMotionListener。当事件被触发时,它会检查选择了哪个菜单选项(新矩形、移动、调整大小或删除),并做出相应反应。
当选择“调整大小”选项时,侦听器的方法将执行以下操作:
MouseMoved 调用 boolean detectorBoundary()。当返回 true 时,边界所属的矩形将设置为活动矩形。
MouseDragged 调用 void moveBoundary,它会沿拖动手势的方向移动检测到的边界。
现在我正在寻找一种方法,使要移动的边界脱颖而出。我可以用更粗的线条或不同的颜色重新绘制整个矩形,这就是我现在将给定矩形设置为活动矩形时所做的,但这不是我想要的。我想只重新着色一个边界。
可以处理 BorderFactory 的 createMatteBorder 方法的 setBorder 方法似乎非常适合这些目的,但我还没有找到一种方法来实现这一目的。
这里有人知道我如何实现这一目标吗?
所有建议将不胜感激。
I have a simple Java program that allows a user to draw rectangles on a JPanel, then move them around, resize them, and delete them.
The drawing panel implements MouseListener and MouseMotionListener. When an event is triggered, it checks which menu option is selected (new rectangle, move, resize, or delete), and reacts accordingly.
When the 'resize' option is selected, the listener's methods do the following:
MouseMoved calls boolean detectBoundary(). When that returns true, the rectangle to which the boundary belongs is set as the active rectangle.
MouseDragged calls void moveBoundary, which moves the detected boundary in the direction of the dragging gesture.
Now what I am looking for is a way to make the boundary that is going to be moved stand out. I can re-paint the whole rectangle in thicker lines or a different color, which is what I do now when I set a given rectangle as the active one, but that's not what I want. I'd like to re-color just the one boundary.
A setBorder method that can handle BorderFactory's createMatteBorder method would seem to be ideal for these purposes, but I haven't been able to find a way to make that work.
Does anyone here have an idea how I could accomplish this?
All suggestions will be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以调用 java.awt.Graphics 上的 setColor(Color color) 方法吗?
听起来你可能要求一些更复杂的东西,但我不确定到底是什么。
如果您想在同一个矩形上使用两种不同的边界颜色,我认为您必须使用两个矩形对象来执行此操作。顶部矩形将具有透明填充。这两个矩形需要一起移动,并且移动完成后需要从视图中删除第二个矩形。
我不确定是否可以仅更改一个简单矩形的一个边缘的颜色,但是您可以从多个形状中构建一个更复杂的形状,或者您可以将矩形绘制到 BufferedImage 中并在上面画一条线顶部有不同的颜色。
Could you call the setColor(Color color) method on java.awt.Graphics?
It sounds like you might be asking for something more complicated, but I'm not sure exactly what.
If you want two different boundary colors on the same rectangle, I think you would have to use two rectangle objects to do this. The top rectangle would have a transparent fill. The two rectangles would need to move together, and the second rectangle would need to be removed from the view when the move is complete.
I'm not sure if it is possible to change the color of just one edge of a simple rectangle, but you could build a more complex shape out of multiple shapes, or alternatively you could draw your rectangle into a BufferedImage and draw a line over the top in a different color.
BorderFactory 类通常用于为 Swing 组件创建边框,因此我不确定这是否也适用于您的情况。您是否尝试过创建一个带边框的新面板
,然后将矩形添加到该面板并将其添加到您实际绘制的现有面板?
The BorderFactory class is usually used for creating borders for Swing components, so I am not sure if this works also in your case. Have you tried to create a new panel with border
and then add your Rectangle to the panel and add it to the existing panel where you are actually drawing?