• Post Reply Bookmark Topic Watch Topic
  • New Topic

showing dotted line  RSS feed

 
Tony Chow
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible for a rectangle with dotted side be shown when dragging the mouse on a component?
Just something like the one in the attachments, usually occurs in some graphics/pictures software. Please help.

2014-07-07-172247.png
[Thumbnail for 2014-07-07-172247.png]
 
Gary W. Lucas
Ranch Hand
Posts: 65
6
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, from your text, there's a lot of room for interpretation in terms of what exactly you're trying to do. I can see two possible scenarios:

1) You are using a JPanel to show some artwork and want to click and drag a rectangle over it.
2) You have a fully-functional user interface and you want it to operate normally (responding to mouse and keyboard events) except when you perform a click-and-drag.

In the first case, it's relatively easy. Just add a Mouse listener and MouseMotion listener to the JPanel. Use some member elements to keep track of when you are dragging (recording the initial mouse press position and the current drag position). Override the JPanel's paintComponent method to paint the drag-rectangle. Have the mouseDragged method call repaint() to cause your JPanel to redraw as the mouse moves along. To enhance efficiency and application response, don't repaint the whole component, but only the region necessary to cover the previous rectangle (so it's removed) and the current one (so it's shown). The best way I know of for drawing the dotted line is to use the setStroke method in Graphics2D to set a BasicStroke instance with a dotted line.

In the second case, in which you have a standard UI, and want it to behave normally (buttons responding to mouse presses, etc), except when you do a click and drag.... This is a little more involved. You can create and set glass pane (see webpage ) to intercept all the events except the click-and-drag and then redispatch them to the underlying components. For the click and drag, you would override the paintComponent method for your glass pane class in a manner similar to the JPanel example shown above.

Gary
 
Rob Camick
Ranch Hand
Posts: 2788
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't do the custom painting in the paintComponent() method as suggested above. This means you would need to do custom painting on every component you wanted to drag.

Instead create a custom Border and do the custom painting. Then when you click on the component you add your custom Border on an a release you restore the original Border.

Read the section from the Swing tutorial on How to Use Borders for more information.
 
Tony Chow
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright maybe I explain it more...
It is quite complicated. For example, in Paint (mspaint), or in some picture viewers like XnViewMP, there is a selection tool that allows the user to drag and select, and say, a "dotted rectangle" appears. It can move to other locations, not being resized (the user just drags the rectangle and then move). For the corners, there are four small points, allowing user to resize the rectangle by dragging the rectangle. At the mid-point of each side, there are also small points that when one of them is dragged, the rectangle will resize according to the drag.

I have no ideas how to make the "dotted rectangle". Any ideas?
 
Gary W. Lucas
Ranch Hand
Posts: 65
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you should look at Rob Camick's suggestion as well as mine, because I suspect he has something rather clever in mind... But here's my idea. There's a lot to digest here and also a lot of opportunities for improvement, but I think it illustrates the principle.

Gary


 
Rob Camick
Ranch Hand
Posts: 2788
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suspect he has something rather clever in mind


Not really clever, its just more the way the way the API was designed to be used. Your code may work great for a JPanel. But what if you want to drag a JLabel or JButton or JTextField or JCheckbox etc... Now you will need to create custom implementations of every component.

A Border can be added to any component. I suggest you challenge yourself and try to create a "DragBorder" that you can add to any component. Basically all you need to do is take the custom painting code and move it to the paintPorder() method of the custom Border.
 
Gary W. Lucas
Ranch Hand
Posts: 65
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tony,

One thing I should have noted about my code example: The key to the dotted line is the use of the Graphics2D object. In Swing, all the Graphics objects you see are actually Graphics2D objects in disguise. So you can just cast them to Graphics2D as needed... Historically, the original Java implemented included the Graphics class and a lot of the original API user interface methods took Graphics as an argument. Later on (in Java release 1.2), when the more capable Graphics2D was introduced (along with Java2D), Sun adopted the current strategy as a way of supporting backward compatibility.

Graphics2D supports a much broader range of visual effects including thick or dotted lines (via the BasicStroke class), transparency (via AlphaComposite and others), and fill patterns (via Paint). To find out more, Google Java2D.

Gary
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!