简体   繁体   English

可以用两种不同的边界颜色绘制单个java.awt.Rectangle吗?

[英]Can a single java.awt.Rectangle be painted with two different boundary colors?

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. 我有一个简单的Java程序,允许用户在JPanel上绘制矩形,然后移动它们,调整它们并删除它们。

The drawing panel implements MouseListener and MouseMotionListener. 绘图面板实现MouseListener和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(). MouseMoved调用boolean detectBoundary()。 When that returns true, the rectangle to which the boundary belongs is set as the active rectangle. 当返回true时,边界所属的矩形被设置为活动矩形。

  • MouseDragged calls void moveBoundary, which moves the detected boundary in the direction of the dragging gesture. MouseDragged调用void moveBoundary,它在拖动手势的方向上移动检测到的边界。

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. 可以处理BorderFactory的createMatteBorder方法的setBorder方法似乎是出于这些目的的理想选择,但我还没有找到一种方法来实现这一目的。

Does anyone here have an idea how I could accomplish this? 这里有没有人知道我怎么能做到这一点?

All suggestions will be greatly appreciated. 所有建议将不胜感激。

Could you call the setColor(Color color) method on java.awt.Graphics? 你能在java.awt.Graphics上调用setColor(Color color)方法吗?

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. 我不确定是否可以改变一个简单矩形的一个边缘的颜色,但你可以用多个形状构建一个更复杂的形状,或者你可以将你的矩形绘制成一个BufferedImage并画一条线顶部有不同的颜色。

The BorderFactory class is usually used for creating borders for Swing components, so I am not sure if this works also in your case. BorderFactory类通常用于为Swing组件创建边框,因此我不确定这是否也适用于您的情况。 Have you tried to create a new panel with border 您是否尝试使用边框创建新面板

JPanel panel = new JPanel(); 
Border mb = (BorderFactory.createMatteBorder (0, 5, 0, 0, Color.red); 
panel.add(mb);

and then add your Rectangle to the panel and add it to the existing panel where you are actually drawing? 然后将矩形添加到面板并将其添加到您实际绘制的现有面板中?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM