This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
i am adding a crop feature to my paint program. i am going to(for now) draw a rectangle and then pop up a dialogue asking crop/cancel. i would like it so when the mouse is being dragged it shows numbers near the cursor showing relative x and y coordinates(width and height). i would also like it if instead of drawing a solid rectangle, i drew a marquee. any help or ideas welcome
You would certainly get more help here if you showed what you have done so far (if anything).
Suppose you have a custom panel defined as a canvas for working with image (say, ImagePanel which extends JPanel). For what you want, you only need to keep track on two points: starting (point where user has clicked mouse and began marking an area to crop) and ending (point where user released mouse click). With those you have actually marked an area to crop from original image. These would be some steps you should follow in order to get it done:
For getting a starting point, you need to register MouseListener to ImagePanel. Since you just use two events, MouseAdapter would do the job, where you would override mousePressed(MouseEvent event) which will get you starting point and mouseReleased(MouseEvent event) which you can use to reset starting point to null so area to crop will not be displayed when user finishes cropping.
In order to obtain coordinates of mouse cursor over canvas, you need to register MouseMotionListener to your ImagePanel. Since you want to get that information only when drawing area to crop, MouseMotionAdapter would do the job, where you would override mouseDragged(MouseEvent event) method. From this event you can get ending point for cropping area.
In addition, your canvas should override paintComponent(Graphics g) method in order to draw a rectangle marking area to crop.
So, basically, you would have something like this:
You should also check Graphics2D#setStroke(Stroke stroke) method to make your rectangle be dashed (or whatever).
The quieter you are, the more you are able to hear.
i have already done ALL those thing. i am just adding a new feature. i just want to know how to display a tooltip kind of thing showing the height and width of the selection, and to draw a marquee instead of a regular rectangle(or nothing)