Is it possible to draw one java.awt.Rectangle with two different boundary colors?

I have a simple Java program that allows a user to draw rectangles on a JPanel and then move them around, resize them and delete them.

The drawing panel implements MouseListener and MouseMotionListener. When an event is fired, it checks which menu option is selected (new rectangle, move, resize or delete) and reacts accordingly.

When the "resize" option is selected, the listener methods do the following:

  • MouseMoved calls boolean detectBoundary (). When this returns true, the rectangle to which the border belongs is set as the active rectangle.

  • MouseDragged calls a void moveBoundary, which moves the detected border in the direction of the drag gestures.

Now what I'm looking for is a way to make a border that will move, stand out. I can re-draw the entire rectangle in thicker lines or in a different color, which I’m doing now when I set this rectangle to be active, but that’s not what I want. I would like to repaint only one border.

The setBorder method, which can handle the BorderFactory createMatteBorder method, seems ideal for these purposes, but I could not find a way to do this work.

Does anyone have an idea how I could do this?

All suggestions will be greatly appreciated.

+4
source share
2 answers

Could you call the setColor (Color) method on java.awt.Graphics?

It sounds like you could ask for something more complicated, but I don’t know for sure. If you need two different border colors on the same rectangle, I think you will need to use two rectangle objects for this. The top rectangle will have a transparent fill. Two rectangles would have to move together, and the second rectangle would have to be removed from the view when the movement was complete.

I'm not sure that you can change the color of only one edge of a simple rectangle, but you could create a more complex shape from several shapes or, conversely, you could draw your own rectangle in BufferedImage and draw a line on top of everything in a different color.

0
source

The BorderFactory class is usually used to create borders for Swing components, so I'm not sure if this works in your case either. You tried to create a new panel with a frame

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

and then add a rectangle to the panel and add it to the existing panel that you are actually drawing on?

0
source

Source: https://habr.com/ru/post/1301963/


All Articles